Python Port Scanning Script Development
During a penetration test, you gain access to a system with a limited user interface. This machine appears to have access to an isolated network that you would like to port scan. You need to complete a Python port scanning script to identify open ports on target systems in the isolated network. Target: The script should scan ports 21 (FTP) and 22 (SSH) on a host provided as a command-line argument.
#!/usr/bin/python#!/usr/bin/ruby#!/usr/bin/bashports = [21, 22]{ports => 21 ports => 22}export SPORTS = 21,22for port in ports:
try:
s.connect((ip, port))
print("%s:%s - OPEN" % (ip, port))
except socket.timeout:
print("%s:%s - TIMEOUT" % (ip, port))
except socket.error as e:
print("%s:%s - CLOSED" % (ip, port))
finally:
s.close()for SPORT IN SPORTS:
try:
s.connect((ip, port))
print("%s:%s - OPEN" % (ip, port))
except socket.timeout
print("%s:%s - TIMEOUT" % (ip, port))
except socket error as e
print("%s:%s - CLOSED" % (ip, port))
finally
s.close()port_scan(sys.argv[1], ports)run_scan(sys.argv[1], SPORTS)shebang:
imports:
ports:
loop:
call: