linux expect 批量修改主机密码

ip.txt

192.168.0.105 123455
192.168.0.106 123456
192.168.0.107 123457
192.168.0.108 123458

passwd.sh

#!/bin/bash

for ip in `awk {print $1} ip.txt`
do
  pass=`grep $ip ip.txt |awk {print $2}`
  echo $ip $pass
  expect action.exp $ip $pass
done

action.exp

#!/bin/expect
set ip [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "$passwd\r"}
}
expect "#"
send "echo 408950 |passwd --stdin root\r"
send "exit\r"
expect eof

使用

[root@localhost ~]# sh passwd.sh
192.168.0.105 123456
spawn ssh root@192.168.0.105
root@192.168.0.105s password:
Last login: Tue Jul  7 22:37:38 2020 from 192.168.0.104
[root@localhost ~]# echo 123456 |passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# exit
logout
Connection to 192.168.0.105 closed.

 

linux expect 批量修改主机密码

上一篇:sql 计算两时间或日期 的相差的 年、 月、 日、时、分、秒,年、月、日分别的提取


下一篇:DCIC-早高峰共享单车潮汐点的群智优化(1)