简体   繁体   English

使用Shell脚本从另一个文件检查用户名和密码

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

I'm new to shell, I want to do the following task 我是Shell的新手,我想执行以下任务

A shell program which accepts username and password and checks the $username and $password with a line in another file (for authentication), I know how to get the username from user but how do I check against a line in another file. 一个shell程序,它接受用户名和密码,并用另一个文件中的一行(用于身份验证)检查$ username和$ password,我知道如何从用户那里获取用户名,但是如何检查另一个文件中的一行。 say I have a file called username (which has the username) and file called password (which has the password) how do I check $username with that line and $password with that line in the file 说我有一个名为username的文件(具有用户名)和名为password的文件(具有密码),如何检查文件中该行的$ username和该行的$ password

Straight from the perl cookbook: (I guess it's no good though because the OP wanted bash) 直接来自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";
}

Super basic and has no security but this should work for finding them in a file 超级基础,没有安全性,但是应该可以在文件中找到它们

How i think it should be done(single file) 我认为应该怎么做(单个文件)

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

For the way OP has files set up 对于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

Here is an example script which prompts the user to enter a username and password and then compares them against the username and password taken from two files: 这是一个示例脚本,提示用户输入用户名和密码,然后将它们与从两个文件中获取的用户名和密码进行比较:

#!/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.

相关问题 如何从shell脚本文件中读取用户名和密码? - how to read username & password from file in shell script? 如何使用Shell脚本将用户名和密码传递给应用程序? - How to pass Username and password to an application using shell script? Executing shell script from another shell script &amp; Checking for a particular string from the output of invoked shell script - Executing shell script from another shell script & Checking for a particular string from the output of invoked shell script linux bash shell:如何使用文件中的用户名和密码创建用户? - linux bash shell: how to create user with username and password from file? 使用 shell 脚本将特定单词从一个文件复制到另一个文件 - Copy specific word from a file to another file using shell script 想要从shell脚本传递scp命令的密码,以便将文件从一台服务器传输到另一台服务器 - want to pass the password from the shell script for the scp command for transferring the file from one server to another 使用 shell 脚本从另一个文件生成报告 - Generate report from a another file using shell script 使用Shell脚本检查EOF - Checking for EOF using shell script 从shell脚本中的另一个文件中读取命令? - Reading commands from another file in a shell script? 如何在Shell脚本中以非root用户身份登录(同时使用用户名和密码)? - How to login (using both username and password) as a non root user in linux in a shell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM