簡體   English   中英

使用Shell腳本從另一個文件檢查用戶名和密碼

[英]username and password checking from another file using shell script

我是Shell的新手,我想執行以下任務

一個shell程序,它接受用戶名和密碼,並用另一個文件中的一行(用於身份驗證)檢查$ username和$ password,我知道如何從用戶那里獲取用戶名,但是如何檢查另一個文件中的一行。 說我有一個名為username的文件(具有用戶名)和名為password的文件(具有密碼),如何檢查文件中該行的$ username和該行的$ password

直接來自perl食譜:(我猜這不好,因為OP想要bash)

#!/usr/bin/perl -w
# 

checkuser - demonstrates reading and checking a user's password

use Term::ReadKey;

print "Enter your password: ";
ReadMode 'noecho';
$password = ReadLine 0;
chomp $password;
ReadMode 'normal';

print "\n";

($username, $encrypted) = ( getpwuid $< )[0,1];

if (crypt($password, $encrypted) ne $encrypted) {
    die "You are not $username\n";
} else {
    print "Welcome, $username\n";
}

超級基礎,沒有安全性,但是應該可以在文件中找到它們

我認為應該怎么做(單個文件)

echo 'Type username'
read username
echo 'Type Password'
read -s Password
awk /^$username.*$Password$/' {print "found"}' loginfile

對於OP設置文件的方式

echo 'Type username'
read username
echo 'Type Password'
read -s Password
UserCheck=$(awk /^$username$/' {print "1"}' Username.txt)
PassCheck=$(awk /^$Password$/' {print "1"}' Password.txt)

if [[ UserCheck -eq 1 && PassCheck -eq 1 ]]; then
     echo "Found"
else
     echo "not found"
fi

這是一個示例腳本,提示用戶輸入用戶名和密碼,然后將它們與從兩個文件中獲取的用戶名和密碼進行比較:

#!/bin/bash

# prompt the user to enter a username and password
read -p "Username: " inputUsername
read -s -p "Password: " inputPassword

# read the username and password from file
username=$(<username.txt)
password=$(<password.txt)

# compare
if [[ "$inputUsername" == "$username" && "$inputPassword" == "$password" ]]
then
    echo "valid username and password"
else
    echo "invalid username and password"
fi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM