1

I need to check the existence of some files on different servers remotely. The servers are in Linux and AIX.

I tried python telnet and the linux shell behave differently at login so I need to use different code for different OS to recognize the prompt symbol so that I could issue command via 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')

After connection succesful, I used a simple 'ls' command to check with the files.

ls -ltr  | awk -F" " '{print $5 "|" $6 "-" $7 "-" $8 "|" $9}' | tail -n 20

However bash shell and ksh shell may behave differently in some commands so I need to a once-write-run-everywhere solution.

Available choice: Java 6, Python 3.0

lamwaiman1988
  • 3,729
  • 15
  • 55
  • 87

1 Answers1

0

Asusming both servers are UNIX/Linux based and have an SSH Daemon running you could use (Fabric)[http://docs.fabfile.org/en/1.8/api/contrib/files.html#fabric.contrib.files.exists}'s fabric.contrib.exists() function call.

Interactive Example:

from fabric.api import execute
from fabric.contrib.files import exists

>>> execute(exists, "data.csv", hosts=["localhost"])
[localhost] Executing task 'exists'
{'localhost': True}
James Mills
  • 18,669
  • 3
  • 49
  • 62