Search code examples
pythonsshparamikocisco

Logging into a Cisco Wireless Controller via SSH


I have a working python script that will log into a cisco device (router/switch) and pull whatever information you want from it. I am now working on doing the same for a cisco wlc, however to ssh into a wlc it requires a 'login as' name along with the 'username/password'. I am using paramiko and can't figure out how to add the extra step of connecting with a 'login as' name. Is there another ssh module that would allow me to do this?

Here is the example of logging into a cisco wlc via ssh:

login as: test

(Cisco Controller)

User: test

Password:****

And this is the documentation for using 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)

Here is the code I'm currently working with in case it helps:

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()

Solution

  • Did a lot of playing around with paramiko.transport but couldn't get it to go. Finally just tried to .send() the information and it is now giving me the output I'm looking for:

    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()
    

    The 'Login As' field is omitted from my output, so I'm not sure what is being put in there- Possibly just the parameters from remote_conn_pre.connect(device, username=user, password=passwd). Anyways, now that I'm able to log into the devices I can start to parse the output to get the information I want.