Sysadmin > UtilitIes > ExpeCt

expect

#!/bin/bash
export HOST="localhost"
export USER="myuname"
export PASS="mypassword"

script=<<'END_OF_SCRIPT'
    set timeout 120
    spawn ssh $env(USER)@$env(HOST)
    expect "password:"
    send "$env(PASS)\r"
    expect "\$"
    send "ls\r"
    expect -re "$env(USER).*"
    send "logout"
    expect eof
END_OF_SCRIPT

VAR=$(expect -c "$script")

echo "==============="
echo "$VAR"

sshbot

Prompt for AIX

#! /usr/bin/expect
# expect {filename-of-the-script} user password host

set timeout 20

;# -- command line arguments to our scripts
set user [lindex $argv 0]
set password [lindex $argv 1]
set host [lindex $argv 2]

;# shell prompt character
set prompt "> "

;# -- main procecure
proc dostuff {} {
   send -- "sudo cat /etc/security/passwd\r"
   return
}

;# script start running here

spawn /usr/bin/ssh $user@$host

;# loops forever until we get a shell prompt

while (1) {

   expect {
     ;# -- This is the prompt when you first use
     ;# -- ssh that says "Are you sure you want to continue ..."

     "no)? " {
        send -- "yes\r"
     }

     ;# -- the prompt for password
     "password: " {
         send -- "$password\r"
     }

     ;# -- break in case of failed login
     "Permission denied" {
         puts "Access not granted, aborting..."
         exit 1
     }

     timeout {
         puts "Timeout expired, aborting..."
         exit 1
     }

     ;# -- and finally we got a shell prompt
     "$prompt" {
        dostuff
        break
     }
   }

}

;# -- exit
expect "$prompt"
send -- "exit\r"

expect eof