linux自动交互,批量修改密码和批量传文件

 

先安装expect

sudo yum install expect

 

批量修改密码

#!/bin/bash
SERVERS=`cat ip_list.txt`
OLDPASS='****'
NEWPASS='888'

for ip in $SERVERS       
do
    expect << EOF
    spawn ssh $ip "echo $NEWPASS | passwd --stdin root"
    expect "(yes/no)?" {
    send "yes\r"
    expect "password:"
    send "$OLDPASS\r"
    } "password:" {send "$OLDPASS\r"}
    expect eof
EOF
done

 

批量传文件

#!/bin/bash
SERVERS=`cat ip_list.txt`
PASSWORD="123456"

auto_ssh_copy_hosts(){
    expect -c "set timeout -1;
        spawn scp /etc/hosts $1:/etc/hosts;
        expect {
            *(yes/no)* {send -- yes\r;exp_continue;}
            *password* {send -- $2\r;exp_continue;}
            eof        {exit 0;}   
        }";
}

for SERVER in $SERVERS
do
    auto_ssh_copy_hosts $SERVER $PASSWORD
done   

您可以选择一种方式赞助本站