手机
当前位置:查字典教程网 >脚本专栏 >linuxshell >expect实现单台、多台服务器批量scp传输文件
expect实现单台、多台服务器批量scp传输文件
摘要:今天公司同事要在一台服务器上对另外两台同机房的服务器进行文件传输,两台服务器的文件存放地址一致,密码和key也一致(我们公司在安全上做得有点...

今天公司同事要在一台服务器上对另外两台同机房的服务器进行文件传输,两台服务器的文件存放地址一致,密码和key也一致(我们公司在安全上做得有点变态的),为了效率写了下面这个脚本.

1.单台传输脚本

cat /root/soft_shell/allscp.sh

复制代码 代码如下:

#!/usr/bin/expect

if {$argc < 2} {

send_user "usage: $argv0 src_file username ip dest_file passwordn"

exit

}

##set key [lindex $argv 0]

set src_file [lindex $argv 0]

set username [lindex $argv 1]

set host_ip [lindex $argv 2]

set dest_file [lindex $argv 3]

set password [lindex $argv 4]

##spawn scp -i $key $src_file $username@$host_ip:$dest_file

spawn scp $src_file $username@$host_ip:$dest_file

expect {

"(yes/no)"

{

send "yesn"

expect "password:" {send "$passwordn"}

}

"password:"

{

send "$passwordn"

}

}

expect "100%"

expect eof

2.多台传输脚本

cat /root/soft_shell/mainscp.sh

复制代码 代码如下:

#!/bin/bash

host_list="server_list.conf"

cat $host_list | while read line

do

host_ip=`echo $line|awk '{print $1}'`

username=`echo $line|awk '{print $2}'`

password=`echo $line|awk '{print $3}'`

src_file=`echo $line|awk '{print $4}'`

dest_file=`echo $line|awk '{print $5}'`

##key=`echo $line|awk '{print $6}'`

##./allscp.sh $key $src_file $username $host_ip $dest_file $password

./allscp.sh $src_file $username $host_ip $dest_file $password

done

3.服务器信息文件

复制代码 代码如下:

cat /root/soft_shell/server_list.conf

格式为:

复制代码 代码如下:

ip 用户名 密码 源文件 目标文件地址

ps:以上3个文件,相信大家都看出来了,都是放在同一文件夹下面的.我本地测试只用ssh密码,没有加上ssh key,如果要用上跟我们公司正式环境一样的安全方式(ssh密码+key,才能登录服务器),那么请自己修改脚本文件,我比较懒这里就不说得那么详细了.

最后来张效果图吧,免得大家说我脚本有问题.

expect实现单台、多台服务器批量scp传输文件1

【expect实现单台、多台服务器批量scp传输文件】相关文章:

Shell脚本实现批量下载网络图片代码分享

Linux中使用expect脚本实现远程机器自动登录

使用scp获取远程linux服务器上的文件 linux远程拷贝文件

Shell脚本实现线上服务器之间对比文件是否一致

Shell中使用plink工具实现远程批量关机

Shell脚本实现自动输入密码登录服务器

Shell中实现“多线程”执行脚本文件完美解决方案

shell脚本从SVN推送到多台服务器的代码

shell结合expect写的批量scp脚本工具

Shell脚本实现批量生成nagios配置文件

精品推荐
分类导航