我需要远程检查不同服务器上的一些文件是否存在。服务器在 Linux 和 AIX 中。
我尝试了 python telnet 和 linux shell 在登录时的行为不同,所以我需要为不同的操作系统使用不同的代码来识别提示符号,以便我可以通过 telnet 发出命令:
Linux:
tn = telnetlib.Telnet(self.mHost)
tn.read_until(b"login: ")
tn.write(self.mUsername.encode('ascii') + b"\n")
tn.read_until(b"Password: ")
tn.write(self.mPassword.encode('ascii') + b"\n")
(tn.read_until(b"$")).decode('ascii')
人工智能:
tn2 = telnetlib.Telnet(self.mHost)
tn2.read_until(b"login: ")
tn2.write(self.mUsername.encode('ascii') + b"\n")
tn2.read_until(b"Password: ")
tn2.write(self.mPassword.encode('ascii') + b"\n")
(tn2.read_until(b">")).decode('ascii')
连接成功后,我使用了一个简单的“ls”命令来检查文件。
ls -ltr | awk -F" " '{print $5 "|" $6 "-" $7 "-" $8 "|" $9}' | tail -n 20
然而,bash shell 和 ksh shell 在某些命令中的行为可能不同,因此我需要一个一次写入,到处运行的解决方案。
可选:Java 6、Python 3.0
最佳答案
假设这两个服务器都是基于 UNIX/Linux 的,并且有一个运行的 SSH 守护进程,您可以使用 (Fabric)[ http://docs.fabfile.org/en/1.8/api/contrib/files.html#fabric.contrib.files.exists } 的 fabric.contrib.exists()
函数调用。
交互示例:
from fabric.api import execute
from fabric.contrib.files import exists
>>> execute(exists, "data.csv", hosts=["localhost"])
[localhost] Executing task 'exists'
{'localhost': True}
关于java - 远程检查文件是否存在于不同的服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21083159/