Python Tutorial: sys module for network engineers

  Рет қаралды 124

Ferds Tech Channel

Ferds Tech Channel

Күн бұрын

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

Пікірлер
Python Tutorial: psycopg2 module for beginners (Part 1) - easy!
10:01
Ferds Tech Channel
Рет қаралды 38
Become a bash scripting pro - full course
36:00
CODE IS EVERYTHING
Рет қаралды 59 М.
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 45 МЛН
Synyptas 4 | Жігіттер сынып қалды| 3 Bolim
19:27
Argparse Tutorial - Python  2023 (Creating Your First CLI)
18:59
Why More People Dont Use Linux
18:51
ThePrimeTime
Рет қаралды 255 М.
Coding Was HARD Until I Learned These 5 Things...
8:34
Elsa Scola
Рет қаралды 525 М.
UUID vs INT: What’s Better For Your Primary Key?
9:40
Database Star
Рет қаралды 50 М.
Using docker in unusual ways
12:58
Dreams of Code
Рет қаралды 454 М.
Professional CLI Applications with Click
24:02
NeuralNine
Рет қаралды 59 М.
How to Check if a User Exists Among Billions! - 4 MUST Know Strategies
12:44
I'm out. Leaving tech behind forever.
11:24
Daniel
Рет қаралды 80 М.
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 45 МЛН