zoukankan      html  css  js  c++  java
  • 一个shell脚本的实践

    一、生成htpasswd的账号密码 htpasswd.sh

    #!/bin/bash
    
    # define restricted path
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    
    # adirname - return absolute dirname of given file
    adirname() { odir=`pwd`; cd `dirname $1`; pwd; cd "${odir}"; }
    
    
    # ---------
    # constants
    # ---------
    MYNAM=`basename "$0"`
    MYDIR=`adirname "$0"`
    HTF=${MYDIR}/htpasswd/auth.secrets
    
    # ---------
    # functions
    # ---------
    
    message() { echo "$@"; }    # message - output message on stdout
    error() { echo "$@" >&2; }  # error - output message on stderr
    die() { error "$@"; exit 1; }   # die - output message on stderr and exit
    
    # check uid (should be root)
    [ `id -u` -eq "0" ] ||
        die "${MYNAM}: this should be run as root only!"
    
    echo "====================================="
    echo "# A tool like htpasswd for Nginx    #"
    echo "#-----------------------------------#"
    echo "# Author: wang hengzhi                #"
    echo "====================================="
    
    
    #set UserName
    
    username=""
    read -p "Please input UserName: " username
    if [ "$username" = "" ]; then
    	echo "Error:UserName can't be NULL!"
    	exit 1
    fi
    echo "==========================="
    echo "UserName was: $username"
    echo "==========================="
    
    
    #set password
    
    unpassword=""
    read -p "Please input the Password: " unpassword
    if [ "$unpassword" = "" ]; then
    	echo "Error:Password can't be NULL!"
    	exit 1
    fi
    echo "==========================="
    echo "Password was: $unpassword"
    echo "==========================="
    password=$(/usr/bin/perl -e 'print crypt($ARGV[0], "W18-salt-hash")' $unpassword)
    
    
    #set htpasswd file
    
    htfile=""
    read -p "Please input Auth filename(${HTF}): " htfile
    if [ "$htfile" = "" ]; then
    	htfile="${HTF}"
    fi
    echo "==========================="
    echo "Auth File: $htfile"
    echo "==========================="
    if [ -f $htfile ]; then
    	echo "Auth File: exist !"
    else
    	echo "Auth File: not found !"
    fi
    echo "==========================="
    
    echo ""
    read -p "Press any key to Create...or Press Ctrl+c to cancel" T
    
    
    if [ -f $htfile ]; then
    	echo "Modify Auth file......"
    	sed -i "/${username}:/d" $htfile
    else
    	echo "Create Auth file......"
    fi
    echo "${username}:${password}" >> $htfile
    echo "Write Auth file successful,auth file path: $htfile."
    
    echo -e "
    
    "
    

     

    二、仅显示htpasswd 的账号密码echo_htpasswd.sh

    #!/bin/bash
    
    # define restricted path
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    
    # adirname - return absolute dirname of given file
    adirname() { odir=`pwd`; cd `dirname $1`; pwd; cd "${odir}"; }
    
    
    # ---------
    # constants
    # ---------
    MYNAM=`basename "$0"`
    MYDIR=`adirname "$0"`
    HTF=${MYDIR}/htpasswd/auth.secrets
    
    # ---------
    # functions
    # ---------
    
    message() { echo "$@"; }    # message - output message on stdout
    error() { echo "$@" >&2; }  # error - output message on stderr
    die() { error "$@"; exit 1; }   # die - output message on stderr and exit
    
    # check uid (should be root)
    #[ `id -u` -eq "0" ] ||
    #    die "${MYNAM}: this should be run as root only!"
    
    echo "====================================="
    echo "# A tool like htpasswd for Nginx    #"
    echo "#-----------------------------------#"
    echo "# Author: wang hengzhi                #"
    echo "====================================="
    
    
    #set UserName
    
    username=""
    read -p "Please input UserName: " username
    if [ "$username" = "" ]; then
    	echo "Error:UserName can't be NULL!"
    	exit 1
    fi
    echo "==========================="
    echo "UserName was: $username"
    echo "==========================="
    
    
    #set password
    
    unpassword=""
    read -p "Please input the Password: " unpassword
    if [ "$unpassword" = "" ]; then
    	echo "Error:Password can't be NULL!"
    	exit 1
    fi
    echo "==========================="
    echo "Password was: $unpassword"
    echo "==========================="
    password=$(/usr/bin/perl -e 'print crypt($ARGV[0], "W18-salt-hash")' $unpassword)
    
    echo -e "
    
    "
    
    echo "${username}:${password}"
    
    echo -e "
    
    "
    

      

  • 相关阅读:
    矢量瓦片切割工具,注意不是切图工具哦
    openlayers模仿google地图--地图版权随鹰眼关闭打开而改变位置
    centos建立本地yum源shell脚本
    python通用序列操作
    awk手册
    linux启动级别简单说明
    win8程序开机自启动管理
    linux系统监控shell脚本
    shell脚本实现源码lamp自动化安装
    python实现冒泡排序
  • 原文地址:https://www.cnblogs.com/xzlive/p/15184181.html
Copyright © 2011-2022 走看看