4 Replies - 1870 Views - Last Post: 14 July 2008 - 07:28 AM Rate Topic: -----

#1 cronos4d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-May 08

using sys.argv to execute on a numeric range.

Posted 30 June 2008 - 06:44 PM

I have the following code
if sys.argv[4] == 'state':
	try:

		print(sys.argv[5])

	except:

		print "Error gathering server state ", sys.argv[5]


I invoke the python program with myscript.py option1, 2, 3, etc

The fifth argument is a server with a numeric range:
server1,server2,server3, etc.

Instead of wrapping the script, I would like to be able to feed a range to the script instead.
For example, right now all I can do is myscript.py server1, myscript.py server2

I would like to be able to pass myscript.py server1-20 and it would execute the command on all 20 servers.

Any suggestions are appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: using sys.argv to execute on a numeric range.

#2 linuxunil  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 46
  • Joined: 07-March 06

Re: using sys.argv to execute on a numeric range.

Posted 01 July 2008 - 07:33 AM

It would probably be easier and more useful to just put the servers in a configuration file and load it. This would keep you from having to type the server over and over again. You could even use a YAML file and declare groups of servers. Take a look at PyYaml. YAML is much easier to read and write than XML.
Was This Post Helpful? 0
  • +
  • -

#3 cronos4d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-May 08

Re: using sys.argv to execute on a numeric range.

Posted 01 July 2008 - 12:42 PM

View Postlinuxunil, on 1 Jul, 2008 - 07:33 AM, said:

It would probably be easier and more useful to just put the servers in a configuration file and load it. This would keep you from having to type the server over and over again. You could even use a YAML file and declare groups of servers. Take a look at PyYaml. YAML is much easier to read and write than XML.


I agree that this might be easier; however, I do not always want to execute commands against all servers in the config file. Hence the need for a range.
Was This Post Helpful? 0
  • +
  • -

#4 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: using sys.argv to execute on a numeric range.

Posted 12 July 2008 - 08:22 AM

View Postcronos4d, on 30 Jun, 2008 - 06:44 PM, said:

I have the following code
if sys.argv[4] == 'state':
	try:

		print(sys.argv[5])

	except:

		print "Error gathering server state ", sys.argv[5]


I invoke the python program with myscript.py option1, 2, 3, etc

The fifth argument is a server with a numeric range:
server1,server2,server3, etc.

Instead of wrapping the script, I would like to be able to feed a range to the script instead.
For example, right now all I can do is myscript.py server1, myscript.py server2

I would like to be able to pass myscript.py server1-20 and it would execute the command on all 20 servers.

Any suggestions are appreciated.


Use ZSH. :)
Actually, bash supports this syntax also.

myscript.py server{1..20}

Expands to:
myscript.py server1 server2 server3 ... server20

Then simply process the argument list. If you have other arguments besides servers that get passed to your program, then make them options (as in, begin with one or two dashes... --long-option). You can also implement an option terminator option (just two dashes, --) to indicate that the rest of the arguments in the list are not options.
Was This Post Helpful? 0
  • +
  • -

#5 linuxunil  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 46
  • Joined: 07-March 06

Re: using sys.argv to execute on a numeric range.

Posted 14 July 2008 - 07:28 AM

You may be better off with optparse, it provides more flexibility in setting up you option parsing. Reference Guide
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1