【问题标题】:Execute Git Command with SSH agent using python pramiko使用 python pramiko 使用 SSH 代理执行 Git 命令
【发布时间】:2014-10-05 09:15:52
【问题描述】:

我有一台托管在 AWS EC2 上的服务器。基本上我使用来自 bitbucket 的纯 git 命令更新服务器版本。

使用类似的东西:

ssh-agent /bin/bash
ssh-add .ssh/bitbucket_key
cd /var/www/my-git-bucket
git pull

这是一个非常手动的过程。所以,我想使用 Python + Paramiko 库来做到这一点。

但它实际上不起作用。

注意:我使用 paramiko + ssh key

登录到该服务器

如何使用 python + paramiko 从 git repo 更新?

【问题讨论】:

    标签: python git ssh amazon-ec2 paramiko


    【解决方案1】:
    def remote_exec(cmd_str, hostname, username, password, port, timeout=None):
        """
        execute command remotely
        :param cmd_str:
        :param hostname:
        :param username:
        :param password:
        :param port:
        :param timeout:
        :return:
        """
    
        try:
            max_size = 120 * 1024 * 1024
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect(hostname=hostname, port=port, username=username, password=password, timeout=10)
            channel = client.get_transport().open_session()
            if timeout is None:
                timeout = 100
            channel.settimeout(timeout)
            channel.exec_command(cmd_str)
            content = ""
            data = channel.recv(1024)
            # Capturing data from channel buffer.
            while data:
                content += data
                data = channel.recv(1024)
            status, response, error = channel.recv_exit_status(), content, channel.recv_stderr(max_size)
            client.close()
            final_output = unicode(response) + unicode(error)
            return [status, final_output]
        except Exception, e:
            return [1, unicode(e)]
    print(remote_exec("cd /var/www/my-git-bucket;git status", "127.0.0.1", "username", "password", 10022))
    

    这对我有用。

    【讨论】:

    • 仅供参考,我们使用 ssh key 登录服务器,并且还使用 ssh key 进行 git 更新,而不是密码
    猜你喜欢
    • 2020-03-08
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    相关资源
    最近更新 更多