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
| #!/usr/bin/env python
import paramiko
from getpass import getpass
import time
import json
import re
import socket
import sys
import threading
# On initialise les differentes variables
SYSLOG_SERVER="@ip"
SYSLOG_PORT=@port
# On definit l'inventaire
IP = ["@ip_1", "@ip_2", "@ip_x"]
# On initie le SSH
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
username = raw_input("Enter SSH Username: ")
password = getpass()
port = 22
# On recupere le hostname et le status de interface en format JSON
for swi in IP:
#Recuperation du hostname
ssh.connect(swi, port, username, password, look_for_keys=False)
stdin,stdout,stderr = ssh.exec_command('show hostname ')
host = stdout.readlines()[0].strip()
#Recuperation du status des interfaces
ssh.connect(swi, port, username, password, look_for_keys=False)
stdin,stdout,stderr = ssh.exec_command('show interface status | json ')
output = stdout.readlines()
#print '\n'.join(output)
#Uilisation du retour de "show interfacs status|json"
JSON_DATA = json.loads('\n'.join(output))
#print(JSON_DATA)
# On trie les elements et on renomme les retours
for items in JSON_DATA["TABLE_interface"]["ROW_interface"]:
interface = items["interface"]
state = items["state"]
type = items["type"]
Cisco_Inventory = { "Device" : host, "Interface" : str(items["interface"]), "Status" : str(items["state"]), "Type" : str(items["type"])}
Cisco_Inventory = str(Cisco_Inventory).replace("'",'"').replace("sfpAbsent","Empty").replace("connected","Used").replace("disabled","Unused").replace("--","")
print Cisco_Inventory
# on envoie le log
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((SYSLOG_SERVER, SYSLOG_PORT))
s.send(Cisco_Inventory)
s.close() |
Partager