1. 程式人生 > >Linux系統——shell指令碼應用示例

Linux系統——shell指令碼應用示例

傳入一個網段地址,自動找出本網段記憶體活的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