Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hide a password in tcl

Status
Not open for further replies.

birdy1980

Programmer
Nov 1, 2011
9
0
0
GB
Hi
I know I may sound naive but is it possible to hide/mask the user input in a gets stdin command. I want to do this in my script which accepts user input for password

puts -nonewline stdout "Please enter the password: ";

#want to mask/hide the input text here
gets stdin wspasswd;

I tried searching around but only got code specific to OS/DB. Is there nothing on TCL level itself to get this done

Any help will be greatly appreciated.

Thanks
birdy
 
I don't think you can do it without using Tk (like in an entry widget).

_________________
Bob Rashkin
 
Hi Bob

I am sorry but I am not too aware of Tk and how it can work alongside Tcl. is it as simple as

set passwd makeentry(parent, "Password:", 10, show="*");

can you illustrate a very basic example please?
 
I's possible, but you need a package called Expect.
Long time ago I wrote a function getpass, here is it:

Code:
[COLOR=#804040][b]proc[/b][/color] getpass {prompt} {
[COLOR=#0000ff]  # Required package is Expect. It could be installed using teacup:[/color]
[COLOR=#0000ff]  # teacup install Expect[/color]
  [COLOR=#804040][b]package[/b][/color] require Expect
  [COLOR=#804040][b]set[/b][/color] oldmode [stty -echo -raw]
  send_user [COLOR=#ff00ff]"$prompt"[/color]
  [COLOR=#804040][b]set[/b][/color] timeout -[COLOR=#ff00ff]1[/color]
  expect_user -re [COLOR=#ff00ff]"(.*)[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
  send_user [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
  [COLOR=#804040][b]eval[/b][/color] stty [COLOR=#008080]$oldmode[/color]
  [COLOR=#804040][b]return[/b][/color] [COLOR=#008080]$expect_out[/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]string[/b][/color])
}

Here is an example, how to use it:
Code:
[COLOR=#0000ff]### Enter Login-Data[/color]
[COLOR=#804040][b]puts[/b][/color] -nonewline stdout [COLOR=#ff00ff]"AS/400 name : "[/color]; [COLOR=#804040][b]flush[/b][/color] stdout; [COLOR=#804040][b]set[/b][/color] csebk  [[COLOR=#804040][b]gets[/b][/color] stdin] 
[COLOR=#804040][b]puts[/b][/color] -nonewline stdout [COLOR=#ff00ff]"User Id     : "[/color]; [COLOR=#804040][b]flush[/b][/color] stdout; [COLOR=#804040][b]set[/b][/color] userid [[COLOR=#804040][b]gets[/b][/color] stdin]
[COLOR=#0000ff]# Using getpass for getting password[/color]
[COLOR=#804040][b]source[/b][/color] getpass.tcl
[COLOR=#804040][b]set[/b][/color] pass  [getpass [COLOR=#ff00ff]"Password    : "[/color]]
[COLOR=#804040][b]puts[/b][/color] {}
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"You entered password : [/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]$pass[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color]

Output:
Code:
C:\Work>tclsh85 getpass_usage.tcl
AS/400 name : myAS400
User Id     : mikrom
Password    :

You entered password : "mikroms_password"
 
@mikrom, very cool.
@birdy1980, Tk is the windowing package(s) that work with Tcl. You would need to run the WIndowSHell, "wish", instead of the Tcl Shell, "tclsh". Then you would build a GUI and... It's probably better to use Expect and mikrom's solution.

_________________
Bob Rashkin
 
My first scripting language was Python. It's standard library contains a module getpass with function getpass(). Later I started to learn other scripting languages too.
To evaluate if a new language could be usable for my needs and to compare the languages, I first try to implement in every language the same simple example, i.e. how to retrieve data from database on IBM iSeries (aka AS/400). For database login I need a function for getting user password without echo - something like getpass() in Python.
So - as time passed - I wrote function getpass() for IronPython, Perl, Tcl, REXX, VBscript and Lua.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top