cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3790
Views
0
Helpful
4
Replies

Is there a run a set of commands from a file in flash:?

mjcconsulting
Level 1
Level 1

I would have thought this to be an extremely common request, but I can not find anything on the web which describes if what I want to do is possible, much less how to do it. So, asking here - hopefully someone can respond with how to do this if it is possible. 

 

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, then 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 file 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 (effectively paste, just automated) 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.

- 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 <<<<<<<<<<

    # something like: execute flash:/apply-configs/csr01-apply-config.cfg would be awesome

    # why is is not that simple? why is is hard to find how to do this? 

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

4 Replies 4

ngkin2010
Level 7
Level 7

Hi,

In my opinion, you could do the job on your ssh client machine instead by using TCL Expect, which is much more efficient than ssh to the device every time for each action/command.

Returning to your question about how to execute command listed in text file, you would have to use either TCL or the EEM 4.0 to do that.

The idea is simple, build either TCL or EEM script and store it in the flash.
Both TCL or EEM could do:

foreach sentence in the text file @ flash:
execute sentence
result checking

Once you got the pre-defined script, you may execute the script using ssh.

Note: I did not try that before, I usually using TCL expect on remote server to do so, which is much more easy to maintain.

EEM: https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/eem/configuration/xe-16/eem-xe-16-book.html
TCL: https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/ios_tcl/configuration/xe-16/ios-tcl-xe-16-book/Cisco_IOS_XE_Scripting_with_Tcl.html

Thanks for the suggestion. I'll look through both books, but what you suggest is not really what I'm looking for. There may be something there though that could work, though - IF - there's a method to "run" a script containing IOS commands.

 

I don't need or want to run one statement at a time and check for the results. This is a computer generated file, which I know will be correct, used to create virtual infrastructure for testing, eventually automated within CloudFormation. So, first this should work pretty much 100% of the time, as it's a new configuration, known to work, against a brand new device. And second, if it doesn't work, I can detect that out-of-band manually, and just drop and re-create the Stack.

 

So, what I'm trying to do is simple:

  • Not this: (a) Open the generated file of commands, (b) "select-all", (c) ssh into the CSR, (d) "config t", (e) paste all statements as a single block - as interactive actions, hands on mouse and keyboard
  • This: (a) upload the generated file of commands to some location in flash: (b) "run" this file, meaning exactly the same as just pasting it's lines into a config t session interactively. Just - non-interactive, statements in a script run from MacBook over an ssh connection.

So, I'll look, but for completeness of this thread, what I want, based on your initial feedback, might be: How do I run a TCL or EEM script, which contains only a linear sequence of IOS commands (no other logic or control statements)?

 

And, are those tools installed by default on a Cisco CSR v1000 AMI as published by AWS, or must I do something special to install them? 

I just skimmed through both links on EEM and TCL. While the TCL link looks closer, it's still not clear how to just RUN a simple list of commands contained in a file.

I'm just flabbergasted such a seemingly simple task on any computer does not have a simple to find and use answer when it comes to IOS. All I seem to find is either "open a terminal and cut and paste the list of commands" or some reference to TCL which is both way more complicated than I need or want - and - I still can't seem to find a simple description of how to actually RUN a file containing TCL commands. I don't need to create functions. I just need a list of what I'd paste into a terminal session.

I mean, what century are we in? Is there no concept of scripting a router configuration from a separate host over an ssh connection?

Imagine I wanted to change the static IP of a linux server, then restart networking, then just write "I did it" to a file. I want all 3 actions to be atomic, as I will lose connectivity when changing the IP, but I want the statements to run from start to finish direct on the host. To do this on a linux system is trivial:

# Create script 
cat > /tmp/script << EOF
#!/bin/bash
sed -i -e "/^IPADDR0=/s/=.*$/=IPADDR0=172.16.16.16/" \
/etc/sysconfig/network-scripts/ifcfg-eth0
service network restart
echo "I did it" > /tmp/result
EOF

# Copy script to host and make executable
scp /tmp/script myuser@myserver.mycompany.com:/tmp/script
ssh myuser@myserver.mycompany.com chmod +x /tmp/script

# Run the script
ssh myuser@myserver.mycompany.com /tmp/script

I'm still hoping someone knows how to do this simple thing: Upload a file containing a sequence of IOS commands in the same format as you'd paste them into a terminal session, then run this file. No functions, no error handling, dirt simple. If this is possible, it's certainly not been something I can find via Google search, even in the links provided earlier on this thread.

 

Hi,

 

I did a lab on NX-OS and it basically can do what you want. the same TCL should be able to run on CSR, you may need to try it. Beware to change the correct path (e.g. bootflash).

 

$ cat execute.tcl
set fp [open "/bootflash/commands" r]
set file_data [read $fp]
set data [split $file_data "\n"]
foreach line $data {
    exec $line
}
close $fp

When you ready the pre-defined script, upload it to Cisco device (you need to do it on the first time only):

$ scp execute.tcl test@192.168.15.187:bootflash:
User Access Verification
Password:
execute.tcl                                   100%  144    17.7KB/s   00:00

Then you prepare your command line file (here is just an example):

$ cat commands
terminal length 0
show version | count
show run | count
show ip int b | count

Upload the command file to your device:

$ scp commands test@192.168.15.187:bootflash:
User Access Verification
Password:
commands                                      100%   78     9.6KB/s   00:00

Execute it by remote ssh commands

tclsh bootflash:///execute.tcl

 

Result:

switch# tclsh bootflash:///execute.tcl
Error: getting term size
39

299

4

 

We usually not do it on network equipment itself, when there is hundred of devices in your infrastructure. It's impossible to manage script for each device. So we prefer to do remote scripting on remote UNIX server by using TCL expect.