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

Rate it
  • 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4 out of 5)
    Loading ... Loading ...
Ad
Poll
  • What computer language you specialize in?

    • Java (71%, 10 Votes)
    • PHP (29%, 4 Votes)
    • Scala (0%, 0 Votes)
    • Ruby (0%, 0 Votes)
    • C# (0%, 0 Votes)
    • Basic (0%, 0 Votes)
    • Python (0%, 0 Votes)
    • C (0%, 0 Votes)
    • C++ (0%, 0 Votes)
    • Other (0%, 0 Votes)

    Total Voters: 14

    Vote

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext
Most popular search terms:

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:

[v] most Linux distros
[v] unix systems
[v] CygWin
[v] 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 [email protected] "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 [email protected] "cat /usr/local/apache/htdocs/it_works.html"

 
With less:

ssh [email protected] "less /usr/local/apache/htdocs/it_works.html"

3

Grep remote files

With pipe:

ssh [email protected] "cat /usr/local/apache/htdocs/it_works.html" |  grep "works"

 
Just remote grep:

ssh [email protected] "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 [email protected] "tail -f /etc/httpd/logfiles/access_log"

 

Have fun! ;)

 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +2 (from 2 votes)
2 votes 'YES'  0 votes 'NO'

LEARN MORE (amazon bookstore)

TAGS

RELATED
Pages
Posts
    nope :(

SOCIAL
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • BlinkList
  • Blogosphere News
  • E-mail this story to a friend!
  • Furl
  • LinkArena
  • Live
  • MisterWong
  • Print this article!
  • StumbleUpon
  • Technorati
  • Webnews.de
  • YahooMyWeb

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

    Like or Dislike: Thumb up 0 Thumb down 0

Leave a Reply