Netmiko: Sending plain text with Netmiko?

Created on 6 Mar 2019  路  11Comments  路  Source: ktbyers/netmiko

I'm trying to write a script that will log into a bunch of Arista switches, check the version and if the version is not up to date, log into the ftp server and download the latest image. I am getting stuck at sending a plain text string of 'bash' to get to the bash shell in the Arista switch once logged in. I'm really new to python and scripting as it is, so i'm thinking my "connection.send_command('bash')" is what the issue is, because it doesn't know what 'bash' is (I ASSUME).

Is there a way to send a plain text to the CLI?

Most helpful comment

@ktbyers Easy answer. I am a complete newcomer when it comes to programming languages and writing scripts. I am even more unfamiliar with Netmiko, and just really started messing with it recently. I'll check that out, thanks.

@OzNetNerd I appreciate the advice and suggestions. I assume with some practice and time well spent, i'll re-read any scripts I create and possibly cringe at why I wrote it in one manner as opposed to another haha. Have to start somewhere I suppose. That's precisely one of the reasons why I wrote on here.

Much appreciated to the both of you.

All 11 comments

Can you please share your code? And, do you get any errors/tracebacks? If you do, can you post them too? As per the docs, send_command sends a string.

If you haven't already, you should also enable logging.

The issue is that the prompt is changing, when you execute bash. So probably the easiest solution is to add and expect_string argument to tell Netmiko what pattern to expect after the bash command is sent:

connection.send_command('bash', expect_string=r"pattern")

Where "pattern" is something that will exist in the prompt after bash is executed.

Thanks OZ and Keith. I added that line but still getting an error. Here is my current code (don't laugh at my amateur-ness haha)

import netmiko

arista_devices = '''
192.168.10.50
'''.strip().splitlines()

type = 'arista_eos'
username = 'test'
password = 'test'


for device in arista_devices:
    connection = netmiko.ConnectHandler(ip=device, device_type=type,
                                        username=username, password=password)
    print ('~'*79)
    print(connection.send_command('show run | i hostname'))

    ver_num = connection.send_command('show version | i image version')

    if "4.19.4M" not in ver_num:
        print("Version 4.19.4M is not the latest version.  Downloading latest version now...")
        connection.send_command('bash', expect_string=r"~]$")
        connection.send_command('cd /mnt/flash')
        connection.send_command('scp [email protected]://mnt/flash/EOS-4.19.4M.swi .')
        connection.send_command('test')
        connection.disconnect()

    else:
        print("Version 4.19.4M is the most current version!")
        connection.disconnect()

This is the Traceback i'm receiving:

Traceback (most recent call last):
File "test.py", line 22, in
connection.send_command('bash', expect_string=r"~]$")
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/netmiko/base_connection.py", line 1188, in send_command
search_pattern))
OSError: Search pattern never detected in send_command_expect: ~]$

I manually went into the device, went into the bash shell and coped and pasted the 3 last characters you see when the prompt changes.

Oh, and for the record, i'm certain the code below the 'bash' line is incorrect as well. I'm sure i'll run into more errors but I want to figure all of that out hopefully on my own. This one i'm stuck on currently. Thanks again gents.

I'm actually making progress with the expect commands actually. I'm going to work through this and see how far I can get. Thanks for the assistance.

FYI, I would make it simpler in your expect_string. Note, expect_string is a regex pattern so some characters will be regex special characters.

I would probably do something like:

expect_string=r"\$"

Use the $ sign character, but you will need to backslash escape it.

Ok so now another snag I hit. I get to the point where I enter the password for my device. Question is, I put the expect_string of expect_string=r"[email protected]'s password:"

It doesn't like this. I even included the white space after the colon, still no luck. I'm thinking it's reading the entire banner I have. Is there a way around this to ONLY read a certain set of characters or line, like I have above? Or would I have to put the entire banner? I'm sure the former is doable, just not sure how. I'll keep on searching and testing out in the meantime.

Is there some reason you are not using Netmiko's Secure Copy? This would probably be significantly easier and more reliable.

https://pynet.twb-tech.com/blog/automation/netmiko-scp.html

Here are a couple of suggestions to help you with your Python journey -

Instead of doing this:

arista_devices = '''
192.168.10.50
'''.strip().splitlines()

Do this:

arista_devices = ['192.168.10.50']

Instead of this:

connection = netmiko.ConnectHandler(ip=device, device_type=type,
                                        username=username, password=password)

Do this (taken from here):

from netmiko import ConnectHandler

cisco_881 = {
    'device_type': 'cisco_ios',
    'username': 'test',
    'password': 'password',
}

net_connect = ConnectHandler(**cisco_881)

Having said this, there's nothing wrong with your code. I only make the above suggestions as you might find it cleaner/easier to work with.

@ktbyers Easy answer. I am a complete newcomer when it comes to programming languages and writing scripts. I am even more unfamiliar with Netmiko, and just really started messing with it recently. I'll check that out, thanks.

@OzNetNerd I appreciate the advice and suggestions. I assume with some practice and time well spent, i'll re-read any scripts I create and possibly cringe at why I wrote it in one manner as opposed to another haha. Have to start somewhere I suppose. That's precisely one of the reasons why I wrote on here.

Much appreciated to the both of you.

@magic9669, no worries at all. I don't assume you'll improve in time, I know you will! :) Just keep doing what you're doing and your skills with improve exponentially.

Good luck, and please feel free to reach out at any time. It's a pleasure assisting.

By the way, there are Netmiko and Python channels on the Network to Code Slack. It's a great community. If you're interested, you can sign up using this link.

I am going to close this, but if there is still an issue, feel free to re-open it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

edurguti picture edurguti  路  7Comments

ktbyers picture ktbyers  路  7Comments

aegiacometti picture aegiacometti  路  6Comments

ktbyers picture ktbyers  路  5Comments

JoshuaSmeda picture JoshuaSmeda  路  3Comments