2014-01-13 285 views
1

我需要遠程檢查不同服務器上是否存在某些文件。這些服務器位於Linux和AIX中。檢查文件遠程存在於不同的服務器上

我試圖蟒蛇telnet和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 shell和ksh shell在一些命令中可能會有不同的表現,所以我需要一次性寫入運行的解決方案。

可用的選擇:Java 6中,Python的3.0

+0

能否請你告訴你可以使用Java的哪個版本? – Jabir

+0

你忘了把問題/問題,你已經進入職位。 –

+1

爲什麼你必須使用telnet?兩臺服務器都沒有運行SSH守護進程嗎? –

回答

0

Asusming兩臺服務器都是UNIX/Linux的基礎,有一個SSH守護進程運行,你可以使用(布)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} 
+0

我認爲開發者沒有在AIX或其他類似的基於korn shell的操作系統上測試? – lamwaiman1988

+0

不確定;但因爲它是POSIX它*應該*工作:)我只是試一試 - Python是跨平臺,所以在理論*它應該「只是工作」:) –

相關問題