一个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 "\n\n"

 

二、仅显示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 "\n\n"

echo "${username}:${password}"

echo -e "\n\n"

  

posted @ 2021-08-25 11:18  MR__Wang  阅读(75)  评论(0编辑  收藏  举报