running tail -f on a server which connects to another server over ssh

2

2

I have been using the following command to connect to a server, which connects to another server and runs tail -f on it:

ssh server1.com ssh server2.com tail -f file.log

This so far works fine. However, when I want to quit the tail process by pressing ctrl+c, I do not quit the tail process, but the ssh process. This leaves the tail process on the remote server running, which obviosly is not a very good idea.

Does any one have an idea how I could fix this command line? I also would be interested if there is tool, running on os x, that would allow to comfortably switch between viewing different files on the remote server.

st-h

Posted 2011-11-30T13:59:32.400

Reputation: 123

Couldn't you use tmux or screen on the remote servers? That's what I've always done. – Rob – 2011-11-30T14:10:51.760

See this Q&A : To get it working in command line or in the shell, the ssh agent shall be forwarded too, eg using the ssh option "A" (see ssh man page), like :

ssh -A user_foo@serverB "my_cmd"

– hornetbzz – 2011-11-30T16:25:24.090

Answers

1

If you have netcat installed on server1.com (you probably do), you may want to use the ssh directive ProxyCommand to seamlessly hop across server1.com; thus, when you press Ctrl+C, it will only terminate the command on server2.com, not your SSH session.

Example of your ~/.ssh/config (create the file if it doesn't exist; append to end if it does):

Host server2.com
  User piskvor
  ProxyCommand ssh -q server1.com nc -q0 server2.com 22

What happens here:

  • ssh connects to server1.com
  • it remotely connects from there to server2.com (using nc)
  • which ferries the data through server1.com

This is completely transparent to your ssh client, so you can work with server2.com as if you were connected directly (e.g. SFTP, X forwarding, TCP forwarding, etc.)

For a more detailed explanation (as well as extending this to multiple hops), see this article, or this similar question on SU.

Piskvor left the building

Posted 2011-11-30T13:59:32.400

Reputation: 2 277

Thanks. After some more configuration this gives me a nice shortcut to ssh to server2: "ssh server2.com"... However, if I use "ssh server2.com tail -f file.log" I end up having the same problem. Ctrl+c kill the ssh connection, and leaves the tail process running. I want to run this command in a script, which opens multiple windows etc... so, I can not just ssh to server2 and manually type in the tail command. – st-h – 2011-11-30T14:46:40.853

Accepting your answer, as I never found myself wanting to script tail commands on remote servers, as connecting got much easier using your suggested configuration... I guess there is no more need to find an answer for my initial question – st-h – 2012-03-12T19:05:26.860