day 11 分发系统

[root@iZwz96qzfgxh9l2rk7esxnZ ~]# fg
vim 1.expect
#远程登陆
#!/usr/bin/expect
set host ""
set passwd ""
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact
#远程登陆并执行命令
#!/usr/bin/expect
set user ""
set passwd ""
spawn ssh $user@
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/1.txt\r"
expect "]*"
send "echo 11111 > /tmp/1.txt\r"
expect "]*"
send "exit\r"

~                                                                                                                                       
~       

 

#传递参数
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd ""
set cm [lindex $argv 2]
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

~                                                                                                                                       
~                                                                                                                                       
~                     
#同步文件
#!/usr/bin/expect
set passwd ""
spawn rsync -av root@  :/tmp/1.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

shell项目-分发系统-构建文件分发系统

核心命令rsync -av --files-from=list.txt  /  root@host:/

[root@iZwz96qzfgxh9l2rk7esxnZ expect]# vim rsync.expect
#!/usr/bin/expect
set passwd ""
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
~      

#遍历ip.list选择需要同步的IP,遍历list.txt选择需要同步的文件
[root@iZwz96qzfgxh9l2rk7esxnZ expect]# vim rsync.sh
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done
          

shell项目-分发系统-命令批量执行

[root@iZwz96qzfgxh9l2rk7esxnZ expect]# vim exe.expect

#!/usr/bin/expect
set host [lindex $argv 0]
set passwd ""
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
#遍历文件批量执行命令
[root@iZwz96qzfgxh9l2rk7esxnZ expect]# vim exe.sh

#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done

 

转载于:https://www.cnblogs.com/huzhendong/p/10198245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值