【问题标题】:Need little assistance with pexpect module几乎不需要 pexpect 模块的帮助
【发布时间】:2012-06-26 13:54:59
【问题描述】:

在 pexpect 模块方面需要帮助

我编写了一个简单的代码,它可以使用 ssh 从服务器克隆一个 git 存储库。 我面临几个问题。

密码以纯文本形式显示。

我不知道下载后退出程序的正确方法。它抛出以下错误...

Traceback (most recent call last):
File "ToDelete3.py", line 65, in <module>
  # # if i == 1:
File "ToDelete3.py", line 36, in getRepository
  i = p.expect([ssh_key,'password:',pexpect.EOF])
File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1492, in interact
  self.__interact_copy(escape_character, input_filter, output_filter)
File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1520, in __interact_copy
  data = self.__interact_read(self.child_fd)
File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1510, in __interact_read
  return os.read(fd, 1000)
OSError: [Errno 5] Input/output error

目前我写的代码是:

command = 'git clone ssh://username@someserver/something.git'
ssh_key = 'Are you sure you want to continue connecting'

def gracefulExit():
    print 'Password Incorrect !!!'
    os._exit(1)

def getRepository():
    p = pexpect.spawn(command,maxread=10000,timeout = 100)
    p.logfile = sys.stdout  # logs out the command  
    i = p.expect([ssh_key,'password:',pexpect.EOF])
    if i == 0:
         print 'Inside sshkey'
         p.sendline('yes')
         i = p.expect([ssh_key,'password:',pexpect.EOF])
    if i == 1:
        try:
            p.sendline('mypassword') # this mypassword is shown in clear text on the console
            p.interact()
            p.logfile = sys.stdout
            p.expect(pexpect.EOF)
        except Exception,e:
            print str(e)
            gracefulExit()
    if i == 2:
        print 'Inside EOF block'
        if p.isalive():
            print '******************************************************'
            print '         Closing the process of Download !!!          '
            print '******************************************************\n\n'
            p.close()

任何意见都非常感谢..

谢谢。 -维杰

【问题讨论】:

    标签: python git


    【解决方案1】:

    程序有几个错误:

    p.interact()
    

    当我们想在使用 pexpect 模块自动提供密码后取回控制权时使用它。您不需要使用它,因为您正在自动检出整个存储库。

    还有一些可以改进的地方,在传递密码后,设置无限超时,因为复制 git 存储库可能需要一段时间。

    p.expect(pexpect.EOF, timeout=None)
    

    之后,您可以使用以下命令读取所有执行输出

    output_lines =  p.before
    output_lines_list = output_lines.split('\r\n')
    for line in output_lines: print line
    

    您也可以使用上述方法通过直接写入将输出记录到文件中

    使用p.logifile = sys.stdout 不好,因为它会从一开始就记录 pexpect 操作,包括传递密码。

    在此之后无需关闭,您没有运行交互式程序。删除所有这些行:

    if i == 2:
            print 'Inside EOF block'
            if p.isalive():
                print '******************************************************'
                print '         Closing the process of Download !!!          '
                print '******************************************************\n\n'
                p.close()
    

    问题是您必须在某些地方存储密码并将其与 p.sendline 一起使用。但是,您存储密码,它将是不安全的。您也可以在开始时输入密码,这样您就不会将密码存储在程序中,但会破坏自动化。我看不到出路,但要输入密码,您可以这样做:

    import getpass
    getpass.getpass("please provide your password")
    

    【讨论】:

    • 感谢 Pyfunc 像宝石一样工作。也感谢您的详细解释。有助于更好地理解它.. 抱歉回复晚了.. 是的,确实有效.. 但是有一个问题。我输入时它不显示密码,但之后看起来 p.logfile = sys.stdout 以某种方式在终端中以纯文本形式打印密码。任何避免这种情况的方法..
    【解决方案2】:

    要摆脱将密码回显到标准输出,请在重定向输出时使用以下内容 -

    p.logfile_read = sys.stdout  # logs out the command  
    

    我自己尝试过,似乎可以正常工作。 Here 是这个启示的参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多