Python 通过SSH登录到Cisco无线控制器
我有一个可以工作的python脚本,它将登录到cisco设备(路由器/交换机),并从中提取您想要的任何信息。我现在正在为cisco wlc做同样的工作,但是要通过ssh连接到wlc,它需要一个“login as”名称以及“username/password”。我正在使用paramiko,不知道如何添加使用“登录身份”名称连接的额外步骤。是否有其他ssh模块允许我这样做 以下是通过ssh登录cisco wlc的示例: 登录身份:test (思科控制器) 用户:测试 密码:**** 这是使用paramiko.connect的文档: 连接(self,主机名,端口=22,用户名=None,密码=None, pkey=None,key\u filename=None,timeout=None,allow\u agent=True, 查找(关键字=真,压缩=假) 以下是我目前正在使用的代码,以防它有所帮助:Python 通过SSH登录到Cisco无线控制器,python,ssh,paramiko,cisco,Python,Ssh,Paramiko,Cisco,我有一个可以工作的python脚本,它将登录到cisco设备(路由器/交换机),并从中提取您想要的任何信息。我现在正在为cisco wlc做同样的工作,但是要通过ssh连接到wlc,它需要一个“login as”名称以及“username/password”。我正在使用paramiko,不知道如何添加使用“登录身份”名称连接的额外步骤。是否有其他ssh模块允许我这样做 以下是通过ssh登录cisco wlc的示例: 登录身份:test (思科控制器) 用户:测试 密码:**** 这是使用para
import web
import paramiko
import time
urls = (
'/wlc', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(user=" ", passwd=" ", device=" ")
user = form.user
passwd = form.passwd
device = form.device
#Used to change term length to 0 making it so we don't need to hit a key to scroll through the output
def disable_paging(remote_conn, command="terminal length 0", delay=1):
#Send an enter
remote_conn.send("\n")
#Send 'terminal length 0'
remote_conn.send(command)
#Wait for command to complete
time.sleep(delay)
#Read output
output = remote_conn.recv(65535)
return output
#Have remote_conn_pre equal to the SSHClient module in paramiko
remote_conn_pre = paramiko.SSHClient()
#This allows us to bypass the ssh key popup that comes up when you ssh into a device for the first time
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
remote_conn_pre.connect(device,username=user, password=passwd) #Connect to the device using our defind parameters
remote_conn = remote_conn_pre.invoke_shell() #invoke_shell module gives us access to talk to the device via the cli
output = disable_paging(remote_conn) #Run the disable_paging function that sets term length to 0
remote_conn.send("\n")
remote_conn.send("sh ap inventory")
remote_conn.send("\n")
time.sleep(2)
output = remote_conn.recv(65535) #Read all output given
return output #print output to screen
remote_conn_pre.close() #Close the SSH connection
if __name__ == "__main__":
app.run()
和paramiko.transport玩了很多,但没能让它运行。最后,我尝试了.send()信息,现在它给了我想要的输出:
import web
import paramiko
import time
urls = (
'/wlc', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
def GET(self):
return render.hello_form()
def POST(self):
#Retrieve user input from web form
form = web.input(user=" ", passwd=" ", device=" ")
user = form.user
passwd = form.passwd
device = form.device
#Make it so we don't need to hit a key to scroll through the output
def disable_paging(remote_conn, command="config paging disable", delay=1):
remote_conn.send(command)
remote_conn.send("\n")
time.sleep(delay)
#Have remote_conn_pre equal to the SSHClient module in paramiko
remote_conn_pre = paramiko.SSHClient()
#This allows us to bypass the ssh key popup that comes up when you ssh into a device for the first time
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
remote_conn_pre.connect(device, username=user, password=passwd) #Connect to the device using our defind parameters
remote_conn = remote_conn_pre.invoke_shell() #invoke_shell module gives us access to talk to the device via the cli
#Log into the WLC
remote_conn.send(user)
remote_conn.send("\n")
remote_conn.send(passwd)
remote_conn.send("\n")
#Run the disable_paging function
output = disable_paging(remote_conn)
#Run command
remote_conn.send("sh ap summary")
remote_conn.send("\n")
#Some WLCs can be slow to show output, have program wait 5 seconds
time.sleep(5)
output = remote_conn.recv(65535) #Read all output given
return output #print output to screen
remote_conn.close() #Close the SSH connection
if __name__ == "__main__":
app.run()
我的输出中省略了“Login As”字段,因此我不确定输入的是什么-可能只是来自remote\u conn\u pre.connect(device,username=user,password=passwd)
的参数。不管怎样,现在我可以登录到设备上了,我可以开始解析输出以获得我想要的信息