|
6 | 6 | import re |
7 | 7 | import threading |
8 | 8 | import queue |
| 9 | +import argparse |
9 | 10 | from datetime import datetime |
10 | 11 |
|
| 12 | + |
11 | 13 | # Error checking function |
12 | 14 | def check_error(condition, message): |
13 | 15 | if not condition: |
14 | 16 | print(f"Error: {message}") |
15 | 17 | cleanup() |
16 | 18 | sys.exit(1) |
17 | 19 |
|
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 |
20 | 39 |
|
21 | 40 | # Check the input file exists and is readable |
22 | | -input_file = sys.argv[1] |
23 | 41 | check_error(os.path.isfile(input_file), f"File not found or not readable: {input_file}") |
24 | 42 |
|
25 | | -# Configure variables |
26 | | -# Adjust the delay (in seconds) |
27 | | -delay_between_lines = 0.2 |
28 | | - |
29 | 43 | # Set variables and serial interface to "8N1" |
30 | | -device_file = os.path.expanduser('~/ttyCisco') |
31 | 44 | baud_rate = 9600 |
32 | 45 | data_bits = 8 |
33 | 46 | parity = serial.PARITY_NONE |
34 | 47 | stop_bits = serial.STOPBITS_ONE |
35 | 48 |
|
36 | 49 | # Configure the serial port |
37 | 50 | 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}") |
39 | 52 |
|
40 | 53 | 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) |
42 | 55 | except Exception as e: |
43 | 56 | check_error(False, f"Failed to set serial port parameters: {str(e)}") |
44 | 57 |
|
|
0 commit comments