expect 批量增加用户及配置密码

start.sh

#!/bin/bash

password="111111"
username="test"

if [ $# == 1 ]
then
    username=$1
elif [ $# == 2 ]
then
    username=$1
    password=$2
else
    echo "USAGE:$0 USERNAME"
    echo "USAGE:$0 USERNAME PASSWORD"
    exit 1 
fi

echo "Add UserName:"$username
echo "Add PassWord:"$password

for i in 2 3 4 5 6
do
    if [ -f "add.sh" ]
    then
        ./add.sh ${i} $username $password
    else
        echo "Error:add.sh 脚本不存在"
        exit 1
    fi
done

 

add.sh

#!/usr/bin/expect -f
set timeout 30
set node [lindex $argv 0]
set name [lindex $argv 1]
set pawd [lindex $argv 2]

spawn ssh root@node${node} "useradd $name;passwd $name"
expect "New password:"
send "${pawd}\r"
expect "Retype new password:"
send "${pawd}\r"
expect "#"

 注意:

  1.如果Linux上面,没有安装expect,请先安装expect

  2.在ssh各个服务器的时候,由于我已经配置了免密码登陆,所以不会出现如下几条语句,如果出现一下语句:

  Are you sure you want to continue connecting (yes/no)?

  root@node9's password:

  如果出现,请自行用expect捕获

posted @ 2015-11-19 10:59  LiuWei_Find  阅读(426)  评论(0编辑  收藏  举报