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

def get_config():
	config_list = []
	done = False
	deny = 'n'
	counter = 1
	while not done:
		
		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?

1.You have on line 2 assigned config_list and on line 7 again????
2.You exit logic is no where to be found
3.Why are you checking ip with a string (if ip.lower()==deny.lower())???

ip is a series of numbers so there is no lower method

Besides the logic behind your code i dont get it.

Please explain very well what you intend to do.

;)

config_list, counter = [] wasn't supposed to be on line 7, that was some cut/paste error while i was playith with the code snippet. Thanks for pointing it out. Removed it.
3.Why are you checking ip with a string (if ip.lower()==deny.lower())???
its for the print " Type n or N to exit " on line 8.

$ python launcher.py
Type n or N to exit
Enter IP: 192.168.0.12
you entered 192.168.0.12
Enter Shares : Share1
Enter Shares : Share2
Enter Shares : Share3
Enter Shares : n
Type n or N to exit
Enter IP: 192.168.0.22
you entered 192.168.0.22
Enter Shares : Share1
Enter Shares : n
Type n or N to exit
Enter IP: 192.168.0.14
you entered 192.168.0.14
Enter Shares : Share4
Enter Shares : Share5
Enter Shares : n
Type n or N to exit
Enter IP: n
you entered n
Config File Reads ['192.168.0.12', 'Share1', 'Share2', 'Share3', '192.168.0.22
', 'Share1', '192.168.0.14', 'Share4', 'Share5']

Now I wish do do a command like
cp myfile 192.168.0.12/Share1
cp myfile 192.168.0.12/Share2
cp myfile 192.168.0.12/Share3

cp myfile 192.168.0.22/Share1

cp myfile 192.168.0.14/Share4
cp myfile 192.168.0.14/Share5

cp just for an example, reality it could be some other tasks, but thats the intention

It should be easier if you use a dictionary pointing to a list of shares. This code assumes that you do not care about order of execution, otherwise you will have to order the dictionary keys and execute in that order.

def get_config():
	ip_dict = {}
	done = False
	deny = 'n'
	counter = 1
	while not done:
 
		print " Type n or N to exit "
		ip = raw_input("Enter IP: ")
		print "you entered ", ip
                ip=ip.lower()
		if ip == 'n':
			done = True
			flag = True
		else:
			ip_dict[ip] = []
			flag = False
                        ## indent while() loop to this level
                        ## problems with tabs-you can set an IDE to convert to spaces
        	      	while not flag:
	        		share = raw_input("Enter Shares : ")
		        	if share.lower() == deny.lower():
			        	flag = True
 
        			else:
	        			ip_dict[ip].append(share)
        for ip in ip_dict:
	    print ip
            for share in ip_dict[ip]:
                print "     ", share

Thanks , that works a charm.

I'm trying to use it as module though.
So that I can have the actual tasks seperate and we can scale it up better for future.

worker.py

import launcher
import trial

if len(sys.argv) != 2: 
        print 'Usage: worker.py <no_of_runs>' 
        sys.exit(1) 

no_of_runs = int(sys.argv[1])

launcher.get_config()   // this should get me the IPs and their respective shares
trial.randomtask(2)

trial.py

import sys 
import os

def randomtask(n):
	
	for i in range(n):
		test_file = "//%s/%s/man%i" % (ip, share, i)	
		try:
			print "%s" % test_file	//all i am checking here is if its able to pull the values for IP & Share
		except (KeyboardInterrupt)
			print "problems"
			break

when i run
python worker.py 2, i get

test_file = "//%s/%s/man%i" % (ip, share, i)
NameError: global name 'ip' is not defined

I suggest doing a class for your module with normal methods with return values.

I am getting your idea.

why not like.....

1. The system ask for cmd (1.to add ip, 2. to exit)
2. User makes a choice. if choice 1.
3.The user get cmd. Please add ip.....

4.After ip added and enter..
5. Another choice. 1. to add shares to the entered ip, 2. to enter new ip , 3. to quit.

Hope you get the idea. It will be full interaction with the user and also easy for you to process the code.

note. IP's and Shares are packed into dict with ip as keys and shares as values.
That is IP(key) can have as many values (shares) as possible.

;)

Thanks for the idea.
Well, I'm thinking of a module approach because other people in my team can use the same.
so i got launcher.py which gets the IPs & Shares, trial.py will contain all tasks that i need to do ( can have class within that no problem ) and
worker.py is where i do what i need to. So basically i can pass the trial.py & launcher.py and anyone can write their own 'worker code' do get their tasks done. I'm aware this is trivial stuff at the moment, but guess it will prove useful as number of tasks increase.
As it stands:
launcher.py

import sys 
import os

def get_config():
	ip_dict = {}
	done = False
	deny = 'n'
	while not done:
 
		print " Type n or N to exit "
		ip = raw_input("Enter IP: ")
		#print "you entered ", ip
                ip=ip.lower()
		if ip == 'n':
			done = True
			flag = True
		else:
			ip_dict[ip] = []
			flag = False
		while not flag:
			share = raw_input("Enter Shares : ")
			if share.lower() == deny.lower():
				flag = True
 
			else:
				ip_dict[ip].append(share)
	return ip_dict

trial.py

import sys 
import os

def randomtask(n):
	
	for i in range(n):
		test_file = "//%s/%s/man%i" % (ip, share, i)	
		try:
			print "%s" % test_file	//all i am checking here is if its able to pull the values for IP & Share
		except (KeyboardInterrupt)
			print "problems"
			break

worker.py

import launcher
import trial

if len(sys.argv) != 2: 
        print 'Usage: worker.py <no_of_runs>' 
        sys.exit(1) 

no_of_runs = int(sys.argv[1])

launcher.get_config()   // this should get me the IPs and their respective shares
trial.randomtask(2)

but i get the error

NameError: global name 'ip' is not defined

which means it's not being able to make use of the ip,share values returned, or maybe they are not being returned correctly... :(

Put your idea into a class. You see you are importing a basic file which has only function not a class. That means python will not be able to import that.

secondly you when you import trial.

To use any variable or method in trial.... do

trial.ip
trial.foo
trial.get_config.ip

With this .. python will get it for you.

Hope you get the idea ;)

launcher.get_config() // this should get me the IPs and their respective shares
trial.randomtask(2)

It gets the ip's and then destroys the dictionary because you don't store the return. You would then pass the returned dictionary, or an individual ip with shares, to the randomtask function.

It gets the ip's and then destroys the dictionary because you don't store the return. You would then pass the returned dictionary, or an individual ip with shares, to the randomtask function.

Makes sense, but how do i get around it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.