【问题标题】:WebSphere wsadmin jython - prompting for passwordWebSphere wsadmin jython - 提示输入密码
【发布时间】:2012-10-24 12:01:09
【问题描述】:

我正在寻找一种提示输入密码的方法(即没有输入回显)。 我在 WebSphere 的 7.0.0.19 wsadmin 中使用 jython。

我已经找到了 - 使用 import getpass 或 import termios 似乎是可能的(但我得到“没有名为...的模块”异常)。

有什么方法可以提示输入密码吗?

谢谢。

【问题讨论】:

  • 我的回答有用吗?您对此有任何疑问吗?
  • 即使您不再需要这个问题,您仍然需要检查所提供的答案,如果您认为它回答了您的问题,请接受它。请这样做。

标签: websphere jython wsadmin


【解决方案1】:

您可以使用以下代码。它基本上使用 Java 的 console() 如果存在(请注意控制台 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

【讨论】:

    【解决方案2】:

    以下内容也对我有用:

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

    这并不完美,因为它不会掩盖密码,但它确实有效。出于某种原因,如果您没有指定第一个“raw_input”调用,则脚本不会阻塞第二个调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2018-12-02
      • 2016-04-08
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多