Repeatedly running commands over SSH

1,443 views
Skip to first unread message

dane

unread,
Sep 19, 2013, 2:33:10 PM9/19/13
to golan...@googlegroups.com
Hello everybody,

I want to repeatedly run one (or several commands) over SSH to check the load on the server (and maybe other things).
This is what I got so far:


First question: Is there a better way to do this?

Second question: Can I somehow get rid of the user@host:~$  thing in the ssh stdout,
so that the output is actually only the output of the command I enter.

Third question: Is it guaranteed, that I get all the output of the command with one read on the
stdout of the ssh session?

Thanks for your help!

Best Regards
David

James Bardin

unread,
Sep 19, 2013, 3:17:14 PM9/19/13
to golan...@googlegroups.com


On Thursday, September 19, 2013 2:33:10 PM UTC-4, dane wrote:
Hello everybody,

I want to repeatedly run one (or several commands) over SSH to check the load on the server (and maybe other things).
This is what I got so far:


First question: Is there a better way to do this?


I would highly suggest *not* using a shell. I believe what you want is the Output or CombinedOutput (or even Run) methods (I haven't yet used go's ssh library, but I am very familiar some others).

This isolates your commands, and lets you wait for the process to complete, and get an exit status of some sort.


Second question: Can I somehow get rid of the user@host:~$  thing in the ssh stdout,
so that the output is actually only the output of the command I enter.


This is output by the shell, see my previous comment. When you have a shell, you have to parsing all the output, including command prompts, to determine when your process is actually complete. You end up needing something like the `expect` command.
 
Third question: Is it guaranteed, that I get all the output of the command with one read on the
stdout of the ssh session?


Not if you're using a shell, you have to poll for a command prompt, or some other indicator that your command ran to completion. 
 

dane

unread,
Sep 19, 2013, 3:32:20 PM9/19/13
to golan...@googlegroups.com
I realize that I could use separate sessions and the Run method, but then every session has to establish a new ssh connection, right?
So it could be possible that the goroutine cannot keep up at higher polling frequencies.

Keith Rarick

unread,
Sep 19, 2013, 3:42:24 PM9/19/13
to dane, golan...@googlegroups.com
You can call NewSession repeatedly on a single ssh client connection. Use the Output method on each session as James suggested.

You might also consider github.com/kr/sshpool if you'll be juggling  connections to many hosts. 
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

James Bardin

unread,
Sep 19, 2013, 3:44:49 PM9/19/13
to golan...@googlegroups.com


On Thursday, September 19, 2013 3:32:20 PM UTC-4, dane wrote:
I realize that I could use separate sessions and the Run method, but then every session has to establish a new ssh connection, right?

No, NewSession is a method of a connection. You con't need to create a new connection each time. A Session seems to be what this library calls a channel for the client, and many channels are multiplexed in a single connection.
 
So it could be possible that the goroutine cannot keep up at higher polling frequencies.


I doubt there's any polling going on here, unless it's in your code.
 

dane

unread,
Sep 19, 2013, 4:24:50 PM9/19/13
to golan...@googlegroups.com
Ok, that makes more sense, I was somehow a bit confused by the documentation.
Reply all
Reply to author
Forward
0 new messages