Need little assistance with pexpect module

前端 未结 2 1944
余生分开走
余生分开走 2021-01-21 18:03

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.

相关标签:
2条回答
  • 2021-01-21 18:18

    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")
    
    0 讨论(0)
  • 2021-01-21 18:31

    To get rid of the password being echo'd to stdout, use the following when redirecting output -

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

    I have tried this myself and seems to be working. Here is the reference for this revelation.

    0 讨论(0)
提交回复
热议问题