脚本自动新建及删除open***用户

转载 http://azhuang.blog.51cto.com/9176790/1532521  谢谢博主

1.expect交互脚本

vim ***_expect.expect

#!/usr/bin/expect -f
if $argc<1 {
        puts stderr "Usage: $argv0 need argv.\n"
        exit 1
}
set ***user [lindex $argv 0]
set path /etc/open***/easy-rsa/2.0
spawn $path/build-key $***user
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "\r"
expect "*"
send "y\r"
expect "*"
send "y\r"
expect eof
exit

2.***用户脚本

vim create_***user.sh

#!/bin/bash
canshu_num=$#
xuan_zhe=$1
***_user=$2
help() 
#帮助函数
{
echo '添加***用户执行命令sh create_***user.sh add ***用户名'
echo '删除***用户执行命令sh create_***user.sh del ***用户名'
}
add_user()
#新建***用户函数
{
if [ -f /etc/open***/easy-rsa/2.0/keys/$***_user.crt ];then
   echo "新建***用户存在,请检查!"
   break
else
   cd /etc/open***/easy-rsa/2.0/
   source ./vars
   ./***_expect.expect $***_user 
   cd /etc/open***/easy-rsa/2.0/keys
   tar zcvf $***_user.tar.gz $***_user.* ca.crt client.o***
   echo "================================================<成功提示>================================================"
   echo "新建***用户:$***_user成功!" 
   echo "================================================<成功提示>================================================"
fi
}
del_user()
#删除***用户函数
{
   /etc/open***/easy-rsa/2.0/revoke-full $***_user
   rm -rf /etc/open***/easy-rsa/2.0/keys/$***_user.*
   echo "================================================<成功提示>================================================"
   echo "注销***用户:$***_user成功!" 
   echo "================================================<成功提示>================================================"
}
main()
#主函数
{
  if [ $canshu_num -ne 2 ];then
      echo "参数数量错误,请检查!"
      help
      break
  else
      if [ $xuan_zhe = "add" ];then
           add_user
      elif [ $xuan_zhe = "del" ];then
           del_user
      else
          echo '参数类型选择错误,类型只包含add|del'
          help
          break
      fi
  fi
}
main

备注:我的脚本路径为/etc/open***/easy-rsa/2.0/目录,由于需要使用expect脚本,需要安装yum install -y expect命令.此时执行新建open***用户脚本,需要执行create_***user.sh这个脚本。后期新建用户会写成网页模式添加。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章