linux 创建用户你会? 哪远程批量创建用户呢?
批量创建用户脚本简单示例
#!/bin/bash if [ -z "$1" -o -z "$2" ];then #从USES、PASSWD文件读取用户和密码,限检测参数是否存在 echo " Uages: userfile passfile EXAMPLES: urctl userfile passfile ps: Must do it by this way ... " exit fi USERFILE=$1 PASSFILE=$2 UNUM=`cat $USERFILE | wc -w` PNUM=`cat $PASSFILE | wc -w` if [ $UNUM != $PNUM ];then #获取USES和PASSWD行数,如果不匹配就退出 echo user Number info not match password Nubmer info exit fi for(( un=1; un<=$UNUM ; un++ )) #循环创建用户 do NAME=`sed -n $un"p" $USERFILE` grep $NAME /etc/passwd &> /dev/null && echo User: $NAME is exist create filed || { #判断用户是否存在,不存在则创建,否则跳过 PASS=`sed -n $un"p" $PASSFILE` for IP in 172.25.254.{71,72} #批量 创建用户 do ping -c1 -w1 $IP &> /dev/null && ( #以root身份登陆 redhat@IP ,执行 useradd. ./batch root $IP redhat "useradd $NAME -p $PASS" #batch 是 expect自动应答脚本 ) done } done
cat ./batch
#!/usr/bin/expect set USER [ lindex $argv 0 ] #参数传给变量 set IP [ lindex $argv 1 ] set PASS [ lindex $argv 2 ] set COMM [ lindex $argv 3 ] spawn ssh $USER@$IP ($COMM) #执行命令 sleep 1 expect { "yes/no" #匹配yes/no { send "yes\r"; exp_continue } #回答 yes "password" { send "$PASS\r";} } interact
原文地址:http://12053820.blog.51cto.com/12043820/1856051