0

I want to automate the below scenario:
I have two servers connected in a n/w. If wanted to fetch the /var/logs/messages on both servers. I try to automate it as below but I couldnt proceed from step4 as after logging into the other server the process does not run anymore.

  1 echo "Hello $HOSTNAME"
  2 date
  3 echo -n > /var/log/messages
  4 ssh 10.30.3.2;echo -n > /var/log/messages;exit
  5 ls
  6 cp /var/log/messages > my_bug_log.txt
  7 ssh 10.30.3.2;cp /var/log/messages > my_bug_log.txt

How to automate and fetch the logs from both servers ?

EDITED :

  1 #!/bin/bash
  2
  3 echo "Hello $HOSTNAME"
  4 date
  5 echo -n > /var/log/messages
  6 ssh 10.30.3.2 "echo -n > /var/log/messages ";exit
  7 echo "welcome"

The echo "welcome" is not executed after exiting from the other host.

EDITED :

ssh 10.30.3.2 "cd /var/log" "touch bug_iteration_$i" "cp /var/log/messages > bug_iteration_$i"

Angus
  • 12,133
  • 29
  • 96
  • 151
  • You will probably find your answer here: http://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-shell-script-on-a-remote-machine – Ashalynd Aug 03 '14 at 11:51

2 Answers2

1

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.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Thanks, I missed the ssh 10.30.3.2 "echo ....", hence it was logging into the otherhost and was not executing the next command. But now I could not execute any commands after logging into the server again. Please find the updated script above. – Angus Aug 03 '14 at 08:55
  • David , ssh 10.30.3.2 "cd /var/log" "touch bug_iteration_1" "cp /var/log/messages > bug_iteration_1" I'm trying to execute 3 commands simulaaneously on the other server. But this doesnt create a file on the other host. Please help in identifying the mistake. – Angus Aug 03 '14 at 11:23
  • I wanted to create the file in the other server and copy the contents of /var/log/messages to it. – Angus Aug 03 '14 at 11:25
  • I'm getting a message as "cp: missing destination file operand after `/var/log/messages'Try `cp --help' for more information. " – Angus Aug 03 '14 at 11:27
  • But I have provided the destination file for the cp – Angus Aug 03 '14 at 11:27
  • Is there any possible way to create a file and copy the /var/log/messages residing on the server to the file from the remote server. – Angus Aug 03 '14 at 11:43
  • rsync will copy the file from a remote server to another server. But I wanted to create and copy on a remote server itself. I dont want to copy from one server to another server – Angus Aug 03 '14 at 11:44
  • 1
    Ok, then it is time to create a small script on `10.30.3.2` to executed the series of commands. You can use the same script on all hosts. Then you will just call the script with `ssh 10.30.3.2 scriptname` – David C. Rankin Aug 03 '14 at 19:33
0

Did you consider using scp to fetch the files you need?

Apart from that, if you need to perform the same actions of multiple machines, you might look at ansible (http://www.ansibleworks.com)

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
  • I dont want to ffetch the files from the other server. I wanted to copy the contents of the /var/log/messages on the same server itself. – Angus Aug 03 '14 at 11:36
  • Ah got it. Then may be ansible might work for you. Otherwise there is also csshx (mac only) that is a simple way to do the same commands on multiple servers. – Ashalynd Aug 03 '14 at 11:39
  • Thanks. But I wanted to do with shell scripts. I wanted to create and copy a the contents residing local to the server to a file on 10.31.3.2 by executing a shell script from remote server. – Angus Aug 03 '14 at 11:39