Skip to content

Instantly share code, notes, and snippets.

@shantoroy
Created December 4, 2023 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shantoroy/bf49e8bde0c09c5a7ce5cccb6aa8a84d to your computer and use it in GitHub Desktop.
Save shantoroy/bf49e8bde0c09c5a7ce5cccb6aa8a84d to your computer and use it in GitHub Desktop.
import getpass
import paramiko
import time
def execute_command_ssh(olt_ip, username, password):
try:
# Create an SSH client instance
ssh_client = paramiko.SSHClient()
# Automatically add the server's host key
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the OLT
ssh_client.connect(olt_ip, username=username, password=password, timeout=10)
# Open an interactive shell session
ssh_shell = ssh_client.invoke_shell()
# Send the command
command = "ifconfig\n"
ssh_shell.send(command)
# execute command
# stdin, stdout, stderr = ssh_client.exec_command('ls -l')
# print(stdin,stdout,stderr)
while not ssh_shell.recv_ready():
time.sleep(3)
# Wait for the command output
out = ssh_shell.recv(65535)
output = out.decode('utf-8')
# Print the output
print(output)
# Close the SSH connection
ssh_client.close()
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__=="__main__":
# Input credentials
host = '192.168.0.250'
username = 'your_username'
password = getpass.getpass(prompt='Enter password: ')
# Call the function to collect data using SSH
execute_command_ssh(host, username, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment