CiscoTool_v1/netmiko_Api/utils/turnOnPort.py

156 lines
6.1 KiB
Python

from netmiko import ConnectHandler, NetmikoAuthenticationException, ReadTimeout, ConnectionException, NetMikoTimeoutException
from getpass import getpass
from pprint import pprint
import sys
import socket
# type = sys.argv[1]
# ip = sys.argv[2]
# username = sys.argv[3]
# password = sys.argv[4]
# device_type= (ssh | telnet) cisco_ios | cisco_ios_telnet
# ip
# host
# username
# password= string | getpass() #Yeu cau nhap password
# port
#secret
type = "cisco_ios_telnet"
host = sys.argv[2]
username = ""
password = ""
# R1 = {
# 'device_type':"cisco_ios_telnet",
# 'ip':"172.16.20.17",
# 'username':"",
# 'password':"",
# 'port': 2020,
# # 'password':
# # 'secret':'',
# }
R1 = {
'device_type':type,
'ip':host,
'username':username,
'password':password,
'port': int(sys.argv[3]),
# 'password': '',
'secret':'',
}
R2 = {
'device_type':type,
'ip':host,
'username':username,
'password':password,
'port': int(sys.argv[5]),
# 'password': '',
'secret':'',
}
output = ""
check=""
list = []
list1 = []
list2 = []
# list.remove("2")
# print(list)
def convert_interface_ranges(interfaces):
array_interface = []
other_interface = []
result = []
for x in interfaces.split("\n"):
if ("up" in x or "down" in x) and x.split(" ")[0]!="" and "Embedded" not in x:
if "Giga" in x or "Fast" in x:
array_interface.append(x.split(" ")[0])
else:
other_interface.append(x.split(" ")[0])
if "ASA" in sys.argv[6]:
return array_interface+other_interface
else:
shorthand_ranges = {}
for interface in array_interface:
if len(interface.split("/"))==2:
if interface.split("/")[0] not in shorthand_ranges:
shorthand_ranges[interface.split("/")[0]] = [interface]
else:
shorthand_ranges[interface.split("/")[0]].append(interface)
else:
if len(interface.split("/"))==3:
if interface.split("/")[0]+"/"+interface.split("/")[1] not in shorthand_ranges:
shorthand_ranges[interface.split("/")[0]+"/"+interface.split("/")[1]] = [interface]
else:
shorthand_ranges[interface.split("/")[0]+"/"+interface.split("/")[1]].append(interface)
else:
if len(interface.split("/"))==4:
if interface.split("/")[0]+"/"+interface.split("/")[1]+"/"+interface.split("/")[2] not in shorthand_ranges:
shorthand_ranges[interface.split("/")[0]+"/"+interface.split("/")[1]+"/"+interface.split("/")[2]] = [interface]
else:
shorthand_ranges[interface.split("/")[0]+"/"+interface.split("/")[1]+"/"+interface.split("/")[2]].append(interface)
for x in shorthand_ranges:
if len(shorthand_ranges[x]) > 2:
result.append(shorthand_ranges[x][0]+"-"+shorthand_ranges[x][-1].split("/")[len(shorthand_ranges[x][-1].split("/"))-1])
else:
if len(shorthand_ranges[x]) == 2:
result.append(shorthand_ranges[x][0])
result.append(shorthand_ranges[x][1])
else:
result.append(shorthand_ranges[x][0])
return result+other_interface
def startPort(list, output, list1, check):
try:
with ConnectHandler(**R2) as net_connect1:
net_connect1.enable()
net_connect1.send_command_timing("clear line " + sys.argv[1], strip_prompt=False, strip_command=False, delay_factor=4)
net_connect1.send_command("\n", delay_factor=4, expect_string=r"#")
net_connect1.disconnect()
with ConnectHandler(**R1) as net_connect:
net_connect.enable()
if check == "":
check += net_connect.send_command( "show ip interface brief",read_timeout=300)
check += net_connect.send_command( "show interface ip brief",read_timeout=300)
output += check
list = convert_interface_ranges(check)
for x in list:
if x not in list1:
if "-" in x and ("Fa" in x or "Gi" in x):
output += net_connect.send_config_set([
"interface range "+ x,
"no shut"
], strip_prompt=False, strip_command=False, delay_factor=400,read_timeout=3)
list1.append(x)
else:
output += net_connect.send_config_set([
"interface "+ x,
"no shut"
], strip_prompt=False, strip_command=False, delay_factor=400,read_timeout=3)
list1.append(x)
net_connect.disconnect()
print(output+"\nFinish")
except NetmikoAuthenticationException:
print("Netmiko Authentication xxError\n(1) Can not connect to device\n(2) Can not load IOS image (switch:, rommon)")
except NetMikoTimeoutException:
print("NetMikoTimeoutException xxError")
except ConnectionRefusedError as err:
print("1Connection Refused xxError\n(1) Connection exists on device\n(2) Your connection is interrupted by") #Connection Refused
except ValueError:
print("Mode xxError")
except ReadTimeout:
output = "TIME OUT\n"+output
startPort(list1, output, list1, check)
except ConnectionException:
print("Connection xxError")
startPort(list, output, list1, check)