sys and subprocess of python module and writing simple host scanning script

sys and subprocess of python module and writing simple host scanning script 1.sys module sys.exit(n) function: the interpreter exits automatically whe...

sys and subprocess of python module and writing simple host scanning script

1.sys module

sys.exit(n) function: the interpreter exits automatically when the program is executed to the end of the main program. However, if you need to exit the program halfway, you can call the sys.exit function, with an optional integer parameter returned to the calling program, indicating that you can capture the call to sys.exit in the main program. (0 is normal exit, others are abnormal)

sys.path function: gets the string collection of the specified module search path

sys.platform: get the current system platform

sys.argv passes parameters externally to the program

2.subprocess module (upgraded version of sys module, commonly used)

subprocess.run(): run the command and return the result of command execution (this command will be available in Python 3.5 and later versions)

subprocess.call(): execute the command, return the status of the command, 0 or non-0, 0 means the execution is successful

subprocess.getstatusoutput(): receive command in string form and return tuple form. The first element is execution status and the second is command result

Subprocess. Check "output()" function: Execute command and return binary result

3. Simple host scanning script writing

2.subprocess module

import subprocess #subprocess.run() #Effect:Run command,Returns the result of command execution(python3.5 Later versions will have this command) #r1=subprocess.run("dir",shell=True) #subprocess Want to call cmd Built in commands,Need to add parameters shell=True #r2=subprocess.run("ping www.baidu.com") #ping Program is a separate program,subprocess Can be called directly #print(r1) #print(r2) #subprocess.call() #Effect:Executive order,Returns the status of the command,0 Or non-0, 0 indicates successful execution,1 Indicates execution failed #r1=subprocess.call("dir",shell=True) #r2=subprocess.call("ping 8.8.8.8 -n 1") #print(r1) #print(r2) #subprocess.getstatusoutput() #Effect:Receive command as string,Return tuple form,The first element is the execution state,The second is the command result r1=subprocess.getstatusoutput("dir") print(r1) r2=subprocess.getstatusoutput("dir1") print(r2) r3=subprocess.getstatusoutput("ping 1.1.1.1") print(r3) #subprocess.check_output() #Effect:Executive order,And return binary results r1=subprocess.check_output("dir",shell=True) print(r1)

3. Simple host scanning script writing

import sys import os def ping(net,start=50,end=200,n=1,w=10): for i in range(start,end+1): ip=net+"."+str(i) command="ping %s -n %d -w %d >nul"%(ip,n,w) #>nul No display ping Information returned print(ip,("through","Impassability")[os.system(command)]) #print(os.popen(command).read()) #print(sys.argv) #print(len(sys.argv)) if len(sys.argv) not in [2,4,6]: print("Parameter input error!") print("Operation example:") print("test01.py 123.125.114") print("test01.py 123.125.114 30 50") print("test01.py 123.125.114 30 50 4,5") print("grammar:test01.py net startip,endip,count timeout") elif len(sys.argv)==2: net=sys.argv[1] ping(net) elif len(sys.argv)==4: net=sys.argv[1] ping(net,start=int(sys.argv[2]),end=int(sys.argv[3])) else: net=sys.argv[1] ping(net,start=int(sys.argv[2]),end=int(sys.argv[3]),n=int(sys.argv[4]),w=int(sys.argv[5]))

The operation effect is as follows:

4. Write with subprocess module

The code is as follows:

import sys import subprocess import time def ping(net,start=100,end=200,n=2,w=5): for i in range(start,end+1): ip=net+"."+str(i) command="ping %s -n %d -w %d"%(ip,n,w) print(ip,("through","Impassability")[subprocess.call(command,stdout=open("nul","w"))]) #stdout=open("nul","w") #Do not display the results returned by command execution t1=time.time() if len(sys.argv)!=2: print("Parameter input error!") print("Operation example:") print("test01.py 123.125.114") elif len(sys.argv)==2: net=sys.argv[1] ping(net) t2=time.time() print("Program time consuming%f second!"%(t2-t1)) #195.091611 second

2 December 2019, 08:43 | Views: 5756

Add new comment

For adding a comment, please log in
or create account

0 comments