使用parami在远程计算机中执行git命令

2024-09-30 05:15:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一段代码来在远程服务器上执行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命令


Tags: to代码git命令cleanparamikostepdir
1条回答
网友
1楼 · 发布于 2024-09-30 05:15:46

在发送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)

相关问题 更多 >

    热门问题