Created
December 4, 2023 22:01
-
-
Save shantoroy/bf49e8bde0c09c5a7ce5cccb6aa8a84d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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