Operating System - HP-UX
1827214 Members
2671 Online
109716 Solutions
New Discussion

Re: Expect script to notify if server is down

 
Robert Legatie
Advisor

Expect script to notify if server is down

Hello,

I have the following expect script that runs automatically when called by a shell script. I am trying to figure out a way to notify me or log somewhere if the server is down or if the process cannot connect to the server for any reason.

Expect script is below:
#!/opt/expect/expect
# procedure to attempt connecting; result 0 if OK, 1 elsewhere
proc connect {password} {
expect {
"Password:" {
send "$password\r"
expect {
"sftp*" {
return 0
}
}
}
}
# timed out
return 1
}

# first step is to assign the arguments to appropriate variables
set argc [llength $argv]
set user_id [lindex $argv 0]
set password [lindex $argv 1]
set host_id [lindex $argv 2]
set upload_dir [lindex $argv 3]
set directory [lindex $argv 4]
set filename [lindex $argv 5]


spawn /usr/local/bin/sftp2 $user_id@$host_id
set rez [connect $password]
if { $rez == 0 } {
send "cd $upload_dir\r"
expect "sftp>"
send "lcd $directory\r"
expect "sftp>"
set timeout -1
send "put $filename\r"
expect "sftp>"
send "quit\r"
expect eof
exit 0
}
puts "\nError connecting to the server:\n"
exit 1

I need the "Error connecting to the server" message emailed to me anytime it happens. Is there a way possible to do this using expect.

Thanks in advance.
1 REPLY 1
Robert Legatie
Advisor

Re: Expect script to notify if server is down

I did find it, I can use the command
exec echo "string" | mailx -s "Subject"