学习笔记之简单的自动部署用户的脚本

来源:互联网 发布:命名实体识别算法 编辑:程序博客网 时间:2024/06/06 04:36

脚本作用:

       首先输入脚本。后面跟两个参数。第一个必须是要部署的用户名称,第二个必须是要部署的用户密码。否则会报错的。当没有报错时会提醒输入一个存放着要部署主机IP和密码的文件。输入文件后就成功在相应的主机上创建用户


1.第一个参数 user_file (存放着要创建的用户)

2.第二个参数 passwd_file(存放着要创建的用户的密码)

3.还有一个文件在脚本运行其前输入ip_file(要部署的主机IP和登陆密码)

里面我故意写了一些错的。


执行一下(在文件都有的情况下)


执行后提示 输入 ip_file的文件地址。

然后判断目标主机是否开机,开机的话会登陆,并判断主机内是否存在要添加的用户,有的话跳过,没有的话就添加。

同时如果主机密码错误会报错,如果ip_file中格式ip不是ip形式的话,也会提示ERROR IP 的。


下面演示一下各种错误的报错

1.只输入一个文件参数

[root@www test]# ./auto_adduser.sh   user_file
SYNTAX ERROR:Must hava two files
Frist file is user_file,The Second is passwd_file

2.输入文件不存在

[root@www test]# ./auto_adduser.sh   user_file   eee
One of Two files isn't exist
Please Check

3.输入的IP_file不存在


好了,上脚本吧。


#!/bin/bash############################################################## File Name:auto_adduser.sh# Author:Nicolas Cage# mail:4540xxxxxx@qq.com# Created Time: Mon 12 Dec 2016 09:05:45 PM CST#====================================================#tty_init=`tput sgr0`function error_do1(){echo "SYNTAX ERROR:Must hava two files"echo "Frist file is user_file,The Second is passwd_file"exit 1}function error_do2(){echo "One of Two files isn't exist"echo "Please Check"exit 2}function error_do3(){echo "The user_num is not equal passwd_num"exit 3}function error_do4(){echo -e '\e[31m' "------->The user '$1' is already exist on $2" "${tty_init}"}function check_ip(){res=`echo $1|awk -F "." '($1>0 && $1<255) && ($2>0 && $2<255) && ($3>0 && $3<255) && ($4>0 && $4<255) {print "OK"}'`if [[ $res == "OK" ]];thenreturn 0elseecho -e '\e[6;1;41m' "ERROR IP $1 ,Please Check" "${tty_init}"return 1fi}function add_user(){num=1while read user;dopasswd=`sed -n ${num}p $2`exist_user=`/root/test_xuexi/test/ssh.exp $3 $4 "cut -d\":\" -f1 /etc/passwd|grep ${user}"|grep -vE 'spawn|password:'`tmp=`echo ${exist_user}|grep again`if [[ -n ${tmp} ]];thenecho "\#\#\#The passwd of $3 Mybe error\#\#\#"echo "\#\#\#Please Check                         \#\#\#"let num=$num+1continueelseif [[ -z ${exist_user} ]];then/root/test_xuexi/test/ssh.exp $3 $4 "useradd ${user}" &> /dev/null /root/test_xuexi/test/ssh.exp $3 $4 "echo $passwd|passwd --stdin ${user}" &>/dev/nullecho -e '\e[32m' "------->The user '$user' add successfully on $3" "${tty_init}"elseerror_do4 ${user} $3fifilet num=$num+1done < $1}test $# -ne 2 && error_do1test -e $1 -a -e $2 || error_do2user_num=`wc -l $1|cut -d" " -f 1`passwd_num=`wc -l $2|cut -d" " -f 1`test $user_num -ne $passwd_num && error_do3while true;doread -p "please input The derictory of ip_file: " ip_fileif [ -f ${ip_file} ];thenbreakelseecho -e '\e[6;1;41m'"ERROR:The file  '${ip_file}' is invalid" "${tty_init}"echo "Please Try again"fidonewhile read ip ip_passwd;docheck_ip $ipif [[ $? -eq 0 ]];thenping -c1 -w1 $ip &>/dev/nullif [[ $? -eq 0 ]];thenecho -e '\e[32m' "$ip is up" ${tty_init}add_user $1 $2 ${ip} ${ip_passwd}elseecho -e '\e[31m' "$ip is down" ${tty_init}fielsecontinuefidone < ${ip_file}


脚本中调用了一个其他脚本,是自动ssh的,expect脚本 ssh.exp

#!/usr/bin/expectset ip [lindex $argv 0]set passwd [lindex $argv 1]set cmd [lindex $argv 2]spawn /usr/bin/ssh  root@${ip} ${cmd} expect {        "connecting (yes/no)?"  {                send "yes\r"                expect "password:"                send "${passwd}\r"                expect eof         }        "password:" {                send "${passwd}\r"                expect eof         }}



0 0