Hide input on command line
Try java.io.Console.readPassword
. You'll have to be running at least Java 6 though.
/**
* Reads a password or passphrase from the console with echoing disabled
*
* @throws IOError
* If an I/O error occurs.
*
* @return A character array containing the password or passphrase read
* from the console, not including any line-termination characters,
* or <tt>null</tt> if an end of stream has been reached.
*/
public char[] readPassword() {
return readPassword("");
}
Beware though, this doesn't work with the Eclipse console. You'll have to run the program from a true console/shell/terminal/prompt to be able to test it.
Hiding user input on terminal in Linux script
Just supply -s to your read call like so:
$ read -s PASSWORD
$ echo $PASSWORD
Hide Input in Batch File
You can probably look at the answers here: Can I mask an input text in a bat file
I think EditV32.exe and EditV64.exe are the best options you have (and since you were OK to use conset, which as far as I know does not come with Windows, I don't think you would oppose to using editv?)
http://www.westmesatech.com/editv.html
Also: http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/7d2d92a5-5d59-4954-bf3e-22eeb5cf0ee6/
how to hide password in command line with **** and get the value into .bat file and .sh file
For bash its read -s
.
-s Silent mode. If input is coming from a terminal, characters are not echoed.
For batch it seems to be more complicated.
Read about it here:
Can I mask an input text in a bat file
How to show/hide input option based on the first input?
You would have to use a custom callback:
import click
def prompt_user(ctx, param, user):
new_user = None
if user:
new_user = click.prompt('username')
return (user, new_user)
@click.command()
@click.option('--user/--no-user', prompt='do you want to add user?', callback=prompt_user)
def add_user(user):
user, new_user = user
print(user)
print(new_user)
if __name__ == '__main__':
add_user()
$ python3.8 user.py
do you want to add user? [y/N]: y
username: no
True
no
$ python3.8 user.py
do you want to add user? [y/N]: N
False
None
Note that prompt_user
returns a tuple of two values. So the line user, new_user = user
sets user
equal to the first value and new_user
to the second. See this link for more explanation.
Hide password input on terminal
In the Linux world, masking isn't usually done with asterisks, normally echoing is just turned off and the terminal displays blanks E.g. if you use su
or log into a virtual terminal etc.
There is a library function to handle getting passwords, it won't mask the password with asterisks but will disable echoing of the password to terminal. I pulled this out of a linux book I have. I believe its part of the posix standard
#include <unistd.h>
char *getpass(const char *prompt);
/*Returns pointer to statically allocated input password string
on success, or NULL on error*/
The getpass() function first disables echoing and all processing of
terminal special characters (such as the interrupt character, normally
Control-C).It then prints the string pointed to by prompt, and reads a line of
input, returning the null-terminated input string with the trailing
newline stripped, as its function result.
A google search for getpass() has a reference to the GNU implementation (should be in most linux distros) and some sample code for implementing your own if need be
http://www.gnu.org/s/hello/manual/libc/getpass.html
Their example for rolling your own:
#include <termios.h>
#include <stdio.h>
ssize_t
my_getpass (char **lineptr, size_t *n, FILE *stream)
{
struct termios old, new;
int nread;
/* Turn echoing off and fail if we can't. */
if (tcgetattr (fileno (stream), &old) != 0)
return -1;
new = old;
new.c_lflag &= ~ECHO;
if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
return -1;
/* Read the password. */
nread = getline (lineptr, n, stream);
/* Restore terminal. */
(void) tcsetattr (fileno (stream), TCSAFLUSH, &old);
return nread;
}
If need be you could use this as the basis as modify it to display asterisks.
Can I mask an input text in a bat file?
Up to XP and Server 2003, you can make use of another included tool (VBScript) - the following two scripts do the job you want.
First, getpwd.cmd
:
@echo off
<nul: set /p passwd=Password:
for /f "delims=" %%i in ('cscript /nologo getpwd.vbs') do set passwd=%%i
echo.
Then, getpwd.vbs
:
Set oScriptPW = CreateObject("ScriptPW.Password")
strPassword = oScriptPW.GetPassword()
Wscript.StdOut.WriteLine strPassword
The getpwd.vbs
simply uses the password object to input the password from the user and then print it to standard output (the next paragraph will explain why that doesn't show up in the terminal).
The getpwd.cmd
command script is a bit trickier but it basically works as follows.
The effect of the "<nul: set /p passwd=Password: "
command is to output the prompt with no trailing newline character - it's a sneaky way to emulate the "echo -n"
command from the bash
shell. It sets passwd
to an empty string as an irrelevant side effect and doesn't wait for input since it's taking its input from the nul:
device.
The "for /f "delims=" %%i in ('cscript /nologo getpwd.vbs') do set passwd=%%i"
statement is the trickiest bit. It runs the VBScript with no Microsoft "advertising", so that the only line output is the password (from the VBscript "Wscript.StdOut.WriteLine strPassword"
.
Setting the delimiters to nothing is required to capture an entire input line with spaces, otherwise you just get the first word. The "for ... do set ..."
bit sets passwd
to be the actual password output from the VBScript.
Then we echo a blank line (to terminate the "Password: "
line) and the password will be in the passwd
environment variable after the code has run.
Now, as mentioned, scriptpw.dll
is available only up to XP/2003. In order to rectify this, you can simply copy the scriptpw.dll
file from the Windows\System32
folder of an XP/2003 system to the Winnt\System32
or Windows\System32
folder on your own system. Once the DLL has been copied, you will need to register it by running:
regsvr32 scriptpw.dll
To successfully register the DLL on Vista and later, you will need administrator privileges. I haven't examined the legality of such a move so cave lector.
If you're not overly keen on trying to track down and register older DLL files (for convenience or legal reasons), there is another way. Later versions of Windows (the ones that don't have the required DLL) should have Powershell available to you.
And, in fact, you really should consider upgrading your scripts to use it fully since it's a much more capable scripting language than cmd.exe
. However, if you want to keep the bulk of your code as cmd.exe
scripts (such as if you have a lot of code that you don't want to convert), you can use the same trick.
First, modify the cmd
script so it calls Powershell rather than CScript:
@echo off
for /f "delims=" %%i in ('powershell -file getpwd.ps1') do set passwd=%%i
The Powershell script is equally simple:
$password = Read-Host "Enter password" -AsSecureString
$password = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$password = [Runtime.InteropServices.Marshal]::PtrToStringAuto($password)
echo $password
although with some marshalling to get the actual password text.
Remember that, to run local unsigned Powershell scripts on your machine, you may need to modify the execution policy from the (draconian, though very safe) default, with something like:
set-executionpolicy remotesigned
from within Powershell itself.
Related Topics
Arrays.Fill with Multidimensional Array in Java
How to Convert Double[] to Double[]
How to Split a Text into Sentences Using the Stanford Parser
Java Benchmarking - Why Is the Second Loop Faster
Why Do Consumers Accept Lambdas with Statement Bodies But Not Expression Bodies
What's an Example of Duck Typing in Java
Not Implementing All of the Methods of Interface. Is It Possible
Can Add Extra Field(S) to @Manytomany Hibernate Extra Table
Should You Call Releasestringutfchars If Getstringutfchars Returned a Copy
Selenium Webdriver Submit() VS Click()
How to Compile Java to Native Code
Java Conventions: Use Getters/Setters Within the Class
How to Serialize Object to CSV File
Detect Internet Connection Using Java
Write a File in Utf-8 Using Filewriter (Java)
How to Calculate the Number of Days in a Period
"Faceted Project Problem (Java Version Mismatch)" Error Message