-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetSwitchArpTable.py
57 lines (44 loc) · 1.58 KB
/
GetSwitchArpTable.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
import paramiko
import openpyxl
# create ssh client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# connect to switch
ssh.connect(hostname='192.168.1.1', port=22, username='admin', password='admin')
# execute command to get arp table
stdin, stdout, stderr = ssh.exec_command('display arp')
# read output and parse arp table
# This example applies only to the H3C S10000 series based on the processing results of different switches
arp_table = []
for line in stdout:
if 'GE' in line:
arp_entry = line.split()
arp_table.append({'ip': arp_entry[0], 'mac': arp_entry[1], 'int': arp_entry[3]})
# create excel workbook and worksheet
workbook = openpyxl.Workbook()
worksheet = workbook.active
# write arp table to worksheet
worksheet['A1'] = 'IP Address'
worksheet['B1'] = 'MAC Address'
worksheet['C1'] = 'Interface'
for i, arp_entry in enumerate(arp_table):
worksheet.cell(row=i+2, column=1, value=arp_entry['ip'])
worksheet.cell(row=i+2, column=2, value=arp_entry['mac'])
worksheet.cell(row=i+2, column=3, value=arp_entry['int'])
# save workbook to file
workbook.save('arp.xlsx')
# close ssh connection
ssh.close()
# upload result to sftp server
# create ssh client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# connect to server
ssh.connect(hostname='192.168.1.10', port=22, username='admin', password='admin')
# create sftp client
sftp = ssh.open_sftp()
# upload file to server using tftp
sftp.put('arp.xlsx', '/home/admin/arp.xlsx')
# close sftp and ssh connections
sftp.close()
ssh.close()