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