1

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
)
Eddie
  • 37
  • 1
  • 7

1 Answers1

0

The Plink returns either exit code 0 for success or 1 for failure. So you cannot use the exit code to check for a specific error type.

Though, you can use findstr command to search for the specific error message in the error output of the Plink.

plink.exe -pw %passwd% %userid%@%%i hostname 2> errors.txt

type errors.txt

findstr /C:"Access denied" errors.txt

if errorlevel 1 (
    echo Success or different error
) else (
    echo Access denied
)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I tried the code snippet you have suggested and I have updated the code i'm using above however I'm seeing a few errors. FINDSTR: Cannot open logging.txt – Eddie Nov 13 '15 at 03:49
  • I added another code of snippet based on what you said about return codes. I think using the return codes would be sufficient as well. – Eddie Nov 13 '15 at 04:44
  • You most likely do not have a write access to the `C:\Program Files (x86)\PuTTY`. So the output is not written. You have to use a different path for the output log file. – Martin Prikryl Nov 13 '15 at 06:46
  • Is there a way to source the file from the direct location the script is running from? My batch script is running from my own personal "My Documents" directory and not the directory in which Putty is setup. – Eddie Nov 16 '15 at 09:16
  • If you do not specify a path, the file is read/written from/to the current working directory. What would be the "My documents" folder. Or I do not understand your comment. If you want to explicitly refer to the folder of the batch file, even if the current working directory is different, see [Get current batchfile directory](http://stackoverflow.com/q/17063947/850848). – Martin Prikryl Nov 16 '15 at 17:12