python做这个事情很轻松,如果省去不必要的参数输入部分,代码可以更简单 :-)
直接上代码吧:
#-------------------------------------------------------------------------------
# Name: PortScan
# Purpose: 扫描目标主机的端口开放情况
#
# Author: xxh
#
# Created: 05-12-2011
# Copyright: (c) xxh 2011
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import socket
def main():
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(1000)
ip=input('请输入目标主机:(默认:127.0.0.1)')
if ip=='':
ip='127.0.0.1'
s=input('请输入目标主机开始端口:(默认:80)')
if s=='':
startport=80
else:
startport=int(s)
s=input('请输入目标主机结束端口:(默认:80)')
if s=='':
endport=80
else:
endport=int(s)
for port in range(startport,endport+1):
print('正在扫描端口:%d' % port)
try:
sk.connect((ip,port))
print('Server %s port %d OK!' % (ip,port))
except Exception:
print('Server %s port %d is not connected!' % (ip,port))
sk.close()
if __name__ == '__main__':
main()
# Name: PortScan
# Purpose: 扫描目标主机的端口开放情况
#
# Author: xxh
#
# Created: 05-12-2011
# Copyright: (c) xxh 2011
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import socket
def main():
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(1000)
ip=input('请输入目标主机:(默认:127.0.0.1)')
if ip=='':
ip='127.0.0.1'
s=input('请输入目标主机开始端口:(默认:80)')
if s=='':
startport=80
else:
startport=int(s)
s=input('请输入目标主机结束端口:(默认:80)')
if s=='':
endport=80
else:
endport=int(s)
for port in range(startport,endport+1):
print('正在扫描端口:%d' % port)
try:
sk.connect((ip,port))
print('Server %s port %d OK!' % (ip,port))
except Exception:
print('Server %s port %d is not connected!' % (ip,port))
sk.close()
if __name__ == '__main__':
main()