Solved Batch file using PLINK that accesses multiple devices and ..

July 5, 2016 at 14:10:16
Specs: Windows 7
Not enough space in the subject box.

Here's what I have (and I am new to any type of scripting):

Currently I have a .bat file that ssh's into a particular device (using PLINK) and accepts the RSA2 disclaimer. It logs out and re-initiates an SSH session where it enables FTP on the device. After that the script initiates an FTP session where it uploads a new config file (.ini). After the config file is uploaded it initiates another SSH session, executes a few more commands, outputs some info to a .txt file and closes. I have this working very well.

Here's what I aim to attempt to do.
I have 200+ of these devices and 200+ config files. I need to figure out how to use a For /F command to log into each device (pulling each IP from .txt file) and upload each specific config (all housed in the same folder and each named with it's DNS).


Config files named:

Reiterating: I need the script to log into each device and upload each specific config file.

Here's the generic version of my first batch file:
cmd.exe /c echo y | plink -v -l username -pw password PDU1 exit
timeout /t 3
plink -l username -pw password PDU1 < Initial_Commands.txt
timeout /t 30
call FTP_Execution.bat

Here's the next part (FTP portion):
ftp -i -s:FTP_Commands.bat
timeout /t 5
call Final_Commands.bat

Here's the final leg:
plink -v -l username -pw password PDU1 < Final_Commands.txt >>output.txt

I realize it's a lot to follow so I apologize if my request is vague or unclear. Any help is greatly appreciated.

See More: Batch file using PLINK that accesses multiple devices and ..

Reply ↓  Report •

July 6, 2016 at 20:40:10
✔ Best Answer
So, my understanding is you want to supply username, password, and DNS supplied from a local textfile to drive the process for each device? And I take it that the process works "hard-wired", and you just want to make it work more flexibly over multiple devices. I don't know the format of your textfile that stores this data (fields, delimiters, etc)., but the machinery would be the same regardless: read the textfile, get any required elements' values (username, password, DNS, anything else specific to a particular device), then use them to upload the .ini file. You'll need to insert them into the FTP script and echo them to the plink session, and that's fairly simple. The "for /f" loop takes the value from each line and constructs the FTP script (btw: I recommend some other extension for FTP scripts other than .bat, since it's not a bat file. I always use .FTP). The driver batch can either build text feeders for the plink session or supply them echoed through pipes. Sometimes it's easier to "chunk" these kinds of files:
static portions of the files are kept on-hand, and the batch builds the final result by successively appending static chunks and dynamic material. Otherwise, the batch simply echos the entirety, splicing in the dynamic stuff. You get the idea. The only other consideration is password security, (ie: stored in a raw textfile), but I'll assume you have already addressed that. Here's an example (NOT a working solution):
::========== assuming textfile content/format: username passwd DNS IP
for /f "tokens=1-4" %%a in (driver.txt) do (
cmd.exe /c echo y | plink -v -l %%a -pw %%b %%c exit
timeout /t 3
plink -l %%a -pw %%b %%c < Initial_Commands.txt
timeout /t 30
echo (
open %%d
cd %%c
get %%c.ini
FTP -i -s:ftp_commands.FTP
timeout /t 5
plink -v -l %%a -pw %%b %%c < Final_Commands.txt >>output.txt
::======= end SAMPLE batch

message edited by nbrane

Reply ↓  Report •
Related Solutions

Ask Question