使用pexpect模块自动拉取git仓库代码

2018/05/15 python

从github拉取代码时,经常需要输入密码,可使用pexpect模块免交互拉取。

目录

Pexpect的用途

主要用于:

  • 自动化交互应用程序,如ssh, ftp, passwd, telnet等
  • 软件包的自动安装脚本
  • 软件自动化测试

使用pexpect模块自动拉取git仓库代码

使用git拉取代码时,每次输入git pull命令后都需要手动输入一遍密码,如果动作较频繁,非常耗费时间。

1.jpg

可使用pexpect模块,免交互执行拉取动作。

#/usr/bin/python
# -*- coding: utf-8 -*-
from pexpect import *
import sys

my_secret_password='my_password'

child = spawn('git pull')
child.expect('Password: ')
child.sendline(my_secret_password)
#print child.before   # The before property will contain all text up to the expected string pattern. 
print child.after     # The after string will contain the text that was matched by the expected pattern. 
child.interact()      # Give control of the child to the user.

脚本输出:

print child.after

1.jpg

print child.before

2.jpg

通过以上两图对比可知,child.before里面存放的为匹配字符串之前的所有内容,child.after里面存放的为匹配到的字符串。

因此,print child.after中有**Password: **而print child.before没有。

Search

    Table of Contents