密码生成器

密码生成器

重新学习大学时的 bash,并制作一个密码生成器。我希望能够将结果输出到文本文件,有人能帮忙吗?到目前为止我的代码是:

#!/bin/bash

#Password Generator

#Account Name
echo "What account is this password for"
read account

#username
echo "Please give your username"
read username

#ask for password length

echo "please provide a password length"
read length

echo "Your password is"
openssl rand -base64 $length

我希望能够将帐户、用户名和密码的结果输出到文本文件中,在此先感谢您的帮助,我希望这个查询有意义!

答案1

您可以使用 echo 重定向任何输出,例如:cmd > filename 或者在您的情况下:

openssl rand -base64 $length > passwords.txt

答案2

有几种方法可以做到这一点,但最终只有两种方法:通过管道连接到文件,或通过管道连接到写入文件的程序。

我在下面创建了一些脚本的修改版本,演示了一些方法。

输出到新文件(Gedit):

#!/bin/bash
#Password Generator A 1.1
#A random password generator written for Bash.
#Dependencies: Gedit, OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
##Generate Password 
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal
echo "$output"
###Output to new file in Gedit
echo "$output" | gedit -
exit 0

输出到新文件(Nano):

#!/bin/bash
#Password Generator B 1.1
#A random password generator written for Bash.
#Dependencies: Nano, OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
##Generate Password 
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal
echo "$output"
###Output to new file in Nano
echo "$output" | nano -
exit 0

输出到位于“~/output”的文本文件:

#!/bin/bash
#Password Generator C 1.1
#A random password generator written for Bash.
#Dependencies: OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
##Generate Password 
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal
echo "$output"
###Output to file
echo "$output" > "$HOME/output"
exit 0

输出到位于“〜/ output”的文本文件(使用Tee):

#!/bin/bash
#Password Generator D 1.1
#A random password generator written for Bash.
#Dependencies: Tee, OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
##Generate Password 
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal & file
echo "$output" | tee "$HOME/output"
exit 0

输出到文本文件(用户选择):

#!/bin/bash
#Password Generator E 1.1
#A random password generator written for Bash.
#Dependencies: OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
###Save Location
echo -n "Save to:"
read savelocation
##Generate Password 
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal
echo "$output"
###Output to file
echo "$output" > "$savelocation"
exit 0

相关内容