python - Argparse pass command line string to variable python3 -
i've read argparse
docs , trying make program takes command line arguments , passes values variables so:
ssh.py -username admin -password password -hostlist 192.168.1.1, 192.168.1.2 -commands uname -a, whoami
when statically assign these values within program works, unable argpase pass strings variables regardless of if destination list or single string.
this works:
hostlist = ['192.168.1.1','192.168.1.2'] username = 'admin' password = 'password' commands = ['uname -a','whoami']
this runs silently , not work or generate error or write log file:
parser = argparse.argumentparser() parser.add_argument('-u', '-username', help='the username authentication.') parser.add_argument('-p', '-password', help='the password authentication.') parser.add_argument('-l', '-hostlist', nargs='+', help='list of devices interact with.') parser.add_argument('-c', '-commands', nargs='+', help='an exact list of commands run') args = parser.parse_args() u,username = args.username p,password = args.password l,hostlist = args.hostlist c,commands = args.commands
there several problems code, , how you've used it.
the long option should specified 2 hyphens:
parser.add_argument('-u', '--username', help='the username authentication.')
each option populates 1 value in
args
, shouldn't try unpack two. try instead:username = args.username
values separated on command line spaces, not commas, use this:
--hostlist 192.168.1.1 192.168.1.2
values on command line have spaces in them split on whitespace , interpreted separate values ("words"). force them interpreted separate words, enclose them in quotes:
--commands 'uname -a' 'whoami'
alternatively, can escape spaces backslashes:
--commands uname\ -a whoami
Comments
Post a Comment