问题
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?
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/36335882/how-to-pass-argument-to-putty-commands-file