Ich versuche, das Modell Nr eines Schalters aus dem Show-Inventar zu bekommen, dann setze eine ganze Zahl auf die Anzahl der Ports, die der Switch hat. Ich habe versucht, das Ergebnis in eine Zeile zu gehen und dann das mit Regex zu suchen (die Regex funktioniert ich getestetPython - Funktion gibt nicht die volle Ausgabe zurück
Es sieht nicht so aus, als ob meine Funktion das gesamte Inventar zurückgibt, es wird abgeschnitten. es sollte die unter
Switch#sh inventory
NAME: "1", DESCR: "WS-C2960X-24PS-L"
PID: WS-C2960X-24PS-L , VID: V01 , SN: XXXXX
zurückkehren Dies ist die Ausgabe im ist
Switch#
Switc
Object is: terminal length 0
Switch#sh inventory
NAME:
Inventory is:
Port Count is: 0
bekommen und das ist das Skript
#!/usr/bin/env python
import paramiko
import time
import sys
import re
# For debugging only
#paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
#
interface_regex = "interface GigabitEthernet[1-5]\/0\/"
def Send_Command_and_Get_Response(command, reponse, result):
# Send the su command
shell.send(command)
# Create a new receive buffer
receive_buffer = ""
while not reponse in receive_buffer:
# Flush the receive buffer
receive_buffer += shell.recv(1024)
# Print the receive buffer, if necessary
if result:
print receive_buffer
return receive_buffer
# VARIABLES THAT NEED CHANGED
ip = '10.X.X.X'
username = 'root'
password = 'XXXX'
port = 3010
# Create instance of SSHClient object
client = paramiko.SSHClient()
# Make sure that we add the remote server's SSH key automatically
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# initiate SSH connection
client.connect(ip, username=username, password=password,port=port, look_for_keys=False, allow_agent=False)
print "SSH connection established to %s" % ip
# Use invoke_shell to establish an 'interactive session'
shell = client.invoke_shell()
print "Interactive SSH session established"
time.sleep(1)
shell.send("\r\n")
output = shell.recv(1000)
print output
# Disable more
Send_Command_and_Get_Response("terminal length 0\n", "#", False)
objInv = Send_Command_and_Get_Response("sh inventory\n", "#", False)
strInv =""
strInv.join(objInv.splitlines())
intPort = 0
if (re.match("WS-C.*24", strInv)):
intPort = 24
elif (re.match("WS-C.*48", strInv)):
intPort = 48
print "Object is: " + objInv
print "Inventory is: " + strInv
print "Port Count is: " + str(intPort)
# Close the SSH connection
client.close()
Es ist schlechte Form zu schreiben 'nicht a in b'. Stattdessen sollten Sie 'a nicht in b' verwenden. –
scheint die Ausgabe behoben zu haben, die objInv-Zeichenfolge ist jetzt ausgefüllt, aber die strInv ist empty – AlexW
irgendwelche Ideen zu diesem Thema? viel kämpfen ... – AlexW