ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 使用expect脚本SCP拷贝文件

使用expect脚本SCP拷贝文件

原创 Linux操作系统 作者:germany006 时间:2013-07-18 13:05:44 0 删除 编辑
使用expect脚本SCP拷贝文件,无需输入密码,无需使用密钥对


#!/usr/bin/expect -f
foreach ip {
192.168.10.10} {
spawn scp $ip:/home/zwl_test/my_portal.sh /home/zwl_test/
set timeout 300
expect {
#"continue connecting" {send "yes\r";exp_continue}
"Password:" {send "xxxxxxxx\n"}
}
expect eof

}



注意事项(CENTOS7.6测试也没问题)yum -y install expect

vi expect.sh

#!/usr/bin/expect -f

foreach ip {                                                        --这里foreach ip {  中间都是有空格的,然后必须换行

192.168.1.100} {                                                --这里两个} { 之间必须要有空格,之后必须换行,否则会报错

spawn scp /root/install.log $ip:/BAK/bak/

expect {                                                              --这里必须换行,否则无法执行脚本成功

"password:" {send "******\n"}

}

expect eof

}


OK,以上的都按我写的格式来都写好之后, 在执行脚本的时候,必须./expect.sh这样执行,执行前chmod u+x expect.sh

千万不能用sh -x 或者sh 这种,这样执行不了的。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28371090/viewspace-766612/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
ORACLE,MYSQL,POSTGRESQL,SQLSERVER,MONGODB,TIDB,DB2,REDIS,本博客只为记录自己学习总结,不代表完全准确。

注册时间:2013-03-06

  • 博文量
    776
  • 访问量
    1038298