我需要远程检查不同服务器上是否存在一些文件。服务器位于Linux和AIX中。
我尝试了python和linux在登录时的行为不同,所以我需要为不同的操作系统使用不同的代码来识别提示符号,这样我就可以通过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')
AIX:
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和ksh在某些命令中的行为可能有所不同,所以我需要一次编写-运行-无处不在的解决方案。
可用选择: Java 6,Python3.0
发布于 2014-01-13 03:18:25
使用这两台服务器都是基于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}
https://stackoverflow.com/questions/21083159
复制相似问题