CiscoTool_v1/netmiko_Api/utils/manualTest/manualTest.py

90 lines
2.9 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': 2022,
# # 'password':
# # 'secret':'',
# }
R1 = {
'device_type':type,
'ip':host,
'username':username,
'password':password,
'port': int(sys.argv[6]),
# 'password': '',
'secret':'',
}
R2 = {
'device_type':"cisco_ios_telnet",
'ip':host,
'username':"",
'password':"",
'port': int(sys.argv[3]),
# 'password': '',
'secret':'',
}
output = ""
check=""
try:
# print(R1)
with ConnectHandler(**R2) as net_connect2:
output += net_connect2.send_command_timing("clear line "+sys.argv[4], strip_prompt=False, strip_command=False, delay_factor=4)
output += net_connect2.send_command_timing("\n", strip_prompt=False, strip_command=False, delay_factor=4)
net_connect2.disconnect()
with ConnectHandler(**R1) as net_connect:
net_connect.enable()
output += net_connect.send_command_timing("no", strip_prompt=False, strip_command=False, delay_factor=4)
output += net_connect.send_command_timing("\n", strip_prompt=False, strip_command=False, delay_factor=4)
for x in "show inv, show ver, show logging, show post, show env all".split(","):
output+="###"+x+"\n"
output += net_connect.send_command(x, read_timeout=120)
if "More" in output:
output += net_connect.send_command_timing(" " , strip_prompt=False, strip_command=False, delay_factor=400, last_read=1.0, read_timeout=60)
# print(output)
print(output)
net_connect.disconnect()
# print(output)
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("Connection Refused xxError\n(1) Connection exists on device\n(2) Your connection is interrupted by other connection") #Connection Refused
except ValueError:
print("Mode xxError")
except ReadTimeout:
print("Time out xxError")
except ConnectionException:
print("Connection xxError")