linux自带pexpect库,所以不用安装了,有可能需要安装git库
使用bash脚本没办法在git clone https:之后捕捉到输入账号和密码,使用pexpcet就可以,属于expect的python封装版
代码示例
import sys
import getpass
import pexpect
class GitTool(object):
def __init__(self):
# INPUT
self._user = input('请输入GIT用户名: ')
self._password = getpass.getpass('请输入GIT密码: ')
def clone(self, git_url):
child = pexpect.spawn(f'git clone {git_url}') # 要运行的指令 也可以换成ssh
child.expect('Username') # 获取包含Username的返回
child.sendline(self._user) # 发送用户名
child.expect('Password', timeout=None)
child.sendline(self._password)
if __name__ == '__main__':
_git = GitTool()
_git.clone('https://github.com/ssssss/ssss')```
写了一个最简短的例子,mac也可以用,加油。。。