Linux系统——shell脚本应用示例

时间:2022-08-27 10:18:32

传入一个网段地址,自动找出本网段内存活的IP地址。2,将存活的IP地址当作密码来创建Linux用户,用户名格式为:你的名字_数字 3,有几个存活IP地址,就自动创建几个用户   4,最后将创建的用户名和密码写入到/tmp目录下的某文件里

(1)找存活的ip

(2)将ip去除“.”,生成密码,

(3)创建用户,密码

(4)写入Path路径下的文件里(设置两个变量)

#!/bin/bash

Path=/tmp

for ip in 192.168.214.{1..5}

do

ping -c 1 $ip >/dev/null

if [ $? -eq 0 ];then

echo "$ip"

let i++

user=daisy_$i

useradd $user >/dev/null

(1)  Passwd=`echo $ip | sed -r 's#[^0-9]##g'`

[ -d $Path ] || mkdir $Path

(2) echo $Pas swd | passwd -- stdin $user

echo "$user:$Passwd">>$Path/user_passwd

fi

done

用户登录验证程序

1、脚本里设置两个用户名和密码

2、让用户从屏幕输入账户和密码进行登录

3,、如果账户输入错误,让该用户继续输入账户,也可以注册用户

4、账户正确,输入密码

5、密码正确,登陆成功

6、同一个账号,密码最多允许输错3次,达3次,账号锁定(此时脚本不能退出)。

7、锁定账号以后,可以继续登录未锁定账号,也可以删除用户

#!/bin/bash

function login(){
USER1="daisy"
PASSWD1="123123"
USER2="helen"
PASSWD2="123456"
lock=""

Path=/tmp
useradd daisy
useradd helen
echo $PASSWD1 | passwd --stdin daisy
echo $PASSWD2 | passwd --stdin helen
while :
do
read -p "please write your username:" user
clear
echo "$lock" | grep -w "$user" &>/dev/null
if [ $? -eq 0 ];then
echo "locked"
continue
fi
if [ $user != "daisy" -a $user != "helen" ];then
echo "invaild username "
read -p "create username or not (choice yes or no):" create
  case $create in
  yes)
  useradd $user
  echo "create successfully"
  passwd $user
  [ $? -eq 0 ] && echo "create successfully"
  echo "$user:$passwd" >> $Path/user_passwd
  echo "transfer successfully"
  exit
  ;;
  no)
  continue
  ;;
  *)
  echo "invaild number"
  ;;
  esac

else
i=0
while :
do
read -p "please write your password:" passwd
clear
  if [ $passwd == $PASSWD1 -a $user == "daisy" ];then
    echo "login successfully"
    exit
  elif [ $passwd == $PASSWD2 -a $user == "helen" ];then
    echo "login successfully"
    exit
  else  
    let i++
    if [ $i -eq 3 ];then
    echo "username has locked, please use another unlocked username"
    sleep 1
    clear
    lock=$lock" $user"
    sleep 1
    read -p "delete username or not(choice yes or no):" delete
      case $delete in
      yes)
        userdel $user
        echo "delete successfully"
        exit
        ;;
      no)
        exit  
        ;;
      *)
        echo "invaild number"
        ;;
      esac
    break
    fi
  echo "password is wrong"
  sleep 1
  clear
  fi
done
fi
done
}

function test(){

echo "123"
}
function title (){
cat <<FOF
********************
1、登录
2、退出
********************

FOF

}
while :
do
clear
title
read -p "请输入你的选择:" num
clear
  case $num in
  1)
    login
    ;;

  2)
    echo "exit"
    exit
    ;;
  *)
    echo "invaild number"
    ;;
  esac
sleep 2
done