CentOS下构建Shell简易分发系统

bash经典收集

经典收集1

for f in `(cd .; find suite -type f | grep -v SCCS)`; \

    do \

     d=/usr/local/mysql/mysql-test/`dirname $f`; \

     mkdir -p $d ; \

     /usr/bin/install -c -m 644 ./$f $d ; \

    done

test -z "/usr/local/mysql/mysql-test" || mkdir -p -- "/usr/local/mysql/mysql-test"

 

 

expect

自动登录expect脚本

 

 

[root@nginx sbin]# cat 1.expect

#!/usr/bin/expect

set host "192.168.211.135"

set passwd "root123"

spawn ssh root@$host

expect {

"yes/no" { send "yes\r";exp_continue }

"assword:" { send "$passwd\r" }

}

interact

[root@nginx sbin]#

自动登录后执行命令

 

[root@nginx sbin]# cat 2.expect

#!/usr/bin/expect

set user "root"

set host "192.168.211.135"

set passwd "root123"

spawn ssh $user@$host

expect {

"yes/no" { send "yes\r";exp_continue }

"assword:" { send "$passwd\r" }

}

expect "]*"

send "touch /tmp/12.ext\r"

expect "]*"

send "echo 1212 >/tmp/12.txt\r"

expect "]*"

send "exit\r"

#interact

[root@nginx sbin]#

 

 

 

expect脚本传递参数

 

[root@nginx sbin]# cat 3.expect

#!/usr/bin/expect

set user [lindex $argv 0]

set host [lindex $argv 1]

set passwd "root123"

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"

[root@nginx sbin]#

 

 

[root@nginx sbin]# ./3.expect root 192.168.211.135 "ls /root/"

 

 

自动同步文件脚本

[root@nginx sbin]# cat 4.expect

#!/usr/bin/expect

set passwd "root123"

spawn rsync -av root@192.168.211.135:/tmp/12.txt /tmp/

expect {

"yes/no" { send "yes\r" }

"password:" { send "$passwd\r" }

}

expect eof

[root@nginx sbin]#

 

 

构建简易文件分发系统

适用于有多台机器需要同时更新文件

[root@nginx sbin]# cat rsync.expect

#!/usr/bin/expect

set passwd "root123"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av $file root@$host:/

expect {

"yes/no" { send "yes\r" }

"password:" { send "$passwd\r" }

}

expect eof

[root@nginx sbin]#

 

 

[root@nginx sbin]# cat ip.list

192.168.211.135

192.168.211.137

[root@nginx sbin]#

 

 

 

 

[root@nginx sbin]# cat rsync.sh

#!/bin/bash

for ip in `cat ip.list`

do

echo $ip

[ -x rsync.expect ] || chmod a+x rsync.expect

./rsync.expect $ip ip.list

done

[root@nginx sbin]#

 

 

命令批量执行脚本

适用于有多台机器需要批量执行命令

 

[root@nginx sbin]# cat exe.expect

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "root123"

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@nginx sbin]#

 

[root@nginx sbin]# cat ip.list

192.168.211.135

192.168.211.137

[root@nginx sbin]#

 

 

 

[root@nginx sbin]# cat exe.sh

#!/bin/bash

for ip in `cat ip.list`

do

echo $ip

[ -x exe.expect ] || chmod a+x exe.expect

./exe.expect $ip "w;free -m;ls /tmp"

done

[root@nginx sbin]#

 

posted on 2016-11-02 10:52 8年扛枪梦 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/appresearch/p/6022094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值