How to pass argument to PuTTY commands file

被刻印的时光 ゝ 提交于 2020-01-01 14:09:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!