【发布时间】: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 > 的