Pasted below is a bash script, combined with expect code, which:
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."
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.
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With