Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
EOF)

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."
like image 245
Andrej Avatar asked Jul 25 '11 05:07

Andrej


People also ask

How do you handle errors in bash script?

To activate this "exit-on-error" behavior in bash, you can use the set command as follows. Once called with -e option, the set command causes the bash shell to exit immediately if any subsequent command exits with a non-zero status (caused by an error condition). The +e option turns the shell back to the default mode.

What does expect do in bash?

Bash scripts provide many programs and features to carry out system automation tasks. The expect command offers a way to control interactive applications which require user input to continue.

What is expect in shell script?

The Linux expect command takes script writing to an entirely new level. Instead of automating processes, it automates running and responding to other scripts. In other words, you can write a script that asks how you are and then create an expect script that both runs it and tells it that you're ok.


2 Answers

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.

like image 50
nmat Avatar answered Sep 20 '22 03:09

nmat


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?

like image 26
CanBuyukburc Avatar answered Sep 18 '22 03:09

CanBuyukburc