-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparamiko_show_output_python2.py
More file actions
55 lines (47 loc) · 2.19 KB
/
paramiko_show_output_python2.py
File metadata and controls
55 lines (47 loc) · 2.19 KB
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
#!/usr/bin/env python2
import paramiko
import os
import csv
import re
import time
from datetime import datetime
from itertools import chain
from getpass import getpass
ip = raw_input("Please enter your IP address: ")
username = raw_input("Please enter your username: ")
vpn_peer = raw_input("Please enter remote peer IP address: ")
password = getpass()
remote_conn_pre=paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote_conn_pre.connect(ip, port=22, username=username,
password=password,
look_for_keys=False, allow_agent=False)
remote_conn = remote_conn_pre.invoke_shell()
output = remote_conn.recv(65535)
print output
remote_conn.send("terminal length 0\n")
time.sleep(.5)
remote_conn.send("show crypto ipsec sa peer " + vpn_peer + "\n")
time.sleep(.5)
ipsec_sa_output = remote_conn.recv(65535)
print ipsec_sa_output
ipsec_sa_csv = os.path.join(os.getcwd(), ip + str(datetime.now().strftime('_%Y%m%d%H%M%S')) + '_show_ipsec.csv')
with open(ipsec_sa_csv, 'wb') as f:
header = ['VRF', 'Local-Endpt', 'Remote-Endpt', 'Local-Ident', 'Remote-Ident', 'Encryt', 'Decryt', 'Outbnd-SPI']
ipsec_conns = ipsec_sa_output.split('protected')
del ipsec_conns[0]
conns_list = []
for conn in ipsec_conns:
ident = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d+/\d+", conn, re.MULTILINE)
local_endpt = re.findall(r"(?<=local crypto endpt.: )\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", conn, re.MULTILINE)
remote_endpt = re.findall(r"(?<=remote crypto endpt.: )\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", conn, re.MULTILINE)
encry_count = re.findall(r"(?<=pkts encrypt: )\d+", conn, re.MULTILINE)
decry_count = re.findall(r"(?<=pkts decrypt: )\d+", conn, re.MULTILINE)
spi = re.findall(r"(?<=current outbound spi: ).+", conn, re.MULTILINE)
vrf = re.findall(r"(?<= vrf: ).+", conn, re.MULTILINE)
conn_list = list(chain(vrf, local_endpt, remote_endpt, ident, encry_count, decry_count, spi))
conns_list.append(conn_list)
print(conns_list)
writer = csv.writer(f)
writer.writerow(header)
writer.writerows(conns_list)