linux服务器之间互相传输文件,可以使用scp命令,但是每次传输文件需要输入密码,解决方法可以是两个服务器直接设置公钥证书,也可以使用except脚本实现。
本文记录使用expect脚本实现的方法:
首先需要服务器安装expect,你可以which expect查看服务器上是否有安装。
其次except脚本首行需要注明:
!/usr/bin/expect -f:使用expect来解释该脚本
但是如果是编写bash编辑器的shell脚本,可以通过expect -c的命令嵌入在脚本中,以下是一个scp的脚本示例:
#!/bin/bash
str1=$1
password="pasword123"
DIR_PATH="/home/wjz"
expect -c "
spawn scp $1 wls12c@192.168.1.10:$DIR_PATH
expect {
\"*password\" {set timeout 300;send \"$password\r\";}
}
expect eof"
在使用expect
时,基本上都是和以下四个命令打交道:
命令 | 作用 |
---|---|
spawn | 启动新的进程 |
expect | 从进程接收字符串 |
send | 用于向进程发送字符串 |
interact | 允许用户交互 |
spawn
命令用来启动新的进程,spawn
后的expect
和send
命令都是和使用spawn
启动的新进程进行交互。expect
通常用来等待一个进程的反馈,我们根据进程的反馈,再使用send
命令发送对应的交互命令。send
命令接收一个字符串参数,并将该参数发送到进程。interact
命令用的其实不是很多,一般情况下使用spawn
、expect
和send
和命令就可以很好的完成我们的任务;但在一些特殊场合下还是需要使用interact
命令的,interact
命令主要用于退出自动化,进入人工交互。比如我们使用spawn
、send
和expect
命令完成了ftp登陆主机,执行下载文件任务,但是我们希望在文件下载结束以后,仍然可以停留在ftp命令行状态,以便手动的执行后续命令,此时使用interact
命令就可以很好的完成这个任务。- 如果expect和send同一行需要加{}
- 如果expect后是{
- }说明满足这里面任意一个条件就会send
有主机和备机上的应用,现在通过脚本实现以下目的:
1.在主机升级文件后,文件自动同步至备机
2.重启备机
脚本如下:
#!/bin/bash
echo 'synchronize scripts begin : '`date +%F_%T`
PASSWORD="%r#W@Q!"
DIR=/home/wls12c/weblogic/user_projects/domains/appdomain/app/APP-ROOT
files=`find $DIR -mtime -1`
echo $files
for FILE in $files
do
if [ -d "$FILE" ]; then
echo 'DIR:' $FILE
G_DIR=$FILE
/usr/bin/expect <<EOF
spawn ssh wjz@192.168.1.10
set timeout 3
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "$PASSWORD\r"}
}
expect "*wls12c@*" {send "mkdir -p $FILE\r"}
expect eof
exit
EOF
else
echo 'file' $FILE
DIR_PATH="/home/wjz"
expect -c "
spawn scp $FILE wjz@192.168.1.10:${FILE%/*}
expect {
\"*password\" {set timeout 300;send \"$PASSWORD\r\";}
}
expect eof"
fi
echo ''
done
echo ''
echo 'synchronize scripts end : '`date +%F_%T`
echo ''
echo 'Start Server at ' `date +%F_%T`
/usr/bin/expect <<EOF
spawn ssh wjz@192.168.1.10
set timeout 3
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "$PASSWORD\r"}
}
expect "*wls12c@*" {send "sh /home/wls12c/weblogic/user_projects/domains/appdomain/start_app_script.sh\r"}
expect eof
exit
EOF
echo 'Back Server has been started! ' `date +%F_%T`
expect离线安装
安装expect依赖tcl
http://www.tcl.tk/software/tcltk/downloadnow84.tml
选择: tcl8.4.20-src.tar.gz
上传至 /usr/local/src目录下
2)安装依赖
tar -zxvf tcl8.4.20-src.tar.gz
cd /usr/local/src/tcl8.4.20/unix
./configure
make && make install
3)下载安装expect
下载地址:
expect5.45.tar.gz
https://sourceforge.net/projects/expect/files/latest/download
tar -zxvf expect5.45.3.tar.gz
cd /usr/local/src/expect5.45.3
./configure --prefix=/usr/local/expect --with-tcl=/usr/local/lib --with-tclinclude=/usr/local/src/tcl8.4.20/generic/
make && make install
建立软连接方便使用
ln -s /usr/local/bin/expect /usr/local/expect/bin/expect
ln -s /usr/local/bin/expect /usr/bin/expect
4)测试:
(1)输入命令 : expect
(2)如出现:expect1.1>,说明成功。
安装报错:
使用root用户安装expect没问题,但是切换到普通用户使用expect报错:
expect: error while loading shared libraries: libexpect5.45.3.so: cannot open shared object file: No such file or directory
查询libexpect5.45.3.so
find / -name libexpect5.45.3.so
发现是是文件权限问题,修改后即可:
chmod -R 755 /usr/local/lib/expect5.45.3/
参考文章:
Linux shell 知识心得07 expect+信号处理 - 虾虾78 - 博客园
linux shell用expect实现在scp时自动输入密码_师从名剑山的博客-CSDN博客_expect自动输入密码
Linux技巧(五):expect安装与介绍 | 精美案例 | bash: spawn: command not found详解_血煞长虹的博客-CSDN博客