Using pipe and ssh to connect commands between different unix hosts. Output local cat, less, etc into remote files. Grep or watch remote logsBy neokrates, written on November 26, 2010 |
howto |
- neokrates
- Email: uwarov@yahoo.com
- Website: http://www.thinkplexx.com
- Join date: 05-31-09
- Posts: 20
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
Loading ...
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 [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!
|
LEARN MORE (amazon bookstore)
|
|
TAGS
|
|
SOCIAL
|
|
INCOMING SEARCH TERMS
|

(1 votes, average: 4 out of 5)

















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:
0
0
Hi Gimple,
seems strange that your first case doesn’t work. Similar “watch” commands definitely worked for me. Like :
Like or Dislike:
0
0
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?
Like or Dislike:
0
0
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.
Like or Dislike:
1
0