在shell脚本中使用expect实现scp传输问题
2016-11-09 21:41
746 查看
1.安装expect
expect用于shell脚本中自动交互,其是基于tcl编程语言的工具。所以安装expect首先安装tcl。本文中使用的是expect5.45和tcl8.6.6。安装tcl
[root@tseg0 /]$ mkdir /tools [root@tseg0 /]$ tar -zxvf tcl8.6.6-src.tar.gz [root@tseg0 /]$ cd tcl8.6.6/unix/ [root@tseg0 /]$ ./configure [root@tseg0 /]$ make [root@tseg0 /]$ make install
安装expect
[root@tseg0 /]$ cd /tools [root@tseg0 /]$ tar -zxvf expect5.45.tar.gz [root@tseg0 /]$ cd expect5.45/ [root@tseg0 /]$ ./configure --with-tcl=/usr/local/lib/ --with-tcl include=/tools/tcl8.6.6/generic/ [root@tseg0 /]$ make [root@tseg0 /]$ make install
shell脚本实现scp传输
命令解释
-c 表示可以在命令行下执行except脚本;spawn 命令激活一个unix程序来交互,就是在之后要执行的命令;
expect “aaa” 表示程序在等待这个aaa的字符串;
send 向程序发送字符串,expect和send经常是成对出现的,比如当expect“aaa”的时候,send“bbb”。
执行脚本
#! /bin/sh expect -c " spawn scp -r /home/tseg/hello $name@10.103.240.33:/home/$name/ expect { \"*assword\" {set timeout 300; send \"$pass\r\"; exp_continue;} \"yes/no\" {send \"yes\r\";} } expect eof"
解释:
第二行: -c 表示可以不用与控制台交互;
第三行:spawn激活一个scp的unix程序;
第五行:expect期待含有“assword”的字符串,设置连接时间最大为300毫秒,如果出现这个字符串,就send 变量pass代表的密码字符串, exp_continue表示执行下面的匹配;
第六航:expect期待含有“assword”的字符串,设置连接时间最大为300毫秒,如果出现这个字符串,就send 变量pass代表的密码字符串;
第八行:表示结束。
相关文章推荐
- 使用expect实现shell脚本中scp,ssh自动输入口令
- 使用expect脚本实现scp远程传输
- linux使用shell脚本实现自动登录scp传输文件到其他服务器
- 通过在shell脚本中用expect实现远程scp文件
- shell脚本实现scp上传(expect交互),ftp上传
- 通过scp命令下载备份文件的shell脚本实现——使用sshpass
- shell脚本实现scp文件传输
- 在shell脚本中用expect 实现 scp 文件的时候不手动输入密码
- 在shell脚本中用expect 实现 scp 文件的时候不手动输入密码
- shell脚本通过expect脚本实现自动输入密码(使用expect)
- 通过在shell脚本中用expect实现远程scp文件
- shell脚本中用expect实现远程scp文件
- ssh、scp的使用,以及shell脚本解决scp需要输入密码的问题
- linux,scp传输文件用expect工具免密码输入shell脚本
- 在shell脚本中用expect 实现 scp 文件的时候不手动输入密码
- linux脚本自动化实现远程登陆操作和传输文件(scp ssh expect)
- linux下使用expect+scp+shell实现分布式集群系统安装,升级,部署
- Linux 使用expect工具和scp命令实现自动复制传输文件
- CentOS 7 下 通过shell + expect 实现 scp 文件(目录)传输
- 使用FUNCNAME 和BASH_LINENO实现shell脚本中定位函数错误在代码中的位置