08-09-2018 05:00 PM - edited 03-03-2019 08:52 AM
Good afternoon
I'm running into a wall when trying to create a script that'll go through my terminal server (C2811) to configure a new (Catalyst) switch.
I've tested this code on the C2811 and it works fine, but it seems like once its going through the async module it dies. I'm able to manually SSH into 192.168.25.100:2002 and it works fine so it doesn't seem like its a bad module/cable.
I'm not married to SSH2-Python, so if there is another library/solution I'll be okay with trying it out.
Thoughts?
SSH2-Python Script:
import socket, time from ssh2.session import Session host = '192.168.25.100' # C2811 Router user = 'tech' password = 'tech' port = 2002 # Port where my unconfigured switch is connected to. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port)) session = Session() session.handshake(sock) session.userauth_password(user, password) channel = session.open_session() channel.shell() channel.write("terminal length 0\r") channel.write("show run\r") channel.close() size, data = channel.read() while size > 0: print(data.decode()) size, data = channel.read() print("Exit status: {0}".format(channel.get_exit_status()))
08-09-2018 05:26 PM - edited 08-09-2018 10:23 PM
I'm relatively new (less than a month) to any kind of programming/dvelopment, let alone automation with Python, so someone more saavy might have a better suggestion.
That being said I've had success using exscript for config and management commands to cisco devices.
Take a look at some examples in the exscript documentation to see if it might be something of use.
Also, Nornir just recently surfaced and might be worth a look.
Edit: I didn't fully read your post before responding with my previous Exscript example which didn't at all provide any guidance for accessing the ASYNC line on your 2811, so apologies for that.
08-09-2018 05:29 PM
Hi
You should try Paramiko, Netmiko or Expect. They always worked fine for me.
-If I helped you somehow, please, rate it as useful.-
08-09-2018 10:19 PM
08-12-2018 11:41 AM
I would recommend paramiko.
Have a look at the below example -
import paramiko import time def disable_paging(remote_conn): '''Disable paging on a Cisco router''' remote_conn.send("terminal length 0\n") time.sleep(1) # Clear the buffer on the screen output = remote_conn.recv(1000) return output if __name__ == '__main__': ip = '1.1.1.16' username = 'testuser' password = 'password' # Create instance of SSHClient object remote_conn_pre = paramiko.SSHClient() # Automatically add untrusted hosts (make sure okay for security policy in your environment) remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # initiate SSH connection remote_conn_pre.connect(ip, username=username, password=password, look_for_keys=False, allow_agent=False) print "SSH connection established to %s" % ip # Use invoke_shell to establish an 'interactive session' remote_conn = remote_conn_pre.invoke_shell() print "Interactive SSH session established" # Strip the initial router prompt output = remote_conn.recv(1000) # See what we have print output # Turn off paging disable_paging(remote_conn) # Now let's try to send the router a command remote_conn.send("\n") remote_conn.send("show ip int brief\n") # Wait for the command to complete time.sleep(2) output = remote_conn.recv(5000) print output
Hope this helps.
Please rate helpful posts and accept solutions :)
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide