I'm trying to decipher how I would go about trying to capture some error handling in my Windows batch script for a return code from my Plink command such as "Access Denied" from a server which means I do not have access to it thus meaning I need to gain access to it.
Here is the code:
@echo on
SET userid=root
SET passwd=Welcome1%%
for /f "delims=" %%i in ('type "ipaddress.txt" ') do (
pushd "C:\Program Files (x86)\PuTTY"
echo(========================================
plink.exe -pw %passwd% %userid%@%%i hostname
echo(========================================
popd
)
Updated Code:
@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 2> logging.txt
type "logging.txt"
findstr /C:"Access denied" "logging.txt"
if errorlevel 1 (
echo Connected
) else (
echo Rejected
)
popd
)
I'm looking to print out the accesses which logon successfully and the first for loop achieves this nicely. What I want to ensure is capturing the access denied and than printing the host IP which it failed against but after continue it's processing through the list of IP addresses.
Based on the below details given I even attempted such a code setup to keep it simple based on a simple exit o or 1 code:
@echo on
SET "userid=root"
SET "passwd=Welcome1%%"
for /f "delims=" %%i in ('type "ipaddress.txt" ') do (
pushd "C:\Program Files (x86)\PuTTY"
echo(==================
plink.exe -pw %passwd% %userid%@%%i hostname
if ERRORLELVEL 1 (
echo %%i - Rejected
echo(==================
) else (
echo %%i - Connected
echo(==================
)
popd
)