python - 需要 pexpect 模块的帮助

标签 python git

需要 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()

任何意见都将受到高度赞赏..

谢谢。 -维杰

最佳答案

程序中存在一些错误:

p.interact()

当我们在使用 pexpect 模块自动提供密码后想要取回控制权时,可以使用此方法。您不需要使用它,因为您正在自动化整个存储库的 checkout 。

还有一些事情可以改进,在传递密码后,设置无限超时,因为复制 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")

关于python - 需要 pexpect 模块的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11208931/

相关文章:

git - 点文件存储库 : Switching from per-package Git submodules to ELPA while maintaining portability

python - 符合 PEP440 和 git 描述部署包中可用的信息

git - 文件重命名后如何 merge 两个不同的文件

Git:交互式 rebase 列出不正确(太多)的提交

python - SCons 生成可变数量的目标

python - Bokeh 饼图显示百分比标签倒置

reactjs - 为什么我的项目中的客户端文件夹没有上传到 gitHub?

python Timedelta溢出

python - 删除重复行?

python - 在 matplotlib 中保留多个条形图中的 xticks