If the server accepts sftp sessions, I will not worry about pexpect, but instead use the paramiko SSH2 module for Python:
import paramiko transport=paramiko.Transport("10.10.0.0") transport.connect(username="service",password="word") sftp=paramiko.SFTPClient.from_transport(transport) filestat=sftp.stat("/opt/ad/bin/email_tidyup.sh")
The code opens an SFTPClient connection to a server on which you can use stat () to check for files and directories.
sftp.stat will raise an IOError ("There is no such file") when the file does not exist.
If the server does not support sftp, this will work:
import paramiko client=paramiko.SSHClient() client.load_system_host_keys() client.connect("10.10.0.0",username="service",password="word") _,stdout,_=client.exec_command("[ -f /opt/ad/bin/email_tidyup.sh ] && echo OK") assert stdout.read()
SSHClient.exec_command returns a triple (stdin, stdout, stderr). Here we just check for any output. Instead, you can change the command or check stderr for any error messages.
source share