Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

import paramiko

import getpass

def connect_ssh(hostname, username, password):

ssh_client = paramiko.SSHClient()

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh_client.connect(hostname, username=username, password=password)

return ssh_client

def execute_command(ssh_client, command):

stdin, stdout, stderr = ssh_client.exec_command(command)

output = stdout.read().decode()

error = stderr.read().decode()

if error:

print(f"Error: {error}")

else:

print(output)

def main():

hostname = input("Enter the hostname or IP address: ")

username = input("Enter your username: ")

password = getpass.getpass("Enter your password: ")

ssh_client = connect_ssh(hostname, username, password)

while True:

print("\n1. Start device\n2. Stop device\n3. Restart device\n4. Exit")

choice = input("Enter your choice: ")

if choice == "1":

execute_command(ssh_client, "start_command_here")
elif choice == "2":

execute_command(ssh_client, "stop_command_here")

elif choice == "3":

execute_command(ssh_client, "restart_command_here")

elif choice == "4":

break

else:

print("Invalid choice. Please enter a valid option.")

ssh_client.close()

if __name__ == "__main__":

main()

You might also like