0

I am newer to python and believe there is a simple answer to this but can't seem to figure it out. Is there a way to do a function call at the beginning of this program. As you can see I am running the code and then asking to run it again, this is where it does the function call right now. This works as is, but I would like to get rid of everything but the while loop and function so I can clean up the code. Thanks.


import subprocess import re print ("This will print any service that is stopped or unknown \n") #servers server = str(input("Enter server to query(IP Address): ")) server_list = server.split(",") server_count = len(server_list) #services service = str(input("Enter service to query: ")) service_list = service.split(",") service_count = len(service_list) #server loop for a in range(len(server_list)): server_net_use = 'net use'+r' \\'+server_list[a]+r'\ipc$ /user:Administrator itv' server_disconnect = 'net use'+r' \\'+server_list[a]+r'\ipc$ /d' server_sc = r'\\'+server_list[a] subprocess.check_output(server_net_use, universal_newlines=True) #service loop for i in range(len(service_list)): x = subprocess.check_output(['sc', server_sc, 'query', service_list[i]], universal_newlines=True) y = re.sub('\s+',' ',x) if 'FAILED' in y: print ('\n'+server_list[a]+' - '+service_list[i]+' - Unknown Service!') elif 'STOPPED' in y: print ('\n'+server_list[a]+' - '+service_list[i]+' - '+y.split(" ")[10]) def repeat(): #servers server = str(input("Enter server to query(IP Address): ")) server_list = server.split(",") server_count = len(server_list) #services service = str(input("Enter service to query: ")) service_list = service.split(",") service_count = len(service_list) #server loop for a in range(len(server_list)): server_net_use = 'net use'+r' \\'+server_list[a]+r'\ipc$ /user:Administrator itv' server_disconnect = 'net use'+r' \\'+server_list[a]+r'\ipc$ /d' server_sc = r'\\'+server_list[a] subprocess.check_output(server_net_use, universal_newlines=True) #service loop for i in range(len(service_list)): x = subprocess.check_output(['sc', server_sc, 'query', service_list[i]], universal_newlines=True) y = re.sub('\s+',' ',x) if 'FAILED' in y: print ('\n'+server_list[a]+' - '+service_list[i]+' - Unknown Service!\n') elif 'STOPPED' in y: print ('\n'+server_list[a]+' - '+service_list[i]+' - '+y.split(" ")[10]+'\n') while(True): a = str(input("Do you want to query another service? y/n: ")) if 'y' in a: subprocess.check_output(server_disconnect, universal_newlines=True) print ("\n") repeat() elif 'n' in a: subprocess.check_output(server_disconnect, universal_newlines=True) break; else: print ('Enter y/n') 
0

2 Answers 2

3

This is how:

import subprocess import re def server_checker(): print ("This will print any service that is stopped or unknown \n") #servers server = str(input("Enter server to query(IP Address): ")) server_list = server.split(",") server_count = len(server_list) #services service = str(input("Enter service to query: ")) service_list = service.split(",") service_count = len(service_list) #server loop for a in range(len(server_list)): server_net_use = 'net use'+r' \\'+server_list[a]+r'\ipc$ /user:Administrator itv' server_disconnect = 'net use'+r' \\'+server_list[a]+r'\ipc$ /d' server_sc = r'\\'+server_list[a] subprocess.check_output(server_net_use, universal_newlines=True) #service loop for i in range(len(service_list)): x = subprocess.check_output(['sc', server_sc, 'query', service_list[i]], universal_newlines=True) y = re.sub('\s+',' ',x) if 'FAILED' in y: print ('\n'+server_list[a]+' - '+service_list[i]+' - Unknown Service!\n') elif 'STOPPED' in y: print ('\n'+server_list[a]+' - '+service_list[i]+' - '+y.split(" ")[10]+'\n') if __name__ == "__main__": server_checker() while(True): answer = str(input("Do you want to query another service? y/n: ")) if answer.lower() in ["yes","y","letsgo"]: subprocess.check_output(server_disconnect, universal_newlines=True) print ("\n") server_checker() elif answer.lower() in ["no","n,","nowayhosay"]: subprocess.check_output(server_disconnect, universal_newlines=True) break; else: print ('Enter y/n') 
Sign up to request clarification or add additional context in comments.

5 Comments

There still seems to be quite a lot of repeated code here. I'd suggest that the repeat() function is unnecessary and can be omitted. Further, it seems to me that the while loop shouldn't be in service_checker() (it is currently calling service_checker() recursively) but should be in the main program at the end of the file.
fair point. I guess I got carried away with answering the direct question, I'll tidy up the code later but my main intention was to answer the question and give an idea of how to do it correctly not rewrite 80 lines of code.
@Simon you were 100% right didn't even look at the code correctly.
Thanks @Noelkd for the help, I was not aware of the if __name__ == "__main__": command. this will be helpful in the future as well and saved a lot of lines of code. Works great!
@Tim no problem my friend. If this is the right answer can you accept it? I'm fairly sure as well looking at the code that this would work without the if __name__ == "__main__": bit, you just needed to call the method and then drop into the loop until exit. Good luck in the future!
0

Its actually pretty easy. Just define the function as soon as possible, then call it right after its been defined.

def saysomething(say): print say saysomething("Hello, World!") 

If everything goes well, you should get:

Hello, World! 

If not, check for errors.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.