使用expect命令远程复制和登陆执行命令

我们公司有个场景,经常会有一大批主机需要修改密码,单个修改肯定是最笨的做法,因此,在自己的虚拟上面搭建环境测试自己写的脚本,主机的ip都是192.168.137网段的,主要是要有自己的想法

一 先生成随机密码并分别放到/tmp目录下面,有人说使用makpasswd生成随机密码也是可以的,百度一搜全是这个,我自己的想法是:

[root@localhost ~]# for aaa in `seq 15 30`;do echo -n $aaa" " >>hehe.txt;</dev/urandom tr -dc '[[:graph:]]'|head -c10 >>hehe.txt;echo -n " " >>hehe.txt;</dev/urandom tr -dc '[[:graph:]]'|head -c10 >>hehe.txt;echo -n " " >>hehe.txt;</dev/urandom tr -dc '[[:graph:]]'|head -c10 >>hehe.txt;echo -e " " >>hehe.txt;done;unset aaa
[root@localhost ~]# cat hehe.txt 
15 |#>&T<}.s( rDq'!n=ewX Hp~\h'S}95 
16 ~0??zMC,[b H?tHK)s7jM M\!~^}`}K= 
17 qc0i?}gY`+ hg3rN0*W,h =a}yH?-k4- 
18 1<'=0)w(,n %+:'6;1EGz 61Gq-'8o@" 
19 R,h6$hk?>O [(OI??ls{5 =RWe4gb{m7 
20 +N-V8Q@XzG )uz+7vi,|. T`D>3aRM<t 
21 =88R{l.1b< b-_oBo^GG7 n"r4BNl+d0 
22 DOWm->]co1 Wxrde6HoXv ^fV(JrDxX. 
23 m&LGWJ.Gki {4I(?MNJkY t5'!KK)~W4 
24 e-jMJQ(X^r S"qgVp&Bs! Rd(F3$9N"E 
25 ~_kb`|s1N5 qCMKq,h'1[ k2rgv*9Q@C 
26 xt"8qP0&7- #uwd3_Y-uf ",Zi(0BJO4 
27 !4@_C3,R2` !u+?.Nyude 9&NERXN)\k 
28 RQ%?}\P2OS Y7E6B`-^t< ]>t6x2_kg= 
29 )=[,+S=WGF &b\ouYqw4Y oa^n+Rx|C% 

30 H}j!]x|yJf |A-yfZS"3( tlpl^dzpBL

上面的空格不明显,在Linux是有空格的

[root@localhost ~]# for bbb in `seq 15 30`;do grep ^$bbb hehe.txt|awk '{print $2,$3,$4}'>/tmp/$bbb.txt;done;unset bbb  //分别放入/tmp目录下面

[root@localhost ~]# ls /tmp/*.txt
/tmp/15.txt  /tmp/18.txt  /tmp/21.txt  /tmp/24.txt  /tmp/27.txt  /tmp/30.txt
/tmp/16.txt  /tmp/19.txt  /tmp/22.txt  /tmp/25.txt  /tmp/28.txt

/tmp/17.txt  /tmp/20.txt  /tmp/23.txt  /tmp/26.txt  /tmp/29.txt

二 编写脚本

yum -y install expect

[root@localhost ~]# cat gaimi2.sh 
#!/usr/bin/expect  
set timeout 5  
set host [lindex $argv 0]
#将文件复制到远程服务器上面
spawn scp  /tmp/${host}.txt lisi@192.168.137.${host}:/home/lisi/  
 expect {  
 "(yes/no)?"  
  {  
    send "yes\n"  
    expect "*assword:" { send "123456\n"}  
  }  
 "*assword:"  
  {  
    send "123456\n"  
  }  
}  
expect "100%"
#登陆远程服务器执行命令
spawn ssh lisi@192.168.137.${host}
expect {
 "(yes/no)?"
  {
    send "yes\n"
    expect "*assword:" { send "123456\n"}
  }
 "*assword:"
  {
    send "123456\n"
  }
}


expect "*]$"
#切换root用户密码是123456
send "su - root \n"
expect "*assword:" { send "123456\n"}
#expect "*]#" 
#send {export aaa=`ip addr show|awk -F[./] '/137/{print $4}'`}
#send "\n"
expect "*]#"
send {awk '{print $1}' /home/lisi/`ip addr show|awk -F[./] '/137/{print $4}'`.txt |passwd --stdin sunli}
send "\n"
expect "*]#" 
send {awk '{print $2}' /home/lisi/`ip addr show|awk -F[./] '/137/{print $4}'`.txt |passwd --stdin zhangsan}
send "\n"
expect "*]#" 
send {awk '{print $3}' /home/lisi/`ip addr show|awk -F[./] '/137/{print $4}'`.txt |passwd --stdin wangwu}
send "\n"
expect "*]#" 
send {rm -f /home/lisi/`ip addr show|awk -F[./] '/137/{print $4}'`.txt}
send "\n"
expect "*]#"
expect eof

三执行脚本

for aaa in `seq 18 20`;do ./gaimi2.sh $aaa;done;unset aaa

四验证,一切OK,

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值