How to automate an SSH login with a batch file?

93 Views Asked by At

At my place of work we have to use SSH via CMD to access our Cisco switches. I'd like to write a batch file to semi-automate the login process so that all we need to do is type in the desired switch name and our password. We can't use PUTTY, Cisco Analyser, SSH keys, expect etc., our only allowed method is CMD (it's baffled us too!)

I'm currently the only one of us around and my knowledge on this is not up to par for the job, hence asking you fine folks for a bit of help!

What I'd like the script to do is firstly ask which switch I would like to connect to, use that as a variable to fill in the command at which point the switch will ask for the password to validate access.

The base command for the connection is:

ssh <account-name>@<switch-name(FQDN)>

So far my script reads as:

@echo off
title SSH Switch Access
echo Please enter destination switch name...

As I said, I'm relatively new to writing scripts as I hail from a hardware background so never really had to use them much or had others around that could do it, as much as I'd like to learn how to do this on my own there's a hell of a lot to learn and it'll take me a long while to get conversant enough to do it alone.

1

There are 1 best solutions below

10
Gerhard On

Here is one example. You can list the devices as comma separated values in a variable. Simply run a for loop through them and use the choice command to assign a number to hostname. You simply press the keyboard number to make the connection.

Because only 9 numeric values can be used (unless you use non numerical) I simply created a next function where you can use option 9 to show the next set. You can use a similar approach to add a previous, if you'd like. Simply update your device list and username.

@echo off & cls & title SSH Switch Access
setlocal enabledelayedexpansion
set myuser=USERNAME
set "switchList=switch-1,switch-2,switch-3,switch-4,switch-5,switch-6,switch-7,switch-8,switch-9,switch-10,switch-11,switch-12,switch-13,switch-14,switch-15,switch-16,switch-17,switch-18,switch-19,switch-20"
for %%i in (%switchList%) do (
   set /a cnt+=1
   set "dev[!cnt!]=%%i"
   call echo !cnt!. %%dev[!cnt!]%%
   set chosen=!chosen!!cnt!
   if !cnt! equ 8 (
      set /a cnt+=1
      set "dev[9]=Next"
      call echo !cnt!. %%dev[!cnt!]%%
      set chosen=!chosen!!cnt!
      call :list
      if !selected! neq 9 goto :EOF
      cls
      set /a cnt=0
      set chosen=
   )
)

:list
echo(
choice /c %chosen% /m "Select destination switch:"
if not %errorlevel% equ 9 ssh %myuser%@!dev[%errorlevel%]!
set "selected=%errorlevel%"
if not %errorlevel% equ 9 goto :EOF

Note, if you have a huge list of devices, you can either add the devices to a file and read from the file. Again, this might mean a lot of skipping pages to find your device, in that case, it is much simpler to just run the below and type the switch name on request:

@echo off
set myuser=USERNAME
set /p "switch=Please enter destination switch name : "
ssh.exe %myuser%@%switch%

Edit: Do not name your script the same as any executable in path. i.e. in this instance, do not name it ssh.bat or ssh.cmd.

Reason for this being; cmd.exe will always first check if the basename you supplied is available in the current directory, if not, will it seatch your %path% variable for the basename and extentions in order as specified by %pathext%

I this case, ssh.bat or ssh.cmd exists in the current directory. When you paunch the script will it then run ssh.bat user@switch instead. Rename your script to my_ssh.bat or similar and also change the ssh in the script to ssh.exe to ensure the script does not launch itself.