2017-05-08 6 views
0

スイッチに接続しようとしましたが、コマンドの出力をリダイレクトしようとしましたが動作しません。それだけでスイッチSSHでコマンドの出力を切り替えてリダイレクトします

#!/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() 

と、このようなプリントアウトからの接続と切断:

[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]:~# 

誰もがここで問題になることができます知っていますか?

+0

あなたのコード(私がアクセスできるホストのために更新)私のために動作します。おそらく、それはあなたが接続しているホストと関係があります。 – FamousJameous

+0

私はスイッチに接続していますが、以下のように上記のコードを修正する必要があります stdin、stdout、stderr = client1.exec_command( 'show-config \ n') ホストprinoutコマンドをリダイレクトする際に問題はありません。 Linuxベースこれは(私はスクリプト 輸入PDB pdb.set_traceにデバッグを追加したスイッチ – ne9een

+0

のために動作しません) であり、私はこのエラーが表示されます。 --Return-- > /ルート/テスト-CON "NoneType 'オブジェクトには<バインドされたメソッドの属性がありません"#:。msgid。 0x7f9947f65cd0の>は無視されます。 – ne9een

答えて

0

私は以下のようにコードを変更し、今では正常に動作します:

#!/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() 
関連する問題