Listing 3. Python Script for Expect Interface

#!/usr/bin/python2

import sys, re, string
sys.path.append('.')
import Ssh

  ##  Execute these commands once as root.
"""
service httpd start
adduser ssh_test
chown ssh_test /var/log/httpd/access_log
echo "Type in 'dump_password' for the password."
passwd ssh_test
service httpd stop
"""

#---------------------------------
Host = "127.0.0.1"
User = "ssh_test"
Password = "dumb_password"
Script = "Detect_Log.py"
Destination_Script = "/tmp/Detect_Log.py"
File_Destination = "/tmp/Web_Log.log"

#---------------------------------
Download1  = Ssh.Ssh()

  ### See if the script exists.
(Error, Message) = Download1.File_Test_Exist(
  Destination_File=Destination_Script, User=User,
  Password=Password, Host=Host)

  ## If the script doesn't exist, transfer it.
if Error == -6:
  (Error2, Message2) = Download1.Script_Transfer(
    Destination_Script=Destination_Script, 
    User=User, Password=Password, Host=Host,
    Source_Script=Script)
 
    ## If okay, file is transferred.
    ## Double check to see if it exists.
  if Error2 == 0:
    (Error3, Message3) = Download1.File_Test_Exist(
    Destination_File=Destination_Script, User=User,
     Password=Password, Host=Host)  

       ## Error should be 0, if not abort.
    if Error3 == 0: Error = 0
    else:
      print "ERROR: ", Error3, Message3
      sys.exit()
    ## The transfer must have failed somehow.
  else: 
    print "ERROR: ", Error2, Message2
    sys.exit()

  ## Script already transferred, do nothing.
elif Error == 0: Do_Nothing = 1

  ## Otherwise, there is an error, abort.
else: 
  print Error, Message
  sys.exit()

  ## If everything is okay, Error should be 0
if Error == 0:
   ## Execute the script on the remote computer.
 (Error4, Output) = Download1.Script_Execute(
    Destination_Script=Destination_Script, 
    User=User, Password=Password, Host=Host)
   ## If the error is 0, get the file name.
 if Error4 == 0:
   Re1 = re.compile('GOOD:')
   Re2 = re.compile(':GOOD')
     ## Make sure the boundaries exist.
   if (Re1.search(Output)) and(Re2.search(Output)):
     Temp1 = string.split(Output,'GOOD:')
     Temp2 = string.split(Temp1[1], ':GOOD')
       ## Now we havethe filename.
     File = Temp2[0]
     if File == '': 
       print "ERROR: File is empty string."
     else:
       ## Download the file.
      FD = File_Destination
      (Error5, Output) = Download1.Download_File(
        File_Client=File, File_Destination=FD,
        User=User, Password=Password, Host=Host)
      if Error5 == 0:
        print "GOOD: file downloaded at " + FD
      else: print Error5, Output
  
 else: print Error4, Message4