【问题标题】:Error handling in bash/expect scriptbash/expect 脚本中的错误处理
【发布时间】:2017-12-07 23:56:05
【问题描述】:

下面粘贴的是一个bash脚本,结合expect代码,其中:

  • 通过ssh连接远程主机,收集文件并准备tgz文件;
  • 将tgz文件从远程主机复制到本地机器;
  • 再次通过 ssh 连接到远程主机并删除之前创建的 tgz 文件;
  • 最后,在本地机器上解压 tgz 文件。

如果传递的参数有效(即$host$user$pass),一切正常。如果其中之一不正确,脚本将挂起。我想知道如果用户名(或密码)不正确,如何包含一些错误处理(例如,在 $cmd1 中)以终止带有消息的脚本?

提前感谢您的任何指点。

#!/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."

【问题讨论】:

  • @Ignacio 拼写已更正。感谢您的评论。

标签: bash error-handling expect


【解决方案1】:

expect 命令可以根据不同的答案执行不同的动作。

例如,您可以这样定义 $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)

如果提供的密码无效,此脚本将退出并打印消息“密码错误”。

有关更高级的用法,请查看手册页。那里有一些示例和许多其他选项。

【讨论】:

    【解决方案2】:
    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"
       }
    

    在这里,当我们删除命令退出时,期望应该打印“错误密码”,而不是通过发送“cd /tmp\n”来执行下一部分......。 是这样吗?

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多