-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvps_show.py
69 lines (55 loc) · 2.17 KB
/
vps_show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/local/Python_envs/Python3/bin/python3
import paramiko
import time
from getpass import getpass
import re
version_pattern = re.compile(r'Cisco .+ Software, Version (\S+)')
model_pattern = re.compile(r'cisco (\S+).+bytes of memory\.')
serial_no_pattern = re.compile(r'Processor board ID (\S+)')
uptime_pattern = re.compile(r'(.+) uptime is (.*)')
host = "ip"
username = "username"
password = "password"
lab_csr = {
'host': host,
'username': username,
'password': password
}
devnet_csr = {
'host': lab_csr['host'],
'username': lab_csr['username'],
'password': lab_csr['password']
}
def cisco_parse_version(host, username, password):
try:
print(f"\n{'#' * 55}\nConnecting to the Device {host}\n{'#' * 55} ")
SESSION = paramiko.SSHClient()
SESSION.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SESSION.connect(host, port=22,
username=username,
password=password,
look_for_keys=False,
allow_agent=False)
DEVICE_ACCESS = SESSION.invoke_shell()
DEVICE_ACCESS.send(b'term length 0\n')
DEVICE_ACCESS.send(b'show ver\n')
time.sleep(1)
output = (DEVICE_ACCESS.recv(65000).decode('ascii'))
version_match = version_pattern.search(output)
print('IOS Version'.ljust(18)+': '+version_match.group(1))
model_match = model_pattern.search(output)
print('Model '.ljust(18)+': '+model_match.group(1))
serial_no_match = serial_no_pattern.search(output)
print('Serial Number '.ljust(18)+': '+serial_no_match.group(1))
uptime_match = uptime_pattern.search(output)
print('Host Name '.ljust(18)+': '+uptime_match.group(1))
print('Device Uptime '.ljust(18)+': '+uptime_match.group(2))
print(f"\n{'#' * 55}\nFinished Executing Script\n{'#' * 55} ")
SESSION.close()
except paramiko.ssh_exception.AuthenticationException:
print("Authentication Failed")
except AttributeError:
print("Parsing Error, Please check the command")
except:
print("Can not connect to Device")
cisco_parse_version(**devnet_csr)