I'm working on a simple portscanner, and I want my Program to take two Options when executed by the command shell. In General it can only be executed from the shell since the options are absolutely required for the program.
I want the options to be:
-H
: The Host IP address
-p
: A list of all ports that should be scanned
Here is my problem: I want the ports to be separated by comma and a blank, like in this example of starting my program:
D:\LocalUser\MyPython\Portscanner>C:\Users\LocalUser\AppData\Local\Programs\Pytho
n\Python35-32\python.exe portscanner_v0.0.py -H 192.168.1.1 -p 21, 1720, 8000
Unluckily they seem to not be all collected by my program, Only the first value of the second option gets read to my variable in my code. I'm using optparse and Python 3.5, please tell me how to get ALL ports from the shell.
Here is my code:
def portScan(tgtHost, tgtPorts):
#doing my port scans
#wont show my actual code here, it's working fine
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[s] separated by comma')
(options, args) = parser.parse_args()
tgtHost = options.tgtHost
tgtPorts = str(options.tgtPort).split(', ')
if ((tgtHost == None) | (tgtPorts[0]==None)):
print(parser.usage)
exit(0)
portScan(tgtHost, tgtPorts)
if __name__ == "__main__":
main()
Args are already split by whitespace, so you'll need to use
and call it as
python.exe portscanner_v0.0.py -H 192.168.1.1 -p 21,1720,8000
Also note that the optparse module is deprecated since Python 2.7 and replaced by argparse.