silent raw_input for passwords

Discussion in 'Python' started by Stephen Boulet, May 1, 2004.

  1. I need a password for a script and I would like to not have it stored in a
    file or shown in a terminal.

    "passphrase = raw_input()" still lets you see the input on the screen. Is
    there a way to have it be hidden? It's my gpg passphrase, so I don't want
    it anywhere except in my head.

    If anyone's interested, I'm wrapping the command for the duplicity program
    (incremental GPG encrypted backups to an FTP server;
    http://www.nongnu.org/duplicity/):

    ===========================
    #!/bin/env python

    from ftplib import FTP
    from os import popen

    dirfile = '/usr/local/backups.txt'
    dirs = file(dirfile).read().split('\n')

    host = 'ftphost'
    uname = 'ftpuname'
    password = 'ftppass'

    ftp = FTP(ftphost)
    ftp.login(ftpuname,ftppass)
    l=ftp.nlst()

    for i,dir in enumerate(dirs):
    if str(i+1) not in l:
    print 'Directory "%s" is not on the FTP server.' % dir
    print "Creating directory %d on server..." % (i+1)
    ftp.mkd(str(i+1))
    ftp.quit()

    print "Starting duplicity synchronization ..."
    print "Enter your passphrase:"
    passphrase = raw_input()
    passphrase = "PASSPHRASE=" + passphrase

    for i in range(1,len(dirs)+1):
    command = passphrase + ' duplicity ftp://'
    command += ftpuname + '@' + ftphost + '/ "'
    command += dirs[i-1] + '" ' + str(i)
    print "Starting backup of directory %s" % dirs[i-1]
    popen(command).read()

    print "Done!"


    --
    Stephen
    From here to there
    and there to here,
    funny things are everywhere. -- Dr Seuss
    Stephen Boulet, May 1, 2004
    #1
    1. Advertising

  2. On Fri, 30 Apr 2004 21:26:40 -0500,
    Stephen Boulet <> wrote:

    > I need a password for a script and I would like to not have it
    > stored in a file or shown in a terminal.


    > "passphrase = raw_input()" still lets you see the input on the
    > screen. Is there a way to have it be hidden? It's my gpg
    > passphrase, so I don't want it anywhere except in my head.


    See the getpass module; it's part of the standard library.

    Most GUI's have similar functionality.

    HTH,,
    Heather

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli
    Heather Coppersmith, May 1, 2004
    #2
    1. Advertising

  3. Thanks!

    Heather Coppersmith wrote:

    > On Fri, 30 Apr 2004 21:26:40 -0500,
    > Stephen Boulet <> wrote:
    >
    >> I need a password for a script and I would like to not have it
    >> stored in a file or shown in a terminal.

    >
    >> "passphrase = raw_input()" still lets you see the input on the
    >> screen. Is there a way to have it be hidden? It's my gpg
    >> passphrase, so I don't want it anywhere except in my head.

    >
    > See the getpass module; it's part of the standard library.
    >
    > Most GUI's have similar functionality.
    >
    > HTH,,
    > Heather
    >


    --
    Stephen
    From here to there
    and there to here,
    funny things are everywhere. -- Dr Seuss
    Stephen Boulet, May 1, 2004
    #3
  4. Stephen Boulet

    Dan Bishop Guest

    Stephen Boulet <> wrote in message news:<>...
    > I need a password for a script and I would like to not have it stored in a
    > file or shown in a terminal.
    >
    > "passphrase = raw_input()" still lets you see the input on the screen. Is
    > there a way to have it be hidden? It's my gpg passphrase, so I don't want
    > it anywhere except in my head.


    There's probably an easier way, but you can use:

    def input_password(echo=True):
    chars = []
    while True:
    newChar = getch()
    if newChar in '\r\n':
    break
    elif newChar in '\b\x7F':
    if chars:
    del chars[-1]
    sys.stdout.write('\b')
    else:
    chars.append(newChar)
    if echo:
    sys.stdout.write('*')
    return ''.join(chars)
    Dan Bishop, May 1, 2004
    #4
  5. On 2004-05-01, Dan Bishop <> wrote:
    > Stephen Boulet <> wrote in message news:<>...
    >> I need a password for a script and I would like to not have it stored in a
    >> file or shown in a terminal.
    >>
    >> "passphrase = raw_input()" still lets you see the input on the screen. Is
    >> there a way to have it be hidden? It's my gpg passphrase, so I don't want
    >> it anywhere except in my head.

    >
    > There's probably an easier way, but you can use:
    >
    > def input_password(echo=True):
    > chars = []
    > while True:
    > newChar = getch()
    > if newChar in '\r\n':
    > break
    > elif newChar in '\b\x7F':
    > if chars:
    > del chars[-1]
    > sys.stdout.write('\b')
    > else:
    > chars.append(newChar)
    > if echo:
    > sys.stdout.write('*')
    > return ''.join(chars)


    How is this different from getpass.getpass() ?
    Dominique Orban, May 2, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Silent Install

    Charles A. Lackman, Feb 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,050
    Fireface
    Mar 15, 2005
  2. khawar
    Replies:
    2
    Views:
    1,378
    vMike
    Oct 28, 2003
  3. paul brown

    Silent JRE installation 1.4.2_03

    paul brown, Jan 28, 2004, in forum: Java
    Replies:
    0
    Views:
    429
    paul brown
    Jan 28, 2004
  4. RJGraham
    Replies:
    8
    Views:
    10,275
    Ryan Stewart
    Feb 14, 2004
  5. Danny

    Silent install for JDK

    Danny, Jun 22, 2004, in forum: Java
    Replies:
    0
    Views:
    526
    Danny
    Jun 22, 2004
Loading...

Share This Page