Beginner with python here. I’ve been doing some online tutorials and can’t seem to find the solution to this question. What I’d like to do is this:
hostname = raw_input("Type host name here: ")
Then the user inputs as many host names as they like, type done, then what they entered becomes variables for use in the script. So if they typed HOST1, HOST2, HOST3, done then the script would run commands for each entry. Example:
def myhostsbecomevariables(*args):
arg1, arg2, arg3, etc etc etc = args
print "arg1: %r, arg2: %r, etc: %r, etc: %r" % (arg1, arg2, etc etc)
myhostsbecomevariables(HOST1, HOST2, HOST3)
If the user types in 3 host names then myhostbecomesvariables uses 3 arguments. If they had typed 5 host names then it would have 5 arguments.
raw_inputreturns a single string. You can split that string on a delimiter if you wish:Or split onto 2 lines:
And you could pass this to
myhostbecomevariablesusing argument unpacking:where your function could be defined like this:
There really is no need to unpack the hosts into constituent parts here — Since (I assume) you’ll be performing the same action for each host, just do it in a loop.