【发布时间】:2015-03-29 03:48:48
【问题描述】:
我正在编写一个 python nagios 检查来检查文件上次更新时的时间戳,但是我需要检查脚本中的两个单独的主机,所以我想使用远程 ssh 命令将时间戳返回到stdout 并使用它来确保脚本在过去 24 小时内完成,因为当脚本完成时它会触及文件。
当我从命令提示符运行命令时,它可以工作,但是当我从我的 python 代码运行它时,它不会将输出返回到我的程序。
Grants-MacBook-Pro:test GrantZukel$ ssh ubuntu@hostname sudo ls -la /home/ubuntu/ | grep code_regen_complete
The authenticity of host 'ec2-54-144-160-238.compute-1.amazonaws.com (54.144.160.238)' can't be established.
RSA key fingerprint is hidden.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/Users/GrantZukel/.ssh/known_hosts).
-rw-r--r-- 1 root root 0 Jan 29 17:23 code_regen_complete
Grants-MacBook-Pro:test GrantZukel$
蟒蛇是:
def get_code_time():
p = subprocess.Popen(['ssh','ubuntu@hostname', '"sudo ls -la /home/ubuntu/ | grep code_regen_complete"'], stdout=subprocess.PIPE)
out, err = p.communicate()
m = re.search('\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}', out)
return m.group(0)
但是,当我使用调试器时,out 或 err 变量完全为空。有什么想法吗?
【问题讨论】:
-
不相关,但是如果你以
ubuntu登录,为什么需要sudo才能列出/home/ubuntu? -
你为什么不直接
ls -l /home/ubuntu/code_regen_complete?