cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4994
Views
0
Helpful
5
Replies

Automate CLI Command for 3560 Switch

james.hayek
Level 1
Level 1

I am looking for a way to create a "BATCH" or "BASH" like set of commands where I can log into a new switch, and run a prewritten set of commands. 

 

I would also be VERY open to executing this on an RaspberryPI where I can plug into the USB-B interface and automatically set/do the following:

 

  • Find the COM port I am connected to
  • enable
  • interface range GigabitEthernet 0/1-8
  • power inline static
  • no cdp enable
  • exit
  • exit
  • copy running-config startup-config

Any help/advice/documentation/reading material will be greatly appreciated.

 

Thank you in advance. 

 

5 Replies 5

Ben Walters
Level 4
Level 4

Coming from the world of Windows I personally use plink to handle things like this. It is just the command line version of Putty but it allows you to pass a file containing commands to be automatically run on the target you are connecting to. 

 

It handles serial connections well and since it is a cmd line tool you can just make a 1 line script so you don't have to manually run the command every time. 

 

 Say you have a txt file containing the commands you want to run called commands.txt, just one command per line just like you would manually type it on the switch. You get plink and connect to the switch with a serial connection on COM1

You could then run this command from powershell/cmd

plink.exe -serial COM1 -m commands.txt 

 

It will then connect to the switch on COM1 and run the commands contained in the file. Hopefully this helps, I find it is a quick and easy utility for pushing commands, I am not 100% sure but I believe you can also get plink on linux too, if you wanted to go with your RaspberryPi idea. 

 

 

 

Genius. I am going to try this now. It's a quick solution to avoid unnecessary typing. In the future, I will attempt something on the RPi.

 

Thanks again!! 

I just attempted this and it did not work. I have included the BATCH file below.

I used the "echo. |" as a preceding command to create the initial "Enter" command so I can access the command link of the CLI on the switch. 

timeout 5

@echo off
echo.
echo.
echo .. READ ME ..
echo.
echo.
echo You will be asked to enter the Port Number you want to connect to
echo You can check the device manager to see what port you are connected to.
echo Just enter the number, i.e 4, 5, 6, 7, ... etc.
echo.
echo.
echo.
echo.

set /p PortInput=Enter the JUST the Port number you want to connect to: 

echo.| plink.exe -serial COM%PortInput% -m 3560_8_PortSwitch.txt
::plink.exe -serial COM%PortInput% -m 3560_8_PortSwitch.txt


echo.
echo.

The .TXT file is as follows:

enable
interface range GigabitEthernet 0/1-8
power inline static
no cdp enable
exit
exit
copy running-config startup-config

Any idea why this is not running the text files commands?

It has been a while since I used plink for serial connections, I forgot the -m was only for SSH sessions. 

 

You should be able to use < in lace of -m in your script to pass the contents of the file to plink.

 

echo.| plink.exe -serial COM%PortInput% < 3560_8_PortSwitch.txt

 

I just saw that... using the "<" symbol now leaves me with a Prompt window with a blinking cursor. I can press enter, but this time I won't enter into the switch console. I can issue commands such as "dir", but it yeilds nothing in return.

If I enter "enable" or any other switch related command I get the following error: "'enable' is not recognized as an internal or external command, operable program or batch file."

 

The code was changed to:

cd %CD%\SwitchConfigs

timeout 5

@echo off
echo.
echo.
echo .. READ ME ..
echo.
echo.
echo You will be asked to enter the Port Number you want to connect to
echo You can check the device manager to see what port you are connected to.
echo Just enter the number, i.e 4, 5, 6, 7, ... etc.
echo.
echo.
echo.
echo.

set /p PortInput=Enter the JUST the Port number you want to connect to: 

::echo.| plink.exe -serial COM%PortInput% -m 3560_8_PortSwitch.txt
CMD /k echo.| plink.exe -serial COM%PortInput% < 3560_8_PortSwitch.txt


echo.
echo.

and text file remains:

enable
interface range GigabitEthernet 0/1-8
power inline static
no cdp enable
exit
copy running-config startup-config
exit

Review Cisco Networking for a $25 gift card