学习笔记之简单的自动部署用户的脚本
来源:互联网 发布:命名实体识别算法 编辑:程序博客网 时间: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
- 学习笔记之简单的自动部署用户的脚本
- Linux平台自动部署脚本的一个简单例子
- shell脚本学习笔记--一个简单的脚本
- Linux的学习笔记06:批量创建用户的脚本
- Unity3D学习笔记之脚本的生命周期
- 【自动构建系列】自动部署环境的shell脚本
- Liferay简单的自动部署的方法
- zookeeper 学习笔记001(部署和简单的使用)
- shell学习笔记(2) 一个简单的shell脚本程序
- Oracle学习笔记(几个简单的性能分析脚本)
- 链接脚本学习笔记1------sections的简单应用分析
- PHP学习笔记02——简单的php脚本
- JFinal学习笔记(二) 实现简单的用户登录
- 简单的ftp 自动上传脚本
- 简单的zabbix agent自动安装脚本
- Linux学习笔记:简单了解用户,对用户的简单操作
- Linux学习笔记:bash脚本编程之用户交互
- Linux学习之shell脚本的简单使用
- iOS开发之懒加载2
- OPENCV边缘提取
- 用筛选法求n以内的素数
- Linux下不同运维人员共用root 账户权限审计
- iOS开发 ☞ OS_dispatch_data
- 学习笔记之简单的自动部署用户的脚本
- Codeforces735C Tennis Championship
- 使用LocalBroadcastManager
- RelativeLayout属性记录
- Java FutureTask 源码解析
- [Google Guava] 1.1-使用和避免null
- 【Unity】2D Sprite九宫格(NineSliced)
- 基于slideout.js实现的移动端侧边栏滑动特效
- centos安装nodejs