一个shell脚本的实践
一、生成htpasswd的账号密码 htpasswd.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #!/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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #!/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" |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端