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.

  1. the long option should specified 2 hyphens:

    parser.add_argument('-u', '--username', help='the username authentication.') 
  2. each option populates 1 value in args, shouldn't try unpack two. try instead:

    username = args.username 
  3. values separated on command line spaces, not commas, use this:

    --hostlist 192.168.1.1 192.168.1.2 
  4. 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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -