我有一个工作的python脚本,它将登录到cisco设备(路由器/交换机)并从中提取您想要的任何信息。我现在正在为cisco wlc做同样的事情,但是为了ssh进入wlc,它需要'login as'这个名称以及'username / password'。我正在使用paramiko,无法弄清楚如何添加与“登录为”名称连接的额外步骤。还有另一个ssh模块允许我这样做吗?
以下是通过ssh登录cisco wlc的示例:
登录为:test
(思科控制器)
用户:测试
密码:****
这是使用paramiko.connect的文档:
connect(self,hostname,port = 22,username = None,password = None,pkey = None,key_filename = None,timeout = None,allow_agent = True,look_for_keys = True,compress = False)
这是我正在使用的代码,以防它有帮助:
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_conn_pre.connect(device, username=user, password=passwd)
的参数。无论如何,既然我能够登录设备,我就可以开始解析输出以获取我想要的信息。
JMarks - 您的代码中的想法非常有用。谢谢
我有以下 - 这很好地工作:)
...
def cisco_wlc_reconfigure(device_ip,username,password):
print('\nAttempting to connect to',device_ip)
ssh_conn_pre = paramiko.SSHClient()
ssh_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_conn_pre.connect(device_ip, port=22, username=username, password=password, look_for_keys=False, allow_agent=False)
ssh_conn = ssh_conn_pre.invoke_shell()
time.sleep(5)
ssh_conn.send(username)
ssh_conn.send("\n")
ssh_conn.send(password)
ssh_conn.send("\n")
commands = ['show interface summary\r\n', 'show wlan summary\r\n']
for i in commands:
print('\n----------------------------------------\n')
print('DATE/TIME:',datetime.datetime.now(),'\n')
ssh_conn.send(i)
time.sleep(5)
output = ssh_conn.recv(65535)
print(output.decode())
print('\n')
...