Using pipe and ssh to connect commands between different unix hosts. Output local cat, less, etc into remote files. Grep or watch remote logs

By neokrates, written on November 26, 2010

howto

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 3,263 views
Rate it
Ad
Poll
  • Which SQL implementations are mostly used in your projects?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

Ever wanted to grep through the remote file like you do on your local host? Log in and logout just to check the server config may be avoided. Many Linux / unix commands let you to connect local STDOUT to remote STDIN or vice versa. Here are some use cases which may make your life simpler.

Works for:

✔ most Linux distros

✔ unix systems

✔ CygWin

✔ openSsh

Preconditions

Your user has public / private key pair without pass phrase.
So, you can execute remote commands without any interaction.

1

Write local output into remote file

You just installed the apache on your $myhost. Now you want to test it.

You can echo the test string into file, only this time the file is in remote htdocs root:

echo "It works" | ssh user@your.host 'cat >> /usr/local/apache/htdocs/it_works.html'

 
Then you open the browser and should see http://$myhost/it_works.html

2

Open remote file with less or cat

With cat:

ssh user@your.host "cat /usr/local/apache/htdocs/it_works.html"

 
With less:

ssh user@your.host "less /usr/local/apache/htdocs/it_works.html"

3

Grep remote files

With pipe:

ssh user@your.host "cat /usr/local/apache/htdocs/it_works.html" |  grep "works"

 
Just remote grep:

ssh user@your.host "grep works /usr/local/apache/htdocs/it_works.html"

4

Watching remote logs with tail

This way you can watch the access to apache server in real-time:

ssh user@your.host "tail -f /etc/httpd/logfiles/access_log"

 

Have fun! ;)

Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +1 (from 3 votes)
2 votes 'YES'  1 votes 'NO'


TAGS

SOCIAL
Be Sociable, Share!

INCOMING SEARCH TERMS


4 Responses to “Using pipe and ssh to connect commands between different unix hosts. Output local cat, less, etc into remote files. Grep or watch remote logs”

  1. Gimple says:

    Have you tried using watch with ssh i.e.
    ssh user@system “watch cat /var/log/auth.log”
    I know it can be done otherway round i.e.
    watch ssh user@system “cat /var/log/auth.log”
    In first case it doesnt work. I dont know why
    In second case it works, but it reconnects the ssh connection everytime, that can take a while EVERYTIME. I know theres a workaround to this as well but using connection sharing but it wont be nice if we could just use the first one. Any ideas on why the first one doesnt work ?
    cheers
    gimple

    • neokrates says:

      Hi Gimple,
      seems strange that your first case doesn’t work. Similar “watch” commands definitely worked for me.  Like :

      ssh user@your.host "less /usr/local/apache/htdocs/it_works.html"
      
      Does "less" command work for you, or not?
      
      BR // Neo

       

      • Sergani says:

        Hi neo,
        I have a server running special software, and my monitoring tool is to run the following:
        watch -n 1 “monitoring command”
        I’m trying to do:
        ssh user@IP ‘watch -n 1 “monitoring command”‘
        but it’s giving:

        Error opening terminal: unknown.
        Any thoughts guys?

        • Without knowing much about your system, it is hard to say. Here is one common cause for that problem. It may be failing to ID you term type… Try to set it before watch.

          ssh user@IP ‘export TERM=vt100 && watch -n 1 “monitoring command”‘

          OR

          ssh user@IP ‘export TERM=ansi && watch -n 1 “monitoring command”‘

          Hope did no mistake in syntax. That should work if remote shell type is bash.

           

Leave a Reply