0

I would like to execute a ssh command and pipe the output to a file.

In general I would do:

ssh user@ip "command" >> /myfile

the problem is that ssh close the connection once the command is executed, however - my command sends the output to the ssh channel via another programm in the background, therefore I am not receiving the output.

How can I treat ssh to leave my shell open?

cheers

sven

sven
  • 59
  • 1
  • 3
  • 1
    Please post how you are 'sending data to the ssh channel in the background'. This makes no sense to me as written. – bmargulies Jan 28 '10 at 13:52
  • the system I am speaking about is like a cisco router. the command I am executing enables debugging and multiple steps are required, for example: 1. diag debug en 2. diag debug application proxy 1 both commands are executed and I am placed back to the "shell". as soon as something is triggered the output of the debugging is shown on the cli. however I want to include this in a script and if I execute the commands the ssh channel is closed once the command finished executing. cheers sven – sven Jan 28 '10 at 14:01
  • See this possibly similar question: http://stackoverflow.com/questions/1633750/ssh-with-command-plus-the-shell – Dennis Williamson Jan 28 '10 at 17:07

2 Answers2

1

My understanding is that command starts some background process that perhaps will write some output to the terminal later. If command terminates before that the ssh session will be terminated and there will be no terminal for the background program to write to.

One simple and naive solution is to just sleep long enough

ssh user@ip "command; sleep 30m" >> /myfile

A better solution than sleep would be to wait for the background process(es) to finish in some more intelligent way, but that is impossible to say without further details.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • thanks a lot. but the remote system is a closed system with no access to a real shell (think of a cisco ssh shell), so no custom commands are allowed. To simplify what I need is I want to have the following command executed without closing the ssh channel once the execution finished (this must be done without chaning anything at the "command" itself, so no remote changing): ssh user@ip "command" – sven Jan 28 '10 at 15:03
0

Something more powerful than bash would be Python with Paramiko and PyExpect.