program tip

Linux 스크립트에서 터미널의 사용자 입력 숨기기

radiobox 2020. 8. 3. 08:34
반응형

Linux 스크립트에서 터미널의 사용자 입력 숨기기


다음과 같은 bash 스크립트가 있습니다.

#!/bin/bash

echo "Please enter your username";
read username;

echo "Please enter your password";
read password;

사용자가 터미널에 암호를 입력 할 때 암호를 표시하지 않아야합니다 (또는 *******와 같은 것이 표시되어야 함). 어떻게하면 되나요?


다음과 같이 읽기 호출에 -s를 입력하십시오.

$ read -s PASSWORD
$ echo $PASSWORD

최신 정보

입력하는 *각 문자에 대해 를 출력하여 멋지게 만들고 싶다면 andreas의 read -s솔루션을 사용하여 다음과 같이 할 수 있습니다 .

unset password;
while IFS= read -r -s -n1 pass; do
  if [[ -z $pass ]]; then
     echo
     break
  else
     echo -n '*'
     password+=$pass
  fi
done

화려하지 않고

echo "Please enter your username";
read username;

echo "Please enter your password";
stty -echo
read password;
stty echo

bash 또는 특정 기능없이 작동하는 솔루션의 read경우 stty에코를 비활성화 하는 데 사용할 수 있습니다

stty_orig=$(stty -g)
stty -echo
read password
stty $stty_orig

여기 백 스페이스를 지원하는 @SiegeX의 우수한 *인쇄 솔루션 의 변형이 있습니다 . 이를 통해 사용자 는 일반적으로 암호 프롬프트에서 지원하는 것처럼 키 ( Mac의 경우 키)사용하여 항목수정할 수 있습니다 .bash backspacedelete

#!/usr/bin/env bash

password=''
while IFS= read -r -s -n1 char; do
  [[ -z $char ]] && { printf '\n'; break; } # ENTER pressed; output \n and break.
  if [[ $char == $'\x7f' ]]; then # backspace was pressed
      # Remove last char from output variable.
      [[ -n $password ]] && password=${password%?}
      # Erase '*' to the left.
      printf '\b \b' 
  else
    # Add typed char to output variable.
    password+=$char
    # Print '*' in its stead.
    printf '*'
  fi
done

노트 :

  • 백 스페이스 레코드를 눌러 문자 코드를 기록하는 이유는 0x7f"현대 시스템에서 백 스페이스 키는 종종 삭제 문자 (ASCII 또는 유니 코드의 0x7f)에 매핑됩니다" https://en.wikipedia.org/wiki/Backspace
  • \b \b is needed to give the appearance of deleting the character to the left; just using \b moves the cursor to the left, but leaves the character intact (nondestructive backspace). By printing a space and moving back again, the character appears to have been erased (thanks, The "backspace" escape character '\b' in C, unexpected behavior?).

In a POSIX-only shell (e.g., sh on Debian and Ubuntu, where sh is dash), use the stty -echo approach (which is suboptimal, because it prints nothing), because the read builtin will not support the -s and -n options.


A bit different from (but mostly like) @lesmana's answer

stty -echo
read password
stty echo

simply: hide echo do your stuff show echo


Here is a variation of @SiegeX's answer which works with traditional Bourne shell (which has no support for += assignments).

password=''
while IFS= read -r -s -n1 pass; do
  if [ -z "$pass" ]; then
     echo
     break
  else
     printf '*'
     password="$password$pass"
  fi
done

I always like to use Ansi escape characters:

echo -e "Enter your password: \x1B[8m"
echo -e "\x1B[0m"

8m makes text invisible and 0m resets text to "normal." The -e makes Ansi escapes possible.

The only caveat is that you can still copy and paste the text that is there, so you probably shouldn't use this if you really want security.

It just lets people not look at your passwords when you type them in. Just don't leave your computer on afterwards. :)


NOTE:

The above is platform independent as long as it supports Ansi escape sequences.

However, for another Unix solution, you could simply tell read to not echo the characters...

printf "password: "
let pass $(read -s)
printf "\nhey everyone, the password the user just entered is $pass\n"

Get Username and password

Make it more clear to read but put it on a better position over the screen

#!/bin/bash
clear
echo 
echo 
echo
counter=0
unset username
prompt="  Enter Username:"
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $'\0' ]]; then
        break
    elif [ $char == $'\x08' ] && [ $counter -gt 0 ]; then
        prompt=$'\b \b'
        username="${username%?}"
        counter=$((counter-1))
    elif [ $char == $'\x08' ] && [ $counter -lt 1 ]; then
        prompt=''
        continue
    else
        counter=$((counter+1))
        prompt="$char"
        username+="$char"
    fi
done
echo
unset password
prompt="  Enter Password:"
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $'\0' ]]; then
        break
    elif [ $char == $'\x08' ] && [ $counter -gt 0 ]; then
        prompt=$'\b \b'
        password="${password%?}"
        counter=$((counter-1))
    elif [ $char == $'\x08' ] && [ $counter -lt 1 ]; then
        echo
        prompt="  Enter Password:"
        continue
    else
        counter=$((counter+1))
        prompt='*'
        password+="$char"
    fi
done

참고URL : https://stackoverflow.com/questions/4316730/hiding-user-input-on-terminal-in-linux-script

반응형