У меня есть сервер, размещенный на AWS EC2. В основном я обновляю версию сервера, используя чистую команду git из bitbucket.
Использование чего-то типа:
ssh-agent /bin/bash
ssh-add .ssh/bitbucket_key
cd /var/www/my-git-bucket
git pull
это довольно ручной процесс. Итак, я хочу сделать это с помощью библиотеки Python + Paramiko.
Но на самом деле это не работает.
NB: я вошел на этот сервер с помощью paramiko + ssh key
Как я могу использовать python + paramiko для обновления из git repo?
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))
Это работает для меня.