4

I have a batch file that starts PuTTY and executes commands listed in a text file. I want to be able to pass in parameters to the text file that has my commands to be run on the remote server.

This is what I currently have -

start C:\Users\putty.exe -load "server" -l userID -pw Password -m commands.txt

Is there a way to pass for example a version number as an argument to the commands.txt file?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ALUMOM
  • 83
  • 1
  • 2
  • 8
  • Take a look at this ===> http://stackoverflow.com/questions/36291324/batch-file-command-hide-password?answertab=active#tab-top – Hackoo Mar 31 '16 at 14:28
  • Try this: `start "" C:\Users\putty.exe -load "server" -l userID -pw Password -m commands.txt`; the `start` command might interprete the first quoted string as a window title, so the `""` explicitly gives one... – aschipfl Mar 31 '16 at 14:36

2 Answers2

4

You have to generate the commands.txt on the fly:

set PARAMETER=parameter
echo ./myscript.sh %PARAMETER% > commands.txt
start C:\Users\putty.exe -load "server" -l userID -pw Password -m commands.txt

Side note: To automate tasks, you should consider using plink.exe instead of putty.exe:

set PARAMETER=parameter
echo ./myscript.sh %PARAMETER% > commands.txt
plink.exe -load "server" -l userID -pw Password -m commands.txt

Plink can even accept the command on its command-line, what makes your task even easier:

plink.exe -load "server" -l userID -pw Password ./myscript.sh parameter
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

I was banging my head on this and just realized we can easily achieve this in the following simple way.

I have a text file which I want to execute using Putty and it also takes run time arguments. In this case , rather than having static text file , I generated the file on the fly using windows bash. My below use case will execute "Execute.txt" on the host I select

Example

@ECHO OFF

set prodhost[1]=host1 <br/>
set prodhost[2]=host2<br/>

ECHO Please select the box where you want to perform checkout.<br/>
ECHO 1. host1 <br/>
ECHO 2. host2<br/>
set /p host="Enter Your Option: "


echo echo "Login and switch user was successful" <br/>
echo hostname <br/>
echo sudo su - 'username' ^<^<  EOF <br/>
echo <br/>
echo EOF <br/>
)>"Execute.txt" <br/>

"plink.exe" -l %username% -ssh !prodhost[%host%]! -m Execute.txt

BottomLine : Generate scripts on the fly that you want to execute using bash and store in onto a file and then push it using Plink

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Balaji
  • 191
  • 3
  • 14