Netmiko: Sending Config T commands. Confused!

Created on 8 Apr 2018  路  8Comments  路  Source: ktbyers/netmiko

Hi Everyone! First timer here. A bit confused on how to send config t commands. I followed the notes but it doesn't seem to be working properly? Here's a piece of the code having problems. It runs through the code but it never changes the interface's vlan.

DEBUG HERE

if changeDecision == 'Yes':
vlanNumber = input ('\nPlease assign VLAN number: ') #User enters VLAN number
print ('\nEntering Config t')
config_commands = ['config t']
print ('\nEntering the interface specified')
config_commands = ['Interface '+interface]
print ('\nAssiging VLAN number')
config_commands = ['switchport access vlan '+vlanNumber]
config_commands = ['exit']
config_commands = ['exit']
print ('\nSaving Configuration...')
config_commands = ['exit'] #Saves configuration
print ('\nShowing New Configuration')
print (output) #Prints new output
else:
net_connect.disconnect()

Most helpful comment

@ktbyers wow! Can't believe I left that line there. It works! thanks so much!

All 8 comments

You aren't actually using net_connect properly from what I can see. There are a few different functions you can use. Each for a specific type of command (string) or config_set (list)

net_connect.send_config_set(<config_set as list>)
net_connect.send_command_expect(<command as string>)
net_connect.send_command(<command as string>)

Entering/exiting config modes is unnecessary as long as you initialize net_connect properly (see below) and use net_connect.send_config_set by handing it the full of config_commands

from netmiko import ConnectHandler

username = 'test'
password = 'test'

ip = '192.168.1.10'

device = {
    'username' : username ,
    'password' : password ,
    'device_type' : 'cisco_ios' ,
    'ip' : ip,
}

net_connect = ConnectHandler(**device)

output = net_connect.send_config_set(['interface g1/0/1','switchport access vlan 100'])

print output

Updated with Suggestions

if changeDecision == 'Yes':
    vlanNumber = input ('\nPlease assign VLAN number: ')
    # config_commands list array. Maintain the order as this is important when dealing with parent/child relationships
    config_commands = [
        'Interface ' + interface,
        'switchport access vlan ' + vlanNumber, #User enters VLAN number
        ]
    # will become ['Interface gig1/0/1', 'switchport access vlan 100'] in that order

    print ('\nAssiging VLAN number')
    # with '.send_config_set' you do not need to enter config t nor exit.
    output = net_connect.send_config_set(config_commands) # returns output of config_commands
    print (output) # show output of config_commands list


    # with '.send_command' you can send a show command
    output = net_connect.send_command('show int ' + interface) # returns output of specified command
    print (output) # show output of show int <interface>




    # with '.send_command_expect' it will try to read all of the data until it encounters the router prompt in the output. 
    print ('\nSaving Configuration...')
    output = net_connect.send_command_expect('write mem') # perfect for write mem because this can take longer than normal to return.
    print (output) # print output of write mem

    print ('\nShowing New Configuration')
    output = net_connect.send_command_expect('show run') # perfect for commands with large outputs like 'show run'
    print (output) # print new config

    net_connect.disconnect() # closing connection
else:
    net_connect.disconnect()

Thanks so you much for your feedback. Unfortunately it seems the vlan change is still not taking properly. Any ideas? I tried to simplify it to focus on this piece.

I uploaded my full code here: https://github.com/nfordhk/Network-Automation/blob/master/vlanChange

 `if changeDecision == 'Yes':
     vlanNumber = input ('\nPlease assign VLAN number: ')
     config_commands = ['Interface '+interface, 'switchport access vlan '+vlanNumber]
     print ('\nAssiging VLAN number')`

If I print interface and vlan. The variables are there. But it seems it's not executing.

print (interface)
gig2/30
print (vlanNumber)
713

@nfordhk Right after this:

    config_commands = [
        'Interface ' + interface,
        'switchport access vlan ' + vlanNumber, #User enters VLAN number
        ]

Add the following:

net_connect.send_config_set(config_commands)

Thanks @ktbyers for commenting! Hum, still seem to facing some odd issues.

Traceback (most recent call last):
  File "C:\Users\admin\Documents\Automate\netmiko-2.1.1\examples\vlan_change_utility.py", line 80, in <module>
    net_connect.send_config_set(config_commands)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\netmiko\base_connection.py", line 1292, in send_config_set
    output = self.config_mode(*cfg_mode_args)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\netmiko\cisco_base_connection.py", line 42, in config_mode
    pattern=pattern)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\netmiko\base_connection.py", line 1206, in config_mode
    if not self.check_config_mode():
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\netmiko\cisco_base_connection.py", line 31, in check_config_mode
    pattern=pattern)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\netmiko\base_connection.py", line 1188, in check_config_mode
    self.write_channel(self.RETURN)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\netmiko\base_connection.py", line 290, in write_channel
    self._write_channel(out_data)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\netmiko\base_connection.py", line 268, in _write_channel
    self.remote_conn.sendall(write_bytes(out_data))
AttributeError: 'NoneType' object has no attribute 'sendall'

@nfordhk Please post or link to your updated code.

@ktbyers I went ahead and updated the github code :)
https://github.com/nfordhk/Network-Automation/blob/master/vlanChange

@nfordhk You are disconnecting the SSH session here:

https://github.com/nfordhk/Network-Automation/blob/master/vlanChange#L67

And then you later try to use the SSH session (even though you already disconnected).

@ktbyers wow! Can't believe I left that line there. It works! thanks so much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ktbyers picture ktbyers  路  4Comments

Murali24872019 picture Murali24872019  路  6Comments

aegiacometti picture aegiacometti  路  6Comments

alexsabry picture alexsabry  路  4Comments

dfields186 picture dfields186  路  6Comments