Expect工具是依赖tcl的,所以也需要安装tcl
有yum直接梭哈
- yum install -y tcl* expect*
复制代码
没yum自己下载,有网络可以wget 安装tcl - # wget https://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz
- # tar zxvf tcl8.4.19-src.tar.gz
- # cd tcl8.4.19/unix && ./configure
- # make
- # make install
复制代码 安装expect- # wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz
- # tar zxvf expect5.45.tar.gz
- # cd expect5.45
- # ./configure --with-tcl=/usr/local/lib --with-tclinclude=../tcl8.4.19/generic
- # make
- # make install
- # ln -s /usr/local/bin/expect /usr/bin/expect
复制代码
自用expect小脚本,触发ssh登录操作,配置完一把环境变量,到目录下执行xxx命令,10s后再执行其他,最终退出ssh,记录日志 - expect << EOF
- log_file expect.log
- set timeout 120
- spawn ssh $user@$ip
- expect "*assword:" {send "$pass\n"}
- expect "$ " {send "source /etc/profile\n"}
- expect "$ " {send "source ~/.bash_profile\n"}
- expect "$ " {send "cd /data/app\n"}
- expect "$ " {send "$xxx_sh stop\n"}
- expect "$ " {send "sleep 10\n"}
- expect "$ " {send "$xxx_sh start\n"}
- expect "$ " {send "\n"}
- expect "$ " {send "exit\n"}
- EOF
复制代码
|