检查一个SSH远程命令在bash退出状态 [英] Checking the exit status of a ssh remote command in bash
问题描述
我需要检查一个transacions文件在远程主机上公布。目前公布的十二月今天的31无交易code。
我知道,一定的。有一个昨天。但是不管我使用的日期,30或31,返回code是相同的。
我在找检查是我认为应该是远程执行命令的返回状态SSH的返回状态
我认为if语句会以0退出,如果'LS -ltr'工作,并大于零的东西时,还是假的,如果'LS -ltr'没
工作。我认为,'如果'语句将看SHH的返回状态,这应该是远程的返回状态
执行命令 - 或LS -ltr
我怎么thie工作?
这是针对30 - 这是在远程主机上,并成功
卡斯帕@主持人:/脚本$ SSH -q -T用户@ capserbox的ls -ltr /home/DropBox/transactions_20141230.csv
-rw-R - R-- 1 rr_prd 2233047 12月31日7时26 /home/DropBox/transactions_20141230.csv
和这对31,其没有报告还为31,所以它是不成功
卡斯帕@主持人:/脚本$ SSH -q -T用户@ capserbox的ls -ltr /home/DropBox/transactions_20141231.csv
/home/DropBox/transactions_20141231.csv未找到
卡斯帕@主持人:/脚本$
我翻转日期注释掉#标签
卡斯帕@主持人:/脚本$ SSH -q -T用户@ capserbox的ls -ltr /home/DropBox/transactions_20141231.csv
/home/DropBox/transactions_20141231.csv未找到
卡斯帕@主持人:/脚本$#!/斌/庆典
今日= $(如/ bin /日期+%Y%M%D)
#今天=20141230
回声$今天
如果SSH -q -T用户@ capserbox的ls -ltr /home/DropBox/transactions_$today.csv>的/ dev / null的2 - ;&放大器; 1;
然后
回声这个工作
其他
回声这个没有工作
科幻
但是 - 当我使用的脚本进行日期 - 它是成功的,当为31,它真的应该返回这并不工作
卡斯帕@主持人:/脚本$
卡斯帕@主持人:/脚本$ VIM offshore_check
卡斯帕@主持人:/脚本$ ./offshore_check
20141230
这个工作
卡斯帕@主持人:/脚本$ VIM offshore_check
卡斯帕@主持人:/脚本$ ./offshore_check
20141231
这个工作
卡斯帕@主持人:/脚本$
卡斯帕@主持人:/脚本$
如果构建你使用应该工作,和它的工作对我来说:
$如果SSH -q -T localhost的LS -ltr /不/没有/存在;然后回显成功;否则回声失败;科幻
密码:
LS:/不/不/存在:没有这样的文件或目录
失败
我注意到,你的 LS印刷错误
程序比我得到的错误措辞不同(我试过两个不同的系统)。在我的找不到文件,在你的情况,没有这样的文件或目录LS:文件。我怀疑是在 LS
命令,该命令你在这里调用是不典型的现代Unix命令。您可能正在运行非标准版本,一个非Unix版本,或一些很老了,它实际上可能没有一个非零退出code此特定错误地退出。
I need to check that a transacions file is published on a remote host. There is no transactions code published for today the 31 of December. I know that for certain. There is one for yesterday. However no matter which date I use, the 30 or the 31, the return code is the same.
What I am looking to check is the return status of ssh which I thought should be return status of the remotely executed command I thought that the 'if statement' would exit with a 0 if the 'ls -ltr' worked and something greater than zero, or false if the 'ls -ltr' did not work. I would think that the 'if' statement would look at the return status of the shh, which should be the return status of the remotely executed command - or the 'ls -ltr'
How do i get thie to work?
this is for the 30 - which is on the remote host, and is successful
casper@host:/scripts$ ssh -q -T user@capserbox "ls -ltr /home/DropBox/transactions_20141230.csv"
-rw-r--r-- 1 rr_prd 2233047 Dec 31 07:26 /home/DropBox/transactions_20141230.csv
and this is for the 31 which there is no report yet for the 31, so it is unsuccessful
casper@host:/scripts$ ssh -q -T user@capserbox "ls -ltr /home/DropBox/transactions_20141231.csv"
/home/DropBox/transactions_20141231.csv not found
casper@host:/scripts$
I flip the date with the comment out hashtag
casper@host:/scripts$ ssh -q -T user@capserbox "ls -ltr /home/DropBox/transactions_20141231.csv"
/home/DropBox/transactions_20141231.csv not found
casper@host:/scripts$
#!/bin/bash
today=$(/bin/date +%Y%m%d)
#today="20141230"
echo $today
if ssh -q -T user@capserbox "ls -ltr /home/DropBox/transactions_$today.csv" > /dev/null 2>&1 ;
then
echo "this worked"
else
echo "this did not work"
fi
However - when i use the script for both dates - it is successful, when for the 31, it really should return "this did not work"
casper@host:/scripts$
casper@host:/scripts$ vim offshore_check
casper@host:/scripts$ ./offshore_check
20141230
this worked
casper@host:/scripts$ vim offshore_check
casper@host:/scripts$ ./offshore_check
20141231
this worked
casper@host:/scripts$
casper@host:/scripts$
The "if" construct that you're using should work, and it does work for me:
$ if ssh -q -T localhost "ls -ltr /does/not/exist"; then echo succeeded; else echo failed; fi
Password:
ls: /does/not/exist: No such file or directory
failed
I notice that the error printed by your ls
program is worded differently than the error that I got (and I tried two different systems). "file not found" in your case, "ls: file: No such file or directory" in mine. My suspicion is that the ls
command which you're invoking here isn't the typical modern Unix command. You may be running a nonstandard version, a non-Unix version, or something very old, and it may not actually be exiting with a non-zero exit code for this particular error.
这篇关于检查一个SSH远程命令在bash退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!