通过Windows批处理文件Plink处理特定错误 [英] Plink handling specific error through Windows batch file
问题描述
我试图解释如何在Windows批处理脚本中捕获一些错误处理,以获取Plink命令的返回代码,例如来自服务器的访问被拒绝",这意味着我无权访问因此,这意味着我需要访问它.
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.
这是代码:
@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
)
更新代码:
@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
)
我希望打印出成功登录的访问权限,并且第一个for循环可以很好地实现此目的.我要确保的是捕获拒绝的访问,而不是打印失败的主机IP,但继续处理后,它会通过IP地址列表进行处理.
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.
基于下面给出的详细信息,我甚至尝试了这样的代码设置,以基于简单的o或1个代码使代码保持简单:
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
)
推荐答案
Plink返回成功的退出代码0或失败的返回代码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.
不过,您可以使用 findstr
命令在Plink的错误输出中搜索特定的错误消息.
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
)
这篇关于通过Windows批处理文件Plink处理特定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!