Bash 远程执行命令的函数

需安装 expect 才能正常使用。

yum install -y expect;
#!/usr/bin/env bash
#远程执行命令
set -e;

EXPECT_SH(){
  local EXUSR=${1}
  local EXHOST=${2}
  local EXPWD=${3}
  local EXCMD=${4}
  expect -c "
    set timeout 300
    spawn ssh ${EXUSR}@${EXHOST}
    expect {
      not known {send_user [exec echo -e Erro:Host not known\n];exit}
      Connection refused {send_user [exec echo -e Erro:Connection refused\n];exit}
      (yes/no)? {send yes\r;exp_continue}
      password: {send ${EXPWD}\r;exp_continue}
      Permission denied {send_user [exec echo -e Erro:Wrong passwd\n];exit}
      ]* {send \r}
      >* {send \r}
    }
    send ${EXCMD}\rexit\r
    expect eof
  "
}
消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息