برای پیکربندی خودکار روتر سیسکو به زبان پایتون، میتوانید از کتابخانههایی مانند netmiko یا paramiko برای دسترسی و اجرای دستورات به صورت خودکار استفاده کنید. در ادامه اسکریپتی به زبان پایتون ارائه میدهم که با استفاده از کتابخانه netmiko، به روتر سیسکو متصل شده و دستورات پیکربندی را اجرا میکند.
نصب کتابخانه netmiko
pip install netmiko
اسکریپت پایتون:
from netmiko import ConnectHandler
** مشخصات روتر
cisco_router = {
‘device_type’: ‘cisco_ios’,
‘host’: ‘Router-IP’, # Replace with the IP address of your router
‘username’: ‘admin’, # Replace with your router username
‘password’: ‘password’, # Replace with your router password
‘secret’: ‘secret’, # Enable password (if any)
}
** اتصال به روتر
def connect_to_router(router):
connection = ConnectHandler(**router)
connection.enable() # Enter enable mode
return connection
def execute_commands(connection):
show_commands = [
"show inventory",
"show voice port summary",
"show controllers e1",
"show isdn status",
"show dial-peer voice summary",
"show dialplan number 6556",
"test voice translation-rule 1 6556"
]
for command in show_commands:
print(f"Running command: {command}")
output = connection.send_command(command)
print(output)
** کامند های پیکربندی
config_commands = [
"interface Serial0/0/0:15",
"no ip address",
"encapsulation hdlc",
"isdn switch-type primary-net5",
"isdn overlap-receiving T302 3000",
"isdn protocol-emulate network",
"isdn incoming-voice voice",
"no cdp enable",
"exit"
]
e1_config_commands = [
"card type e1 0 3", # slot 0, subslot 3
"network-clock-participate wic 3",
"isdn switch-type primary-net5",
"controller e1 0/3/0",
"pri-group timeslot 1-31",
"framing no-crc4",
"exit"
]
voice_config_commands = [
"voice service voip",
"allow-connections h323 to h323",
"allow-connections h323 to sip",
"allow-connections sip to h323",
"allow-connections sip to sip",
"sip",
'rel1xx supported "44"',
"min-se 300",
"exit",
"voice class codec 1",
"codec preference 1 g711ulaw",
"codec preference 2 g711alaw",
"codec preference 3 g729r8",
"exit",
"dial-peer voice 1 voip",
"destination-pattern .T",
"session protocol sipv2",
"session target ipv4:Server-IP",
"incoming called-number Line_Number",
"voice-class codec 1",
"dtmf-relay rtp-nte",
"exit",
"dial-peer voice 10 pots",
"destination-pattern Line_Number",
"direct-inward-dial",
"port 0/0/0:15",
"forward-digits 3",
"exit",
"dial-peer voice 20 pots",
"destination-pattern 1..", # Panasonic extension range
"port 0/0/0:15",
"forward-digits all",
"exit"
]
print("Applying configuration commands...")
connection.send_config_set(config_commands)
connection.send_config_set(e1_config_commands)
connection.send_config_set(voice_config_commands)
print("Verifying configuration with 'show run'...")
output = connection.send_command("show run")
print(output)
if name == “main”:
connection = connect_to_router(cisco_router)
execute_commands(connection)
connection.disconnect()
print("Configuration complete!")