0

use-case: Create a VPN on AWS to a Cisco CSR v1000 router, generate a sequence of commands based on AWS-generated values replacing tokens in a template, configure the router, then save the startup-config - all in a fully automated manner, from the developer's laptop.

What I've always done myself to configure a router from a set of commands I need to apply in order, is what I see on the web when I search for this question - I ssh into the router, then "config t", then I manually paste the commands. I don't want to have this manual cut-and-paste action - I want to run statements on either the router itself, or on my laptop, to apply the commands which are in a script to the router configuration - INSTEAD of having to cut and paste them.

I thought I'd list what I have figured out so far here as it's been surprisingly hard to find how to do even this much simply and in one place. The following assumptions apply:

  • I have an ~/.ssh/config file which simplifies what I need to specify on the command line
  • This supplies the user as ec2-user, and specifies the correct SSH key.
  • I'm using the ssh-agent, which supplies the SSH key passphrase for me
  • The router's FQDN for this example is csr01.mydomain.com
  • The set of commands I want to run is in csr01-apply-config.cfg
  • I want to upload this file to the router, then apply all commands in the file ideally in a single action, as this script may need to make changes which could temporarily disrupt connectivity
  • I want to store all such apply files in flash:apply-configs directory

So here's what I have so far - just missing the one key step:

  1. List files in flash: filesystem

    # ssh csr01.mydomain.com dir flash:
    
  2. List files in nvram: filesystem

    # ssh csr01.mydomain.com dir nvram:
    
  3. Create a directory to hold uploaded configuration apply files
    # ssh csr01.mydomain.com mkdir flash:apply-configs
    
  4. Upload the generated configuration apply file
    # scp csr01-apply-config.cfg csr01.mydomain.com:flash:/apply-configs/csr01-apply-config.cfg
    
  5. Confirm uploaded file exists
    # ssh csr01.mydomain.com dir flash:apply-configs
    
  6. Apply commands
    >>>>>>>>>> HOW CAN I RUN THE COMMANDS IN A FILE HERE <<<<<<<<<<
    For example, something like:

    # run flash:/apply-configs/csr01-apply-config.cfg
    

    It seems like this should be simple, but I can't seem to find any description on if this is possible and if so, what commands to use

  7. Show running config

    # ssh csr01.mydomain.com show run
    
  8. Write running config to memory
    # ssh csr01.mydomain.com write mem
    
  9. Copy startup config back to laptop
    # scp csr01.mydomain.com:nvram:startup-config csr01-startup-config-$(date +%Y%m%d-%H%M).cfg
    
mjcconsulting
  • 75
  • 1
  • 8

1 Answers1

1

You could use a small python script. For the SSH part personally I find the python netmiko library pretty useful. You could do something like this (excerpt from the github page):

from netmiko import ConnectHandler

cisco_881 = {
    'device_type': 'cisco_ios',
    'host':   '10.10.10.10',
    'username': 'test',
    'password': 'password',
    'port' : 8022,          # optional, defaults to 22
    'secret': 'secret',     # optional, defaults to ''
}

# Establish an SSH connection to the device by passing in the device dictionary.
net_connect = ConnectHandler(**cisco_881)
# Execute show commands.
output = net_connect.send_command('show ip int brief')
print(output)
mcrivaro
  • 31
  • 5