通过在shell脚本中用expect实现远程scp文件
2011-11-15 00:00
966 查看
通过在shell脚本中用expect实现远程scp文件:
使用expect前,需要先安装两个rpm包:
# rpm -ihv CentOS/expect-5.43.0-5.1.i386.rpm
# rpm -ihv CentOS/expect-devel-5.43.0-5.1.i386.rpm
#!/usr/bin/expect -f
set password 密码
spawn scp 用户名@目标机器ip:拷贝文件的路径 存放本地文件的路径
set timeout 300
expect "用户名@目标机器ip's password:" //注意:这里的“用户名@目标机器ip” 跟上面的一致
set timeout 300
send "$password\r"
set timeout 300 //此处设置超时时间,单位为秒,如果,拷贝文件比较大并且多时,应该将这个值调大一些。
send "exit\r"
expect eof
附:scp参数
-r:拷贝目录
-c:允许压缩
一个完整的例子
expect_scp.exp
========================================================================
==========================================================================
测试验证:
# chmod +x ./expect_scp.exp
# expect ./expect_scp.exp
spawn scp -r root@192.168.1.107:/root/dba_tool/test /root/DBA_TOOL/
root@192.168.1.107's password:
test 100% 12 0.0KB/s 00:00
spawn scp -r /root/DBA_TOOL/t.txt root@192.168.1.107:/root/dba_tool/
root@192.168.1.107's password:
t.txt 100% 254 0.3KB/s 00:00
本地服务器:(192.168.1.106)
[root@F-app-01 DBA_TOOL]# ll /root/DBA_TOOL/
total 64
-rw-r--r-- 1 root root 548 Nov 14 23:46 expect_scp.exp
-rw-r--r-- 1 root root 12 Nov 14 23:24 test
-rw-r--r-- 1 root root 254 Nov 14 23:24 t.txt
远程服务器:(192.168.1.107)
[root@F-app-02 dba_tool]# ll /root/dba_tool/
total 60
-rw-r--r-- 1 root root 12 Nov 14 22:24 test
-rw-r--r-- 1 root root 254 Nov 14 22:47 t.txt
使用expect前,需要先安装两个rpm包:
# rpm -ihv CentOS/expect-5.43.0-5.1.i386.rpm
# rpm -ihv CentOS/expect-devel-5.43.0-5.1.i386.rpm
#!/usr/bin/expect -f
set password 密码
spawn scp 用户名@目标机器ip:拷贝文件的路径 存放本地文件的路径
set timeout 300
expect "用户名@目标机器ip's password:" //注意:这里的“用户名@目标机器ip” 跟上面的一致
set timeout 300
send "$password\r"
set timeout 300 //此处设置超时时间,单位为秒,如果,拷贝文件比较大并且多时,应该将这个值调大一些。
send "exit\r"
expect eof
附:scp参数
-r:拷贝目录
-c:允许压缩
一个完整的例子
expect_scp.exp
========================================================================
#!/usr/bin/expect -f #Author by kevin #date is 2011-11-14 set password vcdog #download local host spawn scp -r root@192.168.1.107:/root/dba_tool/test /root/DBA_TOOL/ set timeout 3 expect { "yes/no" {send "yes\r";exp_continue} } expect root@192.168.1.107's password: set timeout 3 send "$password\r" set timeout 300 send "exit\r" expect eof #upload remote host spawn scp -r /root/DBA_TOOL/t.txt root@192.168.1.107:/root/dba_tool/ set timeout 3 expect { "yes/no" {send "yes\r";exp_continue} } expect root@192.168.1.107's password: set timeout 3 send "$password\r" set timeout 300 send "exit\r" expect eof |
测试验证:
# chmod +x ./expect_scp.exp
# expect ./expect_scp.exp
spawn scp -r root@192.168.1.107:/root/dba_tool/test /root/DBA_TOOL/
root@192.168.1.107's password:
test 100% 12 0.0KB/s 00:00
spawn scp -r /root/DBA_TOOL/t.txt root@192.168.1.107:/root/dba_tool/
root@192.168.1.107's password:
t.txt 100% 254 0.3KB/s 00:00
本地服务器:(192.168.1.106)
[root@F-app-01 DBA_TOOL]# ll /root/DBA_TOOL/
total 64
-rw-r--r-- 1 root root 548 Nov 14 23:46 expect_scp.exp
-rw-r--r-- 1 root root 12 Nov 14 23:24 test
-rw-r--r-- 1 root root 254 Nov 14 23:24 t.txt
远程服务器:(192.168.1.107)
[root@F-app-02 dba_tool]# ll /root/dba_tool/
total 60
-rw-r--r-- 1 root root 12 Nov 14 22:24 test
-rw-r--r-- 1 root root 254 Nov 14 22:47 t.txt
相关文章推荐
- 通过在shell脚本中用expect实现远程scp文件
- 通过在shell脚本中用scp或rsync实现远程同步文件
- shell脚本中用expect实现远程scp文件
- CentOS 7 下 通过shell + expect 实现 scp 文件(目录)传输
- 在shell脚本中用expect 实现 scp 文件的时候不手动输入密码
- 在shell脚本中用expect 实现 scp 文件的时候不手动输入密码
- 在shell脚本中用expect 实现 scp 文件的时候不手动输入密码
- 通过 Shell 和 Expect 脚本实现对 IBM 刀片服务器高级管理模块固件的大量升级
- shell脚本实现scp上传(expect交互),ftp上传
- scp远程拷贝expect实现SHELL
- linux脚本自动化实现远程登陆操作和传输文件(scp ssh expect)
- linux shell脚本嵌套expect 实现远程ssh登入,传送文件
- Java实现远程ssh执行shell脚本对文件打包下载
- 利用expect在linux shell脚本中交互(scp远程传输文件示例)
- 通过shell脚本实现从文件中读取数据的几种方法
- ssh,expect脚本实现远程登录,拷贝命令scp,rsync服务器的搭建
- shell脚本通过expect实现自动单边无密登录功能
- shell脚本实现scp文件传输
- shell脚本之通过发送带\n字符串或expect脚本实现交互输入自动化
- linux 用expect脚本实现scp服务器之间的文件备份