bash/expect 脚本中的错误处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6812232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error handling in bash/expect script
提问by Andrej
Pasted below is a bash script, combined with expect code, which:
下面粘贴的是一个bash脚本,结合expect代码,其中:
- 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.
- 通过 ssh 连接到远程主机,收集文件并准备 tgz 文件;
- 将 tgz 文件从远程主机复制到本地机器;
- 再次通过 ssh 连接到远程主机并删除之前创建的 tgz 文件;
- 最后,在本地机器上提取 tgz 文件。
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?
如果传递的参数有效(即$host、$user、 和$pass),则一切正常。如果其中之一不正确,脚本将挂起。我想知道如果用户名(或密码)不正确,如何包含一些错误处理(例如,在 $cmd1 中)以终止带有消息的脚本?
Thanks in advance for any pointers.
在此先感谢您的指点。
#!/bin/bash
prog=$(basename 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)
)
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 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"
}
;;
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."
采纳答案by nmat
The expectcommand can perform different actions based on different answers.
该expect命令可以根据不同的答案执行不同的操作。
For example, you could define $cmd1 like this:
例如,您可以像这样定义 $cmd1:
##代码##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.
有关更高级的用法,请查看手册页。那里有一些示例和许多其他选项。
回答by CanBuyukburc
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”来做下一部分......这是正确的吗?

