指令碼小練——使用者認證

來源:互聯網
上載者:User

標籤:shell   python   

需求:輸入使用者名稱和密碼,認證成功後顯示歡迎資訊,輸錯三次後鎖定


shell:

#!/bin/bash# Auth user loginuser="hlf"pass="123456"lock_file=login.lock[ -f ${lock_file} ] || touch ${lock_file}for i in `seq 3`do        read -p "username:" username        read -p "password:" password        if [ -z "$username" -o -z "$password" ];then                echo "使用者名稱和密碼不可為空,請重新登陸:"                LoginSuccess=‘false‘        elif grep -q "\b$username\b" ${lock_file};then                echo "賬戶被鎖定,請聯絡管理員!"                LoginSuccess=‘lock‘        else                if [ "$username" = $user -a "$password" = $pass ];then                        echo "welcome to my system"                        LoginSuccess=‘true‘                else                        echo "使用者名稱或密碼錯誤,請重新登陸:"                        LoginSuccess=‘false‘                fi        fi        [ $LoginSuccess = true -o $LoginSuccess = lock ] && breakdoneif [ $LoginSuccess = false ];then        echo "登陸三次失敗,賬戶被鎖定!!!"         [ -z "$username" ] || echo "$username" >> ${lock_file}fi


python:

user.txt格式:

使用者名稱 密碼

#encoding: utf-8#!/usr/bin/python# Auth user loginuser_file = "user.txt"lock_file = "login.lock"#產生字典,儲存使用者名稱密碼user_dict = {}f = file(user_file)for line in f.readlines():        user_dict[line.split()[0]] = line.split()[1]f.close()#定義元組,儲存被鎖定的賬戶lock_tup = ()f = file(lock_file)for line in f.readlines():        line = line.strip(‘\n‘)        tup = (line,)        lock_tup = lock_tup + tupf.close()#使用者認證for i in range(3):        username = raw_input("username:").strip()        password = raw_input("password:").strip()        if len(username) == 0 or len(password) == 0:                print "使用者名稱和密碼不可為空,請重新登陸:"                LoginSuccess=‘false‘        elif username in lock_tup:                print "賬戶被鎖定,請聯絡管理員!"                LoginSuccess=‘lock‘        else:                if  password == user_dict[username]:                        print "welcome to my system"                        LoginSuccess=‘true‘                else:                        print "使用者名稱或密碼錯誤,請重新登陸:"                        LoginSuccess=‘false‘        if LoginSuccess == "true" or LoginSuccess == "lock":break#迴圈三次後if LoginSuccess == "false" and username != "":        print "登陸三次失敗,賬戶被鎖定!!!"        f = file(lock_file,‘a‘)        f.write(username + ‘\n‘)        f.close


指令碼小練——使用者認證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.