Skip to content

Commit 607a7b8

Browse files
committed
Add --help section
1 parent 785d052 commit 607a7b8

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

cisco-send.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,52 @@
66
import re
77
import threading
88
import queue
9+
import argparse
910
from datetime import datetime
1011

12+
1113
# Error checking function
1214
def check_error(condition, message):
1315
if not condition:
1416
print(f"Error: {message}")
1517
cleanup()
1618
sys.exit(1)
1719

18-
# Check if the correct number of arguments is provided
19-
check_error(len(sys.argv) == 2, "Usage: $ python3 cisco-send.py <input_file>")
20+
DEFAULT_DEVICE_FILE = '/dev/ttyUSB0'
21+
DEFAULT_DELAY = 0.2
22+
23+
# Command-line argument parsing
24+
parser = argparse.ArgumentParser(description='Send a local config to a device over a serial/console connection.')
25+
26+
# Mandatory argument
27+
parser.add_argument('input_file', type=str, help='Path to the config file for the Cisco device (Router/Switch).')
28+
29+
# Optional arguments
30+
parser.add_argument('--device-file', type=str, default=DEFAULT_DEVICE_FILE, help=f'Path to device file. Default is --device-file {DEFAULT_DEVICE_FILE}')
31+
parser.add_argument('--delay', type=float, default=DEFAULT_DELAY, help=f'Delay between sending lines, in seconds. Default is --delay {DEFAULT_DELAY}s')
32+
33+
# Parse the arguments
34+
args = parser.parse_args()
35+
36+
input_file = args.input_file
37+
device_file = args.device_file
38+
delay_between_lines = args.delay
2039

2140
# Check the input file exists and is readable
22-
input_file = sys.argv[1]
2341
check_error(os.path.isfile(input_file), f"File not found or not readable: {input_file}")
2442

25-
# Configure variables
26-
# Adjust the delay (in seconds)
27-
delay_between_lines = 0.2
28-
2943
# Set variables and serial interface to "8N1"
30-
device_file = /dev/ttyUSB0
3144
baud_rate = 9600
3245
data_bits = 8
3346
parity = serial.PARITY_NONE
3447
stop_bits = serial.STOPBITS_ONE
3548

3649
# Configure the serial port
3750
print("------------")
38-
print(f"Setting serial port baud, parity, and stop bits to '8N1' on {device_file}")
51+
print(f"Setting serial port baud, parity, and stop bits to '8N1' on {args.device_file}")
3952

4053
try:
41-
ser = serial.Serial(device_file, baudrate=baud_rate, bytesize=data_bits, parity=parity, stopbits=stop_bits, rtscts=False, timeout=1)
54+
ser = serial.Serial(args.device_file, baudrate=baud_rate, bytesize=data_bits, parity=parity, stopbits=stop_bits, rtscts=False, timeout=1)
4255
except Exception as e:
4356
check_error(False, f"Failed to set serial port parameters: {str(e)}")
4457

send-GNS3/cisco-send-gns3.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,52 @@
66
import re
77
import threading
88
import queue
9+
import argparse
910
from datetime import datetime
1011

12+
1113
# Error checking function
1214
def check_error(condition, message):
1315
if not condition:
1416
print(f"Error: {message}")
1517
cleanup()
1618
sys.exit(1)
1719

18-
# Check if the correct number of arguments is provided
19-
check_error(len(sys.argv) == 2, "Usage: $ python3 cisco-send.py <input_file>")
20+
DEFAULT_DEVICE_FILE = os.path.expanduser('~/ttyCisco')
21+
DEFAULT_DELAY = 0.2
22+
23+
# Command-line argument parsing
24+
parser = argparse.ArgumentParser(description='Send a local config to a device over a serial/console connection.')
25+
26+
# Mandatory argument
27+
parser.add_argument('input_file', type=str, help='Path to the config file for the Cisco device (Router/Switch).')
28+
29+
# Optional arguments
30+
parser.add_argument('--device-file', type=str, default=DEFAULT_DEVICE_FILE, help=f'Path to device file. Default is --device-file {DEFAULT_DEVICE_FILE}')
31+
parser.add_argument('--delay', type=float, default=DEFAULT_DELAY, help=f'Delay between sending lines, in seconds. Default is {DEFAULT_DELAY}s')
32+
33+
# Parse the arguments
34+
args = parser.parse_args()
35+
36+
input_file = args.input_file
37+
device_file = args.device_file
38+
delay_between_lines = args.delay
2039

2140
# Check the input file exists and is readable
22-
input_file = sys.argv[1]
2341
check_error(os.path.isfile(input_file), f"File not found or not readable: {input_file}")
2442

25-
# Configure variables
26-
# Adjust the delay (in seconds)
27-
delay_between_lines = 0.2
28-
2943
# Set variables and serial interface to "8N1"
30-
device_file = os.path.expanduser('~/ttyCisco')
3144
baud_rate = 9600
3245
data_bits = 8
3346
parity = serial.PARITY_NONE
3447
stop_bits = serial.STOPBITS_ONE
3548

3649
# Configure the serial port
3750
print("------------")
38-
print(f"Setting serial port baud, parity, and stop bits to '8N1' on {device_file}")
51+
print(f"Setting serial port baud, parity, and stop bits to '8N1' on {args.device_file}")
3952

4053
try:
41-
ser = serial.Serial(device_file, baudrate=baud_rate, bytesize=data_bits, parity=parity, stopbits=stop_bits, rtscts=False, timeout=1)
54+
ser = serial.Serial(args.device_file, baudrate=baud_rate, bytesize=data_bits, parity=parity, stopbits=stop_bits, rtscts=False, timeout=1)
4255
except Exception as e:
4356
check_error(False, f"Failed to set serial port parameters: {str(e)}")
4457

0 commit comments

Comments
 (0)