需安装 才能正常使用。

 install -y expect;
#!/usr/bin/env 
#expect 功能检查
set -e;

EXPECT_CHECK(){
  local EXUSR=${1}
  local EXHOST=${2}
  local EXPWD=${3}
  #ssh test
  EXP_RST=`
    expect -c "
      set timeout 300
      spawn ssh ${EXUSR}@${EXHOST} \" PASS\"
      expect {
        \"not known\" {send_user \"[exec  -e Erro:Host not known\n];exit\"}
        \"Connection refused\" {send_user \"[exec  -e Erro:Connection refused\n];exit\"}
        \"(yes/no)?\" {send \"yes\r\";exp_continue}
          \"password:\" {send \"${EXPWD}\r\";exp_continue}
        \"Permission denied\" {send_user \"[exec  -e Erro:Wrong \n];exit\"}
      }
    "| -E 'PASS|Erro'| -v echo| 's/\r//g;s/\n//g'
  `
  if [[ ${EXP_RST} && ${EXP_RST} == PASS ]]; then
    echo -e "\nEXPECT CHECK COMPLETE!\n";
    return 0;
  else
    echo -e "\n${EXUSR}@${EXHOST} EXPECT CHECK ERROR!\n";
    echo -e "\n${EXP_RST}\n";
    return 1;
  fi
}