使用 paramiko 在远程机器上执行 git 命令
execute git command in a remote machine using paramiko
我写了一个代码来在远程服务器上执行 git 命令,但我从未执行过
Step 1: Login to remote server
Step 2: change dir to git repository
Step 3: execute git clean -fdx command
下面是示例代码
try:
ssh = paramiko.SSHClient()
sssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(dummyipaddress, username="john", password="philips")
except (paramiko.BadHostKeyException,
paramiko.AuthenticationException, paramiko.SSHException) as e:
print str(e)
sys.exit(-1)
try:
channel = ssh.get_transport().open_session()
channel.send("cd /path to git dir"+ '\n')
time.sleep(5)
print channel.recv(1024)
channel.send("git clean -fdx"+'\n')
print chan.recv(1024)
except paramiko.SSHException as e:
print str(e)
sys.exit(-1)
但问题是我可以更改到 git 存储库但无法执行 git 命令
您需要在发送 shell 命令之前启动 shell:
channel = ssh.get_transport().open_session()
channel.get_pty() # get a PTY
channel.invoke_shell() # start the shell before sending commands
channel.send("cd /path to git dir"+ '\n')
time.sleep(5)
print channel.recv(1024)
channel.send("git clean -fdx"+'\n')
print chan.recv(1024)
我写了一个代码来在远程服务器上执行 git 命令,但我从未执行过
Step 1: Login to remote server
Step 2: change dir to git repository
Step 3: execute git clean -fdx command
下面是示例代码
try:
ssh = paramiko.SSHClient()
sssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(dummyipaddress, username="john", password="philips")
except (paramiko.BadHostKeyException,
paramiko.AuthenticationException, paramiko.SSHException) as e:
print str(e)
sys.exit(-1)
try:
channel = ssh.get_transport().open_session()
channel.send("cd /path to git dir"+ '\n')
time.sleep(5)
print channel.recv(1024)
channel.send("git clean -fdx"+'\n')
print chan.recv(1024)
except paramiko.SSHException as e:
print str(e)
sys.exit(-1)
但问题是我可以更改到 git 存储库但无法执行 git 命令
您需要在发送 shell 命令之前启动 shell:
channel = ssh.get_transport().open_session()
channel.get_pty() # get a PTY
channel.invoke_shell() # start the shell before sending commands
channel.send("cd /path to git dir"+ '\n')
time.sleep(5)
print channel.recv(1024)
channel.send("git clean -fdx"+'\n')
print chan.recv(1024)