2017-05-08 5 views
0

Ich versuche, die Ausgabe des Befehls zu verbinden und umzuleiten, aber es funktioniert nicht. Es ist nur verbinden und trennen von dem SchalterSSH zum Umschalten und Umleiten des Ausdrucks des Befehls

#!/usr/pkg/bin/python 
#importing modules 
import paramiko 
import sys 
import time 

# setting parameters like host IP, username, passwd and port 
# to gather cmds 
HOST = "10.50.170.21" 
USER = "user" 
PASS= "password" 
PORT = 2024 

# A function that logins and execute commands 
def fn(): 
    client1=paramiko.SSHClient() 
    #add missing client key 
    client1.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    #connect to switch 
    client1.connect(HOST,username=USER,password=PASS,port=2024) 
    print "SSH connection to %s established" %HOST 
    stdin,stdout,stderr =client1.exec_command('show-config \n') 
    print stdout.read() 

fn() 

und dem Ausdruck wie folgt aus:

[email protected]:~# python test-con.py 
SSH connection to 10.50.171.22 established 
******************************************************************************** 
BSP 8100 

This system is provided for authorized users only. If you are not 
an authorized user, please exit IMMEDIATELY. 
******************************************************************************** 

[email protected]:~# 

jemand weiß, was hier das Problem sein kann?

+0

Ihr Code (für einen Host aktualisiert ich zugreifen können) funktioniert für mich. Vielleicht hat es etwas mit dem Host zu tun, mit dem Sie sich verbinden. – FamousJameous

+0

Ich verbinde um zu wechseln und ich sollte den Code oben wie unten korrigieren stdin, stdout, stderr = client1.exec_befehl ('show-config \ n') Ich habe kein Problem, um den Befehl prinout wenn der Host umleiten linux basiert, aber das funktioniert nicht für Schalter – ne9een

+0

ich habe das Debuggen das Skript import pdb pdb.set_trace hinzugefügt() und erhalte ich diesen Fehler: --Return-- >/root/Test-con .py (31) () -> Keine -> fn() (Pdb) n Ausnahme AttributError: Das Objekt '' NoneType 'hat kein Attribut' Pfad '' in > ignoriert – ne9een

Antwort

0

änderte ich den Code wie unten angegeben, und es funktioniert jetzt:

#!/usr/pkg/bin/python 

#importing modules 
import paramiko 
import sys 
import time 
import pdb 
#pdb.set_trace() 

# setting parameters like host IP, username, passwd and number of iteration 
# to gather cmds 
HOST = "10.50.171.22" 
USER = "advanced" 
PASS = "ett,30" 
PORT = 2024 
ITERATION = 3 

# A function that logins and execute commands 
def fn(): 
    client1=paramiko.SSHClient() 
    #add missing client key 
    client1.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    #connect to switch 
    client1.connect(HOST,username=USER,password=PASS,port=PORT) 
    print "SSH connection to %s established" %HOST 

    remote_conn = client1.invoke_shell() 
    remote_conn.send("\n") 
    remote_conn.send("show \n") 
    time.sleep(2) 
    output = remote_conn.recv(10000) 
    print output 

# stdin,stdout,stderr =client1.exec_command("config /n") 
# stdin,stdout,stderr =client1.exec_command("show /n") 
# print stdout.read() 
    client1.close()  
fn()