scp不输入密码传文件,使用expect实现&expect离线安装

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后的expectsend命令都是和使用spawn启动的新进程进行交互。
  • expect通常用来等待一个进程的反馈,我们根据进程的反馈,再使用send命令发送对应的交互命令。
  • send命令接收一个字符串参数,并将该参数发送到进程。
  • interact命令用的其实不是很多,一般情况下使用spawnexpectsend和命令就可以很好的完成我们的任务;但在一些特殊场合下还是需要使用interact命令的,interact命令主要用于退出自动化,进入人工交互。比如我们使用spawnsendexpect命令完成了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

1)下载依赖包

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自动输入密码

expect命令使用 - 简书

Linux技巧(五):expect安装与介绍 | 精美案例 | bash: spawn: command not found详解_血煞长虹的博客-CSDN博客

https://www.cnblogs.com/wangzhigang/p/11753447.html

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大宇进阶之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值