Just thought I'd insert the script I made to hopefully save some people hours of syntax and man page research
a) SingleIP, IP Range, or IPList file
b) script name to run on the IP/range (actual unix sh/ksh type script)
c) optionally do nmap scan for port 22 open (i.e. subnet with some non-Linux machines)
-want to avoid IPs putty cannot connect to, avoiding script being able to continue properly
d) optionally run pscp command to autocache the host key (putty does not do this automatically)
This batch assumes:
1. you have the full PuTTY package and SSH key installed in C:\Program Files\PuTTY\
2. batch/scripts/IPlist files installed in C:\Program Files\PuTTY\scripts\
3. The NMAP and PuTTY working directories are in your command path
https://www.harmonyhit.com/PuttyBatch.bat
This is mostly useful for having a single script (such as updating an SSL certificate) across a range of machines
Here is complete batch file:
@ECHO OFF
rem You must modify the CACHEKEY and SCRIPT section to reflect your SSH key location
set IPFILE=IPList_temp.txt
set runagain=n
set OPTION=1
set RANGE=
set IP=
set SCAN=n
set KEYCACHE=n
:OPTION
CLS
ECHO (Working directory is set to C:\Program Files\PuTTY\scripts\)
ECHO.
ECHO 1. Single IP
ECHO 2. IP list file
ECHO 3. IP Range
ECHO.
set /p OPTION="Choose IP type:"
IF %OPTION%==3 GOTO IPRANGE
IF %OPTION%==2 GOTO IPLIST
IF %OPTION%==1 GOTO SINGLEIP
echo Please select a valid option
GOTO OPTION
:IPRANGE
ECHO Enter the IP range(s) as in the following example. Use a space between multiple ranges:
ECHO i.e. "10.21.0.15-99 10.21.1.15-100"
set /p RANGE=Enter Range:
echo %RANGE% > %IPFILE%
GOTO SCRIPTNAME
:SINGLEIP
set /p IP=Enter IP:
echo %IP% > %IPFILE%
GOTO SCRIPTNAME
:IPLIST
set /p IP=Enter IPList filename:
copy /Y %IP% %IPFILE%
GOTO SCRIPTNAME
:SCRIPTNAME
set /p SCRIPT=Enter script name:
if not exist %SCRIPT% (
echo filename does not exist!
GOTO SCRIPTNAME )
ECHO.
set /p SCAN=Do Nmap scan first? (recommended):
if %SCAN%==n GOTO :RUNCACHE
rem Check whether IP type is "range" as nmap cannot read an IP range from file and must be type directly in nmap command
IF %OPTION%==3 GOTO NMAPRANGE
:NMAP
echo Scanning IPs for port 22 open...
nmap --open -n -p22 -iL %IPFILE% -oG - | findstr /E Up > nmap_temp.txt
GOTO AFTERNMAP
:NMAPRANGE
echo Scanning IPs for port 22 open...
nmap --open -n -p22 %RANGE% -oG - | findstr /E Up > nmap_temp.txt
:AFTERNMAP
echo DONE
rem nmap formatting is not correct, the following removes extra info
for /f "tokens=2" %%A in (nmap_temp.txt) do echo %%A >> nmap_temp2.txt
rem remove hidden space at end of IP (requires "repl" be in the batch file directory)
type "nmap_temp2.txt" | repl " " "" > %IPFILE%
:RUNCACHE
set /p KEYCACHE=Scan and cache SSH key (y/n) ?
if %KEYCACHE%==n GOTO SCRIPT
:CACHEKEY
rem Run through all IPs to cache the SSH Host Key if not already cached
for /F "tokens=*" %%A in (%IPFILE%) do ( echo y | "C:\Program Files\PuTTY\pscp.exe" -l root -i "C:\Program Files\PuTTY\SSH.ppk" -touch %%A:/tmp/test )
:SCRIPT
for /F "tokens=1" %%A in (%IPFILE%) do ( "C:\Program Files\PuTTY\putty.exe" -ssh %%A -t -l root -i "C:\Program Files\PuTTY\SSH.ppk" -m "C:\Program Files\PuTTY\scripts\%SCRIPT%" )
if exist nmap_temp.txt (del nmap_temp.txt)
if exist nmap_temp2.txt (del nmap_temp2.txt)
set /p runagain="Press Enter to Finish or y to rerun"
if %runagain%==y GOTO OPTION
1
No need to use
– Martin Prikryl – 2017-12-19T15:15:46.867-m
switch with Plink, as it allow to specify command directly on its command-line, see my answer.I am choosing this answer as I prefer using
-ssh host1
which relates to putty Profile vsuser@host
– FreeSoftwareServers – 2017-12-21T12:39:13.920@FreeSoftwareServers There's no difference to PuTTY in this respect. You can do
plink site command
the same way as with PuTTY (except forcommand
). Plink and PuTTY has basically the same set of command-line options. – Martin Prikryl – 2017-12-28T08:02:45.803