【问题标题】:SSH to Switch and redirect the printout of commandSSH 切换和重定向命令的打印输出
【发布时间】:2017-05-08 13:55:06
【问题描述】:

我正在尝试连接到交换机并重定向命令的输出,但它不起作用。它只连接和断开开关

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

和这样的打印输出:

root@cic-1:~# 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.
********************************************************************************

root@cic-1:~#

有人知道这里有什么问题吗?

【问题讨论】:

  • 您的代码(针对我可以访问的主机更新)对我有用。也许它与您正在连接的主机有关。
  • 我正在连接到交换机,我应该更正上面的代码,如下所示 stdin,stdout,stderr =client1.exec_command('show-config \n') 重定向命令没有任何问题主机基于 linux 时的打印输出,但这不适用于交换机
  • 我在脚本 import pdb pdb.set_trace() 中添加了调试,我收到此错误:--Return-- > /root/test-con.py(31)() ->None -> fn() (Pdb) n Exception AttributeError: "'NoneType' object has no attribute 'path'" in > 的

标签: python redirect


【解决方案1】:

我将代码更改如下,现在可以正常工作了:

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

【讨论】:

    猜你喜欢
    • 2012-09-12
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-09-05
    相关资源
    最近更新 更多