Mar-26-2020, 10:21 AM
Hello,
I am writing a little python script to perform various sysadmin tasks. I have defined the main menu where you pick the choice of task to perform as a function, so I can easily call the main menu again after the specific task that the user picked was performed.
The function looks like this :
Any help is appreciated.
Thanks.
I am writing a little python script to perform various sysadmin tasks. I have defined the main menu where you pick the choice of task to perform as a function, so I can easily call the main menu again after the specific task that the user picked was performed.
The function looks like this :
import getpass import os import subprocess import sys import paramiko print(""" M A I N - M E N U ***************** 1. Run a command on a remote server(s). 2. IP scanning 3. Quit """) def main_menu(): print(""" M A I N - M E N U ***************** 1. Run a command on a remote server(s). 2. IP Scanning 3. Quit """) ## Get input ### choice = input('Enter your choice [1-3] : ') ### Convert string to int type ## choice = int(choice) ### Take action as per selected menu-option ### if choice == 1: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) command = input("Enter a command to run on the remote server(s): ") username = input("Enter your username: ") password = getpass.getpass(prompt='Enter your password: ') while True: servers = input("Enter the full path of your servers file: ") if not os.path.exists(servers): print("The provided file does not exist") continue else: break with open(servers, 'r') as fp: for ip in [line.strip() for line in fp.readlines()]: ssh.connect(ip, 22, username, password) stdin, stdout, stderr = ssh.exec_command(command) print(ip, stdout.readline()) ssh.close() main_menu() elif choice == 2: subnet = input("Enter a subnet to scan (up to the third octet). Example 127.0.0 : ") dot = "." for ping in range(1, 5): address = subnet + dot + str(ping) res = subprocess.call(['ping', '-c', '3', address], stdout=open(os.devnull, 'wb')) if res == 0: print("ping to", address, "OK!") elif res == 2: print("no response from", address) else: print("ping to", address, "FAILED!") main_menu() elif choice == 3: sys.exit()The Problem is that after the user picks a choice and then the code is executed, the function returns only the print function and then the program finishes, so the user is not able to pick another choice, as seen below :
Output: M A I N - M E N U
*****************
1. Run a command on a remote server(s).
2. IP scanning
3. Quit
Enter your choice [1-3] : 2
Enter a subnet to scan (up to the third octet). Example 127.0.0 : 192.168.1
ping to 192.168.1.1 OK!
ping to 192.168.1.2 FAILED!
ping to 192.168.1.3 OK!
ping to 192.168.1.4 OK!
M A I N - M E N U
*****************
1. Run a command on a remote server(s).
2. IP Scanning
3. Quit
Process finished with exit code 0
As I'm a noob I suspect that this may be just an indentation mistake , but I'm not able to find it. I have reformatted with PyCharm, but still...Any help is appreciated.
Thanks.