In this video, I will discuss the sys module in python.
import sys # Import the sys module for accessing command-line arguments
from netmiko import ConnectHandler # Import ConnectHandler from netmiko library for SSH connection
print(type(sys.argv))
Check if the correct number of command-line arguments is provided
if len(sys.argv) != 4:
print("Usage: script.py device_ip username password") # Print usage instructions if arguments are incorrect
sys.exit(1) # Exit the script with a non-zero status indicating an error
Assign command-line arguments to variables
device_ip = sys.argv[1] # IP address of the device to connect to
username = sys.argv[2] # Username for SSH authentication
password = sys.argv[3] # Password for SSH authentication
try:
Attempt to establish an SSH connection to the device
ssh_session = ConnectHandler(
device_type='cisco_ios', # Specify the device type (Cisco IOS in this case)
ip=device_ip, # Provide the IP address of the device
username=username, # Provide the SSH username
password=password # Provide the SSH password
)
Send a command to the device and capture the output
output = ssh_session.send_command('show version')
print(output) # Print the command output to the console
ssh_session.disconnect() # Disconnect the SSH session after executing the command
except Exception as e:
print(f"Error: {e}") # Print any exception/error that occurs during the SSH connection or command execution
sys.exit(1) # Exit the script with a non-zero status if there is an error
#pythonforbeginners #pythontutorial #python
On this page of the site you can watch the video online Python Tutorial: sys module for network engineers with a duration of hours minute second in good quality, which was uploaded by the user Ferds Tech Channel 08 July 2024, share the link with friends and acquaintances, this video has already been watched 543 times on youtube and it was liked by 9 viewers. Enjoy your viewing!