Python与Hack之window下运行带参数的Python脚本,实现一个简单的端口扫描器

1.前提是:windows已经配置好Python的环境变量;

 

2.进入cmd命令行模式:

**输入python命令,检测是否环境配置好:显示这样说明配置环境变量没问题

 

**用cd命令进入Python脚本所在的目录:

 

**Python脚本所在目录下输入命令:python "这里直接把python脚本拖拽过来"  后边加参数:

 

 **脚本的代码问题可能导致,cmd命令行下运行脚本后没有任何反应,所以务必检查脚本正确:

**这里要说明的是可以使用print()函数逐行检测代码,输出对应的参数;

**Python3下的:

if ():            //if后边必须跟else才能执行

else:

 

下面是一个Python脚本写的端口扫描器:

#!C:\Python
#coding = utf-8
import optparse
import socket
from socket import *

def connScan(tgtHost,tgtPort):
try:
connSkt=socket(AF_INET,SOCK_STREAM)
connSkt.connect((tgtHost,tgtPort))
connSkt.send('ViolentPython\r\n')
results=connSkt.recv(100)
print('[+] %d/tcp open'% tgtPort)
print('[+]'+str(results))
connSkt.close()
except:
print('[-]%d/tcp closed'% tgtPort)
def portScan(tgtHost,tgtPorts):
try:
print("Hello Word")
tgtIP=gethostbyname(tgtHost)
except:
print("[-] Cannot resolve '%s':Unknown host"%tgtHost)
return
try:
tgtName=gethostbyaddr(tgtIP)
print('\n[+] Scan Results for:'+tgtName[0])
except:
print('\n[+] Scan Results for:'+tgtIP)
setdefaulttimeout(1)
for tgtPort in tgtPorts:
print('Scanning port:'+tgtPort)
connScan(tgtHost,int(tgtPort))
def main():
parser=optparse.OptionParser('usage %prog -H'+\
'<target host> -p <target port>')
parser.add_option('-H',dest='tgtHost',type='string',\
help='specify target host')
parser.add_option('-p',dest='tgtPort',type='string',\
help='specify target port')
(options,args)=parser.parse_args()
tgtHost=options.tgtHost
tgtPorts=str(options.tgtPort).split(',')
print(tgtHost)
print(tgtPorts)
if (tgtHost==None) | (tgtPorts[0]==None):
print('[-] You must specify a target host and port[s].')
exit(0)
else:
portScan(tgtHost,tgtPorts)
if __name__=='__main__':
main()

运行结果如下:

 


posted @ 2016-11-26 15:41  小春熙子  阅读(712)  评论(0编辑  收藏  举报