需要pexpect模块一点帮助 [英] Need little assistance with pexpect module

查看:127
本文介绍了需要pexpect模块一点帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要pexpect模块的帮助

我已经编写了一个简单的代码,可以使用ssh从服务器克隆git存储库。
我遇到了一些问题。



密码以纯文本显示。



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

pre code $ Traceback最近一次调用最后一次
文件ToDelete3.py ,第65行,在< module>
##if i == 1:
在getRepository
i = p.expect([ssh_key,'password:',​​pexpect.EOF])文件ToDelete3.py中,第36行。
文件/usr/lib/python2.7/dist-packages/pexpect.py,第1492行,在
self .__ interact_copy(escape_character,input_filter,output_filter)
文件/ usr / lib / python2.7 / dist-packages / pexpect.py,第1520行,在__interact_copy
data = self .__ interact_read(self.child_fd)
文件/usr/lib/python2.7 /dist-packages/pexpect.py,第1510行,在__interact_read
返回os.read(fd,1000)
OSError:[Errno 5]输入/输出错误

到目前为止我写的代码是:

  command ='git clone ssh://username@someserver/something.git'
ssh_key ='您确定要继续连接'

def gracefulExit() :
print'Password Incorrect !!!'
os._exit(1)
$ b $ def getRepository():
p = pexpect.spawn(command,maxread = 10000 ,TI meout = 100)
p.logfile = sys.stdout#注销命令
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')#这个mypassword在控制台上以明文形式显示
p.interact()
p.logfile = sys.stdout
p.expect(pexpect.EOF)
除了Exception,e:
print str(e)
gracefulExit()
if i == 2 :
print'inside EOF block'
if p.isalive():
print'********************** ********************************'
print'关闭下载程序!!! '
print'******************************************* *********** \\\
\\\
'
p.close()

非常感谢您的任何意见。

谢谢。
-Vijay

解决方案



  p.interact()

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



在通过密码后,还可以改进一些事情,设置无限超时因为复制git仓库可能需要一段时间。

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

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

  output_lines = p.before 
output_lines_list = output_lines.split('\r\\\
')
为output_lines中的行:print line

你也可以使用上面的方法直接写入输出到文件



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



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

  if if == 2:
print'Inside EOF block'
if p.isalive():
打印'************************************** ****************'
print'关闭下载过程!!! '
print'******************************************* *********** \\\
\\\
'
p.close()

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

  import getpass 
getpass。 getpass(请提供您的密码)


Need assistance with the pexpect module

I have written a simple code which would clone a git repository from a server using ssh. I'm facing couple of problems.

The password is shown in plain text.

I dont know a proper way to exit the program after the download. it throws out the following error...

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

the code that I have written so far is :

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

Any inputs is highly appreciated..

Thanks. -Vijay

解决方案

There are few errors in the program:

p.interact()

This is used when we want to get back the control after having automatically supplied the password using pexpect module. You don't need to use that since you are automating the whole repository check out.

Also a few things can be improved, after passing the password, set a infinite timeout since it may take a while to copy a git repository.

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

After that you can read all the execution output with the following command

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

you can also use the above to log the output to a file by directly writing to it

Using p.logifile = sys.stdout is not good since it will record pexpect operation from start including passing of password.

After this there is no need to close, you are not running a interactive program. Remove all these lines:

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

The issue is that some where you have to store the password and use it with p.sendline. How ever, you store password, it is going to be insecure. You can also take the input at the start for the password, this way you will not be storing the password within your program but that defeats automation. I don't see a way out but for taking password input, you can do:

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

这篇关于需要pexpect模块一点帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆