1

I have written a code to execute git command in a remote server but i never executed

Step 1: Login to remote server
Step 2: change dir to git repository
Step 3: execute git clean -fdx command

Below is the sample code

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)

But the issue is that i can able to change to git repository but not able to execute git command

pynexj
  • 19,215
  • 5
  • 38
  • 56

1 Answers1

0

You need to start a shell before sending shell commands:

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)
pynexj
  • 19,215
  • 5
  • 38
  • 56
  • Hi whjm if i want to add one more channel.send("git reset --hard") how i will do that for me after 2 commands 3rd command is not executing – saswati das Oct 25 '16 at 08:27
  • `channel.send("git reset --hard")` -- You're missing `\n`. – pynexj Oct 25 '16 at 08:33
  • i have put channel.send("git reset --hard"+'\n') after 2nd command ran output was at > – saswati das Oct 25 '16 at 08:51
  • Don't know what you exactly mean by *output was at >*. Try debugging it with `pdb` and provide more information. Ask a new question if necessary as it's not easy for long discussions in *comments*. – pynexj Oct 25 '16 at 08:59