Exam PT0-002 All QuestionsBrowse all questions from this exam
Question 278

A penetration tester developed the following script to be used during an engagement:

However, when the penetration tester ran the script, the tester received the following message:

socket.gaierror: [Errno -2] Name or service not known

Which of the following changes should the penetration tester implement to fix the script?

    Correct Answer: A

    The error message 'socket.gaierror: [Errno -2] Name or service not known' indicates that the hostname provided to the script cannot be resolved. This is occurring because sys.argv[0] refers to the script name itself, not the first argument passed to the script. To obtain the target IP address or hostname, sys.argv[1] should be used instead. Thus, the correct modification to fix the script is to change 'target = socket.gethostbyname(sys.argv[0])' to 'target = socket.gethostbyname(sys.argv[1])'.

Discussion
TiredOfTestsOption: A

A. From: target = socket.gethostbyname(sys.argv[0]) To: target = socket.gethostbyname(sys.argv[1])

TytussOption: C

"The error message socket.gaierror: [Errno -2] Name or service not known is typically raised when a hostname cannot be resolved. In this case, the issue lies in this line of the script: target = socket.gethostbyname(sys.argv[0]) Here, sys.argv[0] is being used, which actually refers to the name of the script itself, not the first argument passed to the script. The first argument passed to the script is sys.argv[1]. So, to fix the script, the penetration tester should replace sys.argv[0] with sys.argv[1]. The corrected line should be: target = socket.gethostbyname(sys.argv[1])

Etc_Shadow28000Option: A

The issue in the script is caused by the incorrect usage of sys.argv[0] to retrieve the target IP address. sys.argv[0] contains the name of the script itself, not the arguments passed to it. The correct way to retrieve the first argument (the target IP address) is sys.argv[1]. From: target = socket.gethostbyname(sys.argv[0]) To: target = socket.gethostbyname(sys.argv[1])