我有一個簡單的Python程序,我目前正在開發,由於某種原因,它不起作用。
完整程序的工作方式是它有一個20個端口的列表,然後循環遍歷該列表,基本上ping所選端口上的主機。下面的代碼:Python - 我的套接字代碼有什麼問題
import socket
import sys
print ' +-====================================================-+'
print '/ \ '
print '| PyPortScanner |'
print '| by Ag3ntChr0m |'
print ' \ /'
print ' +-====================================================-+'
print ''
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + 'Error message: ' + msg[1]
sys.exit()
print 'Socket Created'
host = raw_input('Enter the desired host to scan: ')
port = [80, 443, 21, 22, 4567, 8080, 25, 3389, 23, 53, 1723, 110, 135, 445,
139, 1863, 143, 8081, 10000, 1025]
portFail = []
print 'Scanning top 20 most often open ports ...'
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
#couldn't resolve host at port
print 'Hostname could not be resolved. Program exiting'
sys.exit()
print 'IP address of ' + host + ' is ' + remote_ip
print '+-===================================-+'
print '| Ports Scanned:----------------------|'
print '+-===================================-+'
print ''
#Connect to remote server
for i in range(0, 20):
portScan = int(str(port[i])) <----
try:
s.connect((remote_ip, portScan))
print "\t" + str(portScan)
s.close()
except:
portFail.append(portScan)
err = True
raw_input('Press Enter to Continue...')
if err:
print '+-=============================-+'
print '| Failed Port Scan:-------------|'
print '+-=============================-+'
print ''
size = len(portFail)
for i in range(1, size):
print "\t" + str(portFail[i])
當你運行,那麼它應該
- 加載程序(在由箭頭標記的線)的端口號到一個變量的s.socket可以使用嘗試連接。
- 然後嘗試在主機上打開一個套接字(基本上ping它)然後關閉它。
- 如果它連接它將它寫在Ports Scanned下的屏幕上。
- 但是,如果它失敗(最近它已經),它會打印在失敗的端口掃描下。
當我運行該程序時,它打印Ports Scanned下的列表(80)上的第一個端口,但其餘部分置於Failed Port Scan下 - 即使我知道它們至少有一部分已打開。
我怎麼會得到這個程序ping比第一個成功的端口更多?
謝謝!這似乎工作!儘管不要試圖追蹤谷歌,但它會讓它變得糟糕。 – 2013-03-16 12:52:43