Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Manipulation for a ssy admin task 1

Status
Not open for further replies.

blacknred

Technical User
Nov 24, 2010
12
IN
Hi
I just started with python, and trying to implement some system admin tasks and running into some rough.
Basically I need to do some task on whichever share exists on their corresponding server.
eg
Server A has shares 1,2 and 3
Server B has share 1 and so on.

Any help is appreciated :)
pseudocode

while not Done:
Prompt for IP
while not Flag
prompt for shares
Code:
def get_config():
	config_list = []
	done = False
	deny = 'n'
	counter = 1
	while not done:
		config_list, counter = []
		print " Type n or N to exit "
		ip = raw_input("Enter IP: ")
		print "you entered ", ip
		if ip.lower() == deny.lower():
			done = True
			flag = True
		else:
			config_list.append(ip)
			flag = False
		while not flag:
			share = raw_input("Enter Shares : ")
			if share.lower() == deny.lower():
				flag = True
				
			else:
				config_list.append(share)
	
	print "Config File Reads  ", config_list

This works & after entering the values,I get an array like
[ 192.168.0.12 , Share1, Share2, 192.168.0.22, Share 1, 192.168.0.14, Share5, Share6, Share9 ]

Now for each IP, I need to do some action the shares on that server.
as in
for ip 192.168.0.12,
do task a on Share1,Share2
for ip 192.168.0.22,
do task a on Share1
and so on.....

I'm struggling how to implement the above. Any thoughts?
 
Instead of list
Code:
>>> config_list = [ "192.168.0.12" , "Share1", "Share2", "192.168.0.22", "Share 1", "192.168.0.14", "Share5", "Share6", "Share9" ]
use hash (aka dictionary)
Code:
>>> config_dict = { "192.168.0.12" :["Share1", "Share2"], "192.168.0.22":[ "Share 1"], "192.168.0.14":[ "Share5", "Share6", "Share9" ]}
then you can process dictionary keys and values
Code:
>>> config_dict.keys()
['192.168.0.14', '192.168.0.12', '192.168.0.22']
>>> config_dict.values()
[['Share5', 'Share6', 'Share9'], ['Share1', 'Share2'], ['Share 1']]

 
They you can do something like this:
Code:
>>> for ip in config_dict.keys():
... 	print "%s -> %s" % (ip, config_dict[ip])
... 	# processing shares for the IP: 
... 	print "Processing shares for IP = %s" % ip
... 	for share in config_dict[ip]: 
... 	    print "... processing: %s" % share
... 	print
... 	
192.168.0.14 -> ['Share5', 'Share6', 'Share9']
Processing shares for IP = 192.168.0.14
... processing: Share5
... processing: Share6
... processing: Share9

192.168.0.12 -> ['Share1', 'Share2']
Processing shares for IP = 192.168.0.12
... processing: Share1
... processing: Share2

192.168.0.22 -> ['Share 1']
Processing shares for IP = 192.168.0.22
... processing: Share 1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top