国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Linux Shell > 正文

expect實現單臺、多臺服務器批量scp傳輸文件

2020-07-27 19:09:24
字體:
來源:轉載
供稿:網友

今天公司同事要在一臺服務器上對另外兩臺同機房的服務器進行文件傳輸,兩臺服務器的文件存放地址一致,密碼和key也一致(我們公司在安全上做得有點變態的),為了效率寫了下面這個腳本.

1.單臺傳輸腳本

cat /root/soft_shell/allscp.sh

復制代碼 代碼如下:

#!/usr/bin/expect
if {$argc < 2} {
        send_user "usage: $argv0 src_file username ip dest_file password/n"
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 "yes/n"
                        expect "password:" {send "$password/n"}
                }
        "password:"
                {
                        send "$password/n"
                }
}
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,才能登錄服務器),那么請自己修改腳本文件,我比較懶這里就不說得那么詳細了.

最后來張效果圖吧,免得大家說我腳本有問題.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 家居| 沧州市| 类乌齐县| 华容县| 育儿| 开阳县| 乐昌市| 莲花县| 辽中县| 青龙| 赤壁市| 治多县| 出国| 阿坝| 宽城| 合阳县| 江华| 和硕县| 东城区| 栾川县| 碌曲县| 晋中市| 崇文区| 鹤山市| 兰考县| 林口县| 海淀区| 德州市| 涿州市| 新龙县| 安吉县| 阿拉尔市| 界首市| 汉沽区| 太康县| 罗源县| 临高县| 嘉峪关市| 石渠县| 云浮市| 永德县|