Fetching both message logs from each of the remote servers is fairly easy. However, you don't seem to be using the correct tool for the job. This answer presumes that you are familiar with creating dsa keys to allow passwordless connections between the hosts with ssh-keygen
and that is setup and working properly. I will also presume you have needed permission to copy the message logs.
The correct tool for the job is rsync
(others will work, but rsync
is the defacto standard). What you want to do to retrieve the files is:
rsync -uav 10.30.3.2:/var/log/messages /path/to/store/messages.10.30.3.2
This will get /var/log/messages
on 10.30.3.2
and save it on the local machine at /path/to/store/messages.10.30.3.2
.
Now if you want to modify it in some way as your echo -n > /var/log/messages
suggest before using rsync to retrieve the messages log, remember ssh
will execute any command you tell it to on the remote host. So if you want to enter something in the remote log before retrieving it, then you can use:
ssh 10.30.3.2 "echo -n 'something' > /var/log/messages"
(I'm not sure your reason for suppressing the newline in echo... but to each his own) Another trick for executing multiple commands on 10.30.3.2
easily is to create a script on 10.30.3.2
that does what you need and make sure it has the execute bit set. Then you can run the script on 10.30.3.2
from your machine by ssh:
`
ssh 10.30.3.2 /path/to/your/script.sh
If this hasn't answered your question, send a comment. It was somewhat unclear what your were actually attempting to do from your post.
after comment
It is still unclear what you are trying to do. It appears that you want to echo
the hostname
and date
then truncate the messages file by echo -n > /var/log/messages
, then ssh 10.30.3.2
truncate its /var/log/messages
file and after the ssh command completes exit
the script before you echo "Welcome"
. You see, when ssh 10.30.3.2 "echo -n > /var/log/messages "
completes, your next exit
command causes the script you are running to exit
. You don't need that exit
there.
second addendum:
Let's do it this way. You want to run the same commands on each host and you want to be able to run those commands on a remote host via ssh
. So let's create a script on each box in /usr/local/bin/empty_message_log.sh
that contains the following:
#!/bin/bash
echo "Hello $HOSTNAME" # echos Hello hostname to terminal
date # echos date to terminal
echo -n > /var/log/messages # truncates /var/log/messages to (empty)
if [ "$HOSTNAME" == "fillin localhost hostname" ]; then
# runs this same script on 10.30.3.2
# only run if called on local machine
ssh 10.30.3.2 /usr/local/bin/empty_message_log.sh
fi
echo "welcome" # echos welcome and exits
Now make sure it has the execute bit set:
chmod 0755 /usr/local/bin/empty_message_log.sh
# adjust permissions as required
Put this script on all the hosts you want this capability on. Now you can call this script on your local machine and it will run the same set of command on the remote host at 10.30.3.2. It will only call and execute the script remotely if "fillin localhost hostname"
matches the box it is run on.