shell的分发系统的实现

expect的应用:

  yum install -y expect
  自动远程登录
 #! /usr/bin/expect
set host "192.168.88.129"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact

1.第一个远程登陆的脚本  vim  /usr/local/sbin/1.expect
[root@aming01 sbin]# cat 1.expect
#!/usr/bin/expect
set host "192.168.88.129"
set  passwd "1234567"                        //这里暂时设定要登陆的机器密码为1234567。实际环境,视具体情况吧
spawn  ssh  root@$host
expect {
"yes/no" { send "yes\r"; exp_continue }        
"password:" { send "$passwd\r" }
}
interact
[root@aming01 sbin]# ./1.expect        //执行脚本  直接登陆到了aming02   192.168.88.129这台机器
spawn ssh root@192.168.88.129
root@192.168.88.129's password:
Last login: Tue Jun 18 10:54:41 2019 from 192.168.88.1
[root@aming02 ~]#

4b6278d7e3e92b4028144b50412564bed69.jpg

15635d09fa47208817f582130af02923854.jpg

 

2.自动远程登录后,执行命令并退出

73ce5796871254d3a76d71a3c5303331b18.jpg

b4c11b4bf62f8b558084d1c21c8d05638ee.jpg

[root@aming02 ~]# [root@aming01 sbin]# cat 2.expect
#!/usr/bin/expect
set host "192.168.88.129"
set  passwd "1234567"
spawn  ssh  root@$host
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$passwd\r" }
}
expect  "]*"
send "touch /tmp12.txt\r"
expect "]*"
send "echo test1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"
[root@aming01 sbin]#

3.  传递参数  vim  3.expect  

32b5e5938387ecbee6c4a9161ac5d6b2bf0.jpg

[root@aming01 sbin]# cat 3.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "1234567"
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@aming01 sbin]#

 

4. 自动同步文件

ddc2f5069a7475c18931b66d1b99d00d077.jpg

[root@aming01 sbin]# cat 4.expect
#!/usr/bin/expect
set passwd "1234567"
spawn rsync -av root@192.168.88.129:/tmp/12.txt  /tmp/
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect eof
[root@aming01 sbin]#

5.指定host和要同步的文件

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

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

 需求背景 对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
 实现思路 首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
 核心命令 rsync -av --files-from=list.txt  /  root@host:/

 文件分发系统的实现
 rsync.expect 内容
#!/usr/bin/expect
set passwd "1234567"
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内容
192.168.88.129
192.168.88.130
......

 rsync.sh 内容
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done

 

 

 

 

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

脚本的文件如下  cat /usr/local/sbin/rsync.sh    rsync.expect   

[root@aming01 sbin]# cat rsync.sh
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
 echo $ip
./rsync.expect $ip  /tmp/file.list
done
[root@aming01 sbin]#
[root@aming01 sbin]# cat rsync.expect
#!/usr/bin/expect
set passwd "1234567"
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

[root@aming01 sbin]#

指定同步的目录和主机ip

[root@aming01 sbin]# cat /tmp/file.list      //指定要同步的目录文件
/tmp/12.txt
/root/1.sh
/root/111/112222.txt
[root@aming01 sbin]# cat /tmp/ip.list      //指定要同步到那些主机
192.168.88.129
192.168.88.130
127.0.0.1
[root@aming01 sbin]#

执行脚本rsync.sh
[root@aming01 sbin]# sh rsync.sh
192.168.88.129
spawn rsync -av --files-from=/tmp/file.list / root@192.168.88.129:/
root@192.168.88.129's password:
building file list ... done
tmp/

sent 190 bytes  received 15 bytes  410.00 bytes/sec
total size is 40  speedup is 0.20
192.168.88.130
spawn rsync -av --files-from=/tmp/file.list / root@192.168.88.130:/
root@192.168.88.130's password:
building file list ... done
tmp/

sent 190 bytes  received 15 bytes  136.67 bytes/sec
total size is 40  speedup is 0.20
127.0.0.1
spawn rsync -av --files-from=/tmp/file.list / root@127.0.0.1:/
root@127.0.0.1's password:
building file list ... done

sent 187 bytes  received 12 bytes  398.00 bytes/sec
total size is 40  speedup is 0.20
[root@aming01 sbin]#

 

8分发系统的批量执行命令功能:

[root@aming01 sbin]# cat exe.sh
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
 ./exp.expect $ip "hostname"
done
[root@aming01 sbin]#

[root@aming01 sbin]# cat exe.sh
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
 ./exp.expect $ip "hostname"
done
[root@aming01 sbin]# cat exp.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "1234567"
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@aming01 sbin]#

[root@aming01 sbin]# cat /tmp/ip.list
192.168.88.129
192.168.88.130
127.0.0.1
[root@aming01 sbin]#

执行exe.sh脚本:

[root@aming01 sbin]# sh exe.sh
spawn ssh root@192.168.88.129
root@192.168.88.129's password:
Last login: Wed Jun 19 09:15:21 2019 from 192.168.88.1
[root@aming02 ~]# hostname
aming02
[root@aming02 ~]# spawn ssh root@192.168.88.130
root@192.168.88.130's password:
Last failed login: Wed Jun 19 09:28:09 CST 2019 from 192.168.88.128 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Wed Jun 19 09:27:38 2019 from 192.168.88.1
[root@aming03 ~]# hostname
aming03
[root@aming03 ~]# spawn ssh root@127.0.0.1
root@127.0.0.1's password:
Last failed login: Wed Jun 19 09:26:09 CST 2019 from 127.0.0.1 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Wed Jun 19 09:15:21 2019 from 192.168.88.1
[root@aming01 ~]# hostname
aming01
[root@aming01 ~]# [root@aming01 sbin]#

 

 

 

脚本练习:

1.编写shell脚本,计算1-200的和

[root@aming01 test]# cat 1.sh
#!/bin/bash
sum=0
for i in {1..200}
 do
  sum=$[$sum+$i]
 done
echo $sum
[root@aming01 test]#

2. 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;

[root@aming01 test]# cat 2.sh
#!/bin/bash
sum=0
read -p "please input you number:  " n
echo $n
if [ $n -lt 1 ];then
 read -p  "please enter the numbe>1   " n
fi
if [ $n -eq 1 ];then
 read -p "please enter the number >1  " n
fi

if [ $n -gt 1 ];then
 for i in `seq 1 $n`
  do
  sum=$[$sum+$i]
  done
fi
echo “1+2+....$n=”$sum

[root@aming01 test]#

3. 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下;

find /root/ -type d -maxdepth 1 |xargs -i mv {} /tmp/      //个人想法

参考的答案

3. #! /bin/bash

cd /root
for f in `ls `; do
        if [ -d $f ] ; then
                cp -r $f /tmp/
        fi
done

4,编写shell脚本,批量建立用户user_00, user_01, ... user_100并且所有用户同属于users组; 并把用户名和密码写到/tmp/user.conf文件里面

[root@aming01 test]# cat 4.sh
#!/bin/bash
t=user
for i in `seq -w 00  100 `
do
 useradd $t$i -g users
 pass=`echo $RANDOM|md5sum|sed 's/[A-Z]/[a-z]/g'|cut -c 1-10`
 echo $pass|passwd --stdin $t$i
 echo "$t$i    $pass" >> /tmp/user.conf
done
[root@aming01 test]#

参考答案:

[root@aming01 test]# cat 4.sh
#! /bin/bash
groupadd users
for i in `seq 0 9`; do
        useradd -g users user_0$i
done

for j in `seq 10 100`; do
        useradd -g users user_$j
done
[root@aming01 test]#

5. 编写shell脚本,截取文件test.log中包含关键词 ‘abc’ 的行中的第一列(假设分隔符为 ”:” ),然后把截取的数字排序(假设第一列为数字),然后打印出重复次数超过10次的列;

[root@aming01 test]# cat 5.sh    //参考答案
awk -F ':' '$0~/abc/ {print $1}' test.log  > /tmp/n.txt
sort -n n.txt |uniq -c |sort -n >/tmp/n2.txt
awk '$1>10 {print $2}' /tmp/n2.txt
[root@aming01 test]#

6. 编写shell脚本,判断输入的IP是否正确(IP的规则是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255, 0<n3<255, 0<n4<255)

[root@aming01 test]# cat 6.sh
#!/bin/bash
checkip() {
if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ; then
    a=`echo $1 |awk -F. '{print $1}'`
    b=`echo $1 |awk -F. '{print $2}'`
    c=`echo $1 |awk -F. '{print $3}'`
    d=`echo $1 |awk -F. '{print $4}'`
    
    for n in $a $b $c $d ;do
       if [ $n -ge 255 ] || [ $n -le 0 ];then
         echo "please enter the number between 0 and 255 "
          return 2
        fi
      done
   else
    echo "The IP format must be 192.168.100.1"
   return 1
fi
}
 
rs=1
while [ $rs -gt 0 ];do
  read -p "Please input the ip: " ip
  checkip $ip
  rs=`echo $?`
done
echo "The IP  is right!"
[root@aming01 test]#

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3964315/blog/3063625

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值