Plink handling specific error through Windows batch file

不打扰是莪最后的温柔 提交于 2019-12-08 03:13:43

问题


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
)

回答1:


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
)


来源:https://stackoverflow.com/questions/33665786/plink-handling-specific-error-through-windows-batch-file

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