[英]Error handling in bash/expect script
Pasted below is a bash script, combined with expect code, which: 下面粘贴的是一个bash脚本,结合了expect代码,其中:
Everything works, if passed arguments are valid (ie, $host
, $user
, and $pass
). 一切正常,如果传递的参数是有效的(即
$host
, $user
和$pass
)。 If one of them is incorrect, script hangs. 如果其中一个不正确,脚本将挂起。 I wonder how to include some error handling (eg, in $cmd1) to terminate script with message if username (or password) is incorrect?
我想知道如果用户名(或密码)不正确,如何包含一些错误处理(例如,在$ cmd1中)来终止脚本和消息?
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."
The expect
command can perform different actions based on different answers. expect
命令可以根据不同的答案执行不同的操作。
For example, you could define $cmd1 like this: 例如,您可以像这样定义$ cmd1:
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? 这里当我们删除命令退出时,期望应该打印“错误的密码”而不是通过发送“cd / tmp \\ n”来做下一部分.......这是正确的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.