Error handling in bash/expect script
Pasted below is a bash script, combined with expect code, which:
- connects to remote host via ssh, collects files and prepare tgz file;
- copy tgz file from remote host to local machine;
- connects to remote host via ssh again and remove previously created tgz file;
- finally, extract tgz file on local machine.
Everything works, if passed arguments are valid (i.e., $host
, $user
, and $pass
). If one of them is incorrect, script hangs. I wonder how to include some error handling (e.g., in $cmd1) to terminate script with message if username (or password) is incorrect?
Thanks in advance for any pointers.
#!/bin/bash
prog=$(basename $0)
NO_ARGS=0
E_OPTERROR=85
# Script invoked with no command-line args?
if [ $# -eq "$NO_ARGS" ]; then
echo "Usage: $prog [-h host] [-u username] [-p password]"
echo " $prog -help for help."
exit $E_OPTERROR
fi
showhelp() {
echo "Usage: $prog [-h host] [-u username] [-p password]"
echo " -h: host"
echo " -u: username"
echo " -p: password"
echo " -help: this help message"
exit 2
}
user=""
host=""
pass=""
now=$(date +"%m-%d-%Y")
dir="data_$now"
file="data.tgz"
while getopts "h:u:p:help" name; do
case $name in
h)
host=$OPTARG
;;
u)
user=$OPTARG
;;
p)
pass=$OPTARG
;;
help)
showhelp $0
;;
esac
done
if [ -d "$dir" ]; then
rm -R $dir
mkdir $dir
else
mkdir $dir
fi
cmd1=$(expect << EOF
spawn ssh $user@$host
expect "password: "
send "$pass\n"
expect "$ "
send "cd /tmp\n"
expect "$ "
send "tar -czf $file \`find . -maxdepth 1 -name 'f2p_*' -print\`\n"
expect "$ "
send "logout"
EOF)
cmd2=$(expect << EOF
spawn scp $user@$host:/tmp/$file $dir
expect "password: "
send "$pass\n"
expect "$ "
EOF)
CMD3=$(expect << EOF
spawn ssh $user@$host
expect "password: "
send "$pass\n"
expect "$ "
send "cd /tmp\n"
expect "$ "
send "rm $file\n"
expect "$ "
send "logout"
E开发者_运维技巧OF)
echo "$cmd1"
echo "$cmd2"
echo "$cmd3"
cd $dir
tar -xzf $file
rm $file
count=$(ls -1 | wc -l | awk '{gsub(/^ +| +$/, "")}1')
cd ..
#clear
echo "All done. Extracted $count *.net files."
The expect
command can perform different actions based on different answers.
For example, you could define $cmd1 like this:
cmd1=$(expect << EOF
spawn ssh $user@$host
expect "password: "
send "$pass\n"
expect {
{
"Permission denied, please try again." {
send_user "Wrong password"
exit
}
"$ " {
send "cd /tmp\n"
expect "$ "
send "tar -czf $file \`find . -maxdepth 1 -name 'f2p_*' -print\`\n"
expect "$ "
send "logout"
}
}
EOF)
If the provided password doesn't work, this script will exit and print the message "Wrong password".
For a more advanced usage please take a look at the man page. There are a few examples there and many other options.
expect {
{
"Permission denied, please try again." {
send_user "Wrong password"
}
"$ " {
send "cd /tmp\n"
expect "$ "
send "tar -czf $file \`find . -maxdepth 1 -name 'f2p_*' -print\`\n"
expect "$ "
send "logout"
}
Here when we remove the command exit, expect should print "Wrong password" and than do the next part by sending "cd /tmp\n"....... is that correct than?
精彩评论