-1

I am new to this concept so please help!

I am using Paramiko Channel to execute a command and start a trace. I want the trace to continue running until I send a stop request to trace. If I use channel.send(cmd) it starts and stops the trace whereas I want that the trace should be continue and only stop when I send any stop request as I have other actions to perform before stopping the trace.

Note: I am doing SSH in one machine with Paramiko and starting a new channel in that machine to ssh into one more machine to start the trace there. I tried exec_command but it closes the socket.

Thing I want to know:

  1. How can I start the trace and not let the command stop it before returning
  2. How to stop the trace after I perform my actions after starting the trace.

The code I am using:

    chan = sshClient.get_transport().open_session() 
    chan.get_pty()
    chan.invoke_shell()
    chan.send(cmdline +'\n')
    while not sshClient.recv_ready():
        print("Waiting")
    resp = chan.recv(9999)
    print(resp)

Thanks for your help in advance!

Martijn van Wezel
  • 1,120
  • 14
  • 25

2 Answers2

0

The following example shows that in the while loop that goes for-ever you execute a command and then print the output. After this is finished it sends again a command

import base64
import paramiko
import time

key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('ssh.example.com', username='strongbad', password='thecheat')
while True: 
  time.sleep(5)
  stdin, stdout, stderr = client.exec_command('ls')
  for line in stdout:
     print('... ' + line.strip('\n'))
client.close()

It is possible that you need to send a newline \n after each command so that client.exec_command('ls') becomes -> client.exec_command('ls\n')

Martijn van Wezel
  • 1,120
  • 14
  • 25
  • Hey @MartijnvanWezel , Thanks for your response. but this will keep on running the "ls" command again and again. I have one command which if I run manually, it starts and stops when do crtl+c. But when run through paramiko it stops the command by itself. I need the command to run and stop only when I send the stop request through the channel. – Prabhleen Kaur Nov 05 '19 at 00:29
  • I’m having trouble understanding your question. Is this a school assignment? You should use some method to catch `ctrl+c` maybe? https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python – Martijn van Wezel Nov 05 '19 at 22:43
0

Though it's late, putting it across :

exec_command will close the channel as soon as command is executed.

You may try using something like :

# Get a client
chan = client.invoke_shell()
time.sleep(1) ## You may test with smaller time as well & see what fits best
chan.send('<command>\r')
output = chan.recv(4096)
print(output)
while <Event_is_not_yet_triggered> :
    time.sleep(10)
chan.send('\x03') # Send Ctrl+C
time.sleep(1)
output = chan.recv(4096)
client.close()

In the end it really depends on the server to which you are connected allows. Above will work with most of them.

rajat
  • 1
  • 1