Connecting to a Cisco device with Netmiko

UPDATE:

If you are installing netmiko on ubuntu, I would recommend to install pip first then install netmiko.

$sudo apt install python-pip

Then once this is done, you can execute:

pip install netmiko

Now lets continue with exploring Netmiko. I don’t intend to write long blogs and will try to keep each one as short as possible. On this blog I will show you how to connect to a cisco device using netmiko, run show commands and execute a command.

The first thing we need to establish is you have to make sure that you can simply reach the device you’re after. A simple icmp test would suffice.

[delanajero@DELAN ~]$ ping 192.168.123.2
PING 192.168.123.2 (192.168.123.2) 56(84) bytes of data.
64 bytes from 192.168.123.2: icmp_seq=1 ttl=255 time=2.52 ms
64 bytes from 192.168.123.2: icmp_seq=2 ttl=255 time=1.73 ms
64 bytes from 192.168.123.2: icmp_seq=3 ttl=255 time=1.75 ms
64 bytes from 192.168.123.2: icmp_seq=5 ttl=255 time=1.75 ms
^C
— 192.168.123.2 ping statistics —
5 packets transmitted, 4 received, 20% packet loss, time 4005ms
rtt min/avg/max/mdev = 1.738/1.942/2.526/0.339 ms
[delanajero@DELAN ~]$

Another test is you should be able to putty into your device using SSH. I will not go into the details as how to setup your device with SSH access.

Once you’ve verified you have reachability and can ssh into it using putty, it is time to access it using netmiko. Run python and make sure you got netmiko’s connecthandler running without issues.

>>> from netmiko import ConnectHandler

Next we’ll provide the details of my device:

>>> platform = ‘cisco_ios’ # this is optional you don’t need to include the platform
>>> host = ‘192.168.123.2’
>>> username = ‘admin’
>>> password = ‘secretpassword’
>>> device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)

From other documentation I have seen, python would display that connection has been established but in my case nothing came up so you might experience the same thing. I take it as long as no error message showed up, you are fine.

Now the fun starts. Let us execute some show commands.

>>> output = device.send_command(‘show version’)
>>> print output

Cisco IOS Software, C800 Software (C800-UNIVERSALK9-M), Version 15.3(3)M5, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2015 by Cisco Systems, Inc.
Compiled Wed 04-Feb-15 11:24 by prod_rel_team

ROM: System Bootstrap, Version 15.4(1r)T1, RELEASE SOFTWARE (fc1)

DELAN_HOME_ROUTER uptime is 19 weeks, 10 hours, 7 minutes
System returned to ROM by power-on
System restarted at 06:53:46 aest Sat Mar 4 2017
System image file is “flash:c800-universalk9-mz.SPA.153-3.M5.bin”
Last reload type: Normal Reload
Last reload reason: power-on

 

This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.

Cisco C887VA-K9 (revision 1.0) with 488524K/35763K bytes of memory.
Processor board ID FGL191XXXXMD
1 DSL controller
1 Ethernet interface
4 FastEthernet interfaces
1 ATM interface
1 Virtual Private Network (VPN) Module
DRAM configuration is 32 bits wide
255K bytes of non-volatile configuration memory.
250880K bytes of ATA System CompactFlash (Read/Write)

 

License Info:

License UDI:

————————————————-
Device# PID SN
————————————————-
*0 C887VA-K9 FGL191XXXXMD

License Information for ‘c800’
License Level: advipservices Type: Default. No valid license found.
Next reboot license Level: advipservices

Configuration register is 0x2102

You simply get the point, basically any command you run inside the single quotes in device.send_command(‘<command here>’) would be executed. This gets passed on to the container named output and with the print command, outputs the results.

>>> device.config_mode()
u’config term\r\nEnter configuration commands, one per line. End with CNTL/Z.\r\nDELAN_HOME_ROUTER(config)#’

>>>
>>> device.config_mode()
u”
>>> device.send_command(‘interface Loopback100’)
u”
>>> device.send_command(‘ip address 1.1.1.1 255.255.255.255′)
u”
>>> device.exit_config_mode()
u’end\r\nDELAN_HOME_ROUTER#’
>>>

Note:

I when using show command I can do short cut commands but when i did the same for going into an interface it gave me an error message:

>>> device.send_command(‘int lo100’)

Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “/usr/lib/python2.7/site-packages/netmiko/base_connection.py”, line 811, in send_command
search_pattern))
IOError: Search pattern never detected in send_command_expect: DELAN\_HOME\_ROUTER\(config\)\#

Now let us verify if that went through:

>>> output = device.send_command(‘show run interface lo100’)>>> print output
Building configuration…

Current configuration : 65 bytes
!
interface Loopback100
ip address 1.1.1.1 255.255.255.255
end

>>>

Viola! Pretty simple isn’t it. Ok now, just remember you are still connected to the device. Once you are done testing out some commands, disconnect netmiko from your device with

>>> device.disconnect()

 

Leave a comment