Reputation: 37
I'm putting together a very basic automation script using the windows batch operation in which will loop through a list of IP addresses and run a plink command to logon and keep alive the account on the server because it has recently logged onto the server.
I believe I have most of the function working however I am having an issue with passing through the password. I'm seeing an issue where if the password I have has special characters and in which I run the script through command prompt it does not pass through the special character to the plink command. Here is a look of the script:
@echo on
SET userid=root
SET passwd=Welcome1%
for /f "delims=" %%i in ('type "ipaddress.txt" ') do (
pushd "C:\Program Files (x86)\PuTTY"
plink.exe -pw %passwd% %userid%@%%i hostname
popd
)
The ipaddress.txt file contains:
10.0.0.1
10.0.0.2
10.0.0.3
The idea is to go through the list for each IP address, logon and validate access. I'm also looking to ensure the value Y or N is passed to make sure a server is trusted or not as part of the script. Any help would be greatly appreciated.
Upvotes: 1
Views: 2157
Reputation: 30218
You could see your script behaviour with @echo on
(run it from within a cmd
window instead of double-clicking). You are right that some special characters need to be escaped. If your password should be Welcome1%
literally then use
SET passwd=Welcome1%%
or advanced set
command syntax
SET "passwd=Welcome1%%"
Edit. Above advice covers particularly %
percentage sign in a string. However, escaping some characters with special meaning (e.g. redirectors <
, >
, |
, &
etc.) as ^<
, ^>
, ^|
, ^&
seems to be a bit inconvenient and could not suffice. Therefore required advanced set
command syntax and delayed expansion enabled instead.
For instance, Wel<come>1%
string could be used as follows:
SET "userid=root"
SET "passwd=Wel<come>1%%"
for /f "delims=" %%i in ('type "ipaddress.txt" ') do (
pushd "C:\Program Files (x86)\PuTTY"
SETLOCAL EnableDelayedExpansion
plink.exe -pw !passwd! %userid%@%%i hostname
ENDLOCAL
popd
)
Upvotes: 2