2012-11-04 65 views
3

我正在尋找一種提示輸入密碼的方法(即沒有輸入回顯)。 我在WebSphere的7.0.0.19 wsadmin中使用jython。WebSphere wsadmin jython - 提示輸入密碼

我找過它 - 它似乎有可能與導入getpass或導入termios(但我得到「無模塊命名...」異常)。

任何方式來提示輸入密碼?

謝謝。

+0

我的回答有用嗎?你對此有任何疑問嗎? – Mani

+1

即使這個問題不再需要你,你仍然需要檢查提供的答案,並接受一個,如果你覺得它回答你的問題。請這樣做。 – Mani

回答

2

您可以使用下面的代碼。它基本上使用Java的控制檯(),如果存在的話(注意控制檯may not be present一直),否則使用raw_input()和密碼屏蔽邏輯。

# if console is not available (ex: when invoked from a shell script or another java process) 
# we need to fall back to use raw_input, but we should mask the password if we use it 
import sys, thread, time, threading 
from java.lang import String 
def getPass(stream=None): 
    console = java.lang.System.console() 
    if console is None: 
     global p_stopMasking 
     if not stream: 
      stream = sys.stderr 
     try: 
      p_stopMasking = 0 
      threading.Thread(target=_doMasking,args=(stream,)).start() 
      password = raw_input() 
      p_stopMasking = 1 
     except Exception, e: 
      p_stopMasking = 1 
      print "Error Occured" 
      print e 
      exit() 
    else: 
     password = console.readPassword() 
    return String.valueOf(password) 

def _doMasking(stream): 
    while not p_stopMasking: 
     stream.write("\010*") 
     #stream.write("\n") 
     stream.flush() 
     time.sleep(0.01) 

def populateCredentials(): 
    global username 
    global password 
    print 'Enter username:' 
    username = raw_input(); 
    print 'Enter password:' 
    password = getPass(sys.stdout); 

# start main 
print 'start program...' 
p_stopMasking= 1 
username  = None 
password  = None 
populateCredentials() 
print 'username is : ' + username 
print 'password is : ' + password 
1

下也爲我工作:

raw_input("") 
myPass = raw_input("Please enter a password: ") 

,因爲它不掩蓋密碼這是不完美的,但它確實工作。出於某種原因,如果您沒有指定第一個「raw_input」調用,那麼該腳本將不會阻塞第二個腳本。