LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need to find out the Apache versions (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-find-out-the-apache-versions-4175533555/)

Saicharan Burle 02-09-2015 10:51 PM

Need to find out the Apache versions
 
Hi team ,

I have a list of servers in one textfile

I need a script which will do an ssh to the list of servers and check for the Apache version using httpd -version command.

evo2 02-09-2015 11:21 PM

Hi,

how about something like the following?
Code:

#!/bin/bash
while read server
do
  echo "httpd on $server is:"
  ssh $server httpd -version
done < textfile

Evo2.

Saicharan Burle 02-09-2015 11:38 PM

My Scenario is like below:

there are some list of servers in one file

file1:
serverA
serverB
serverC

and, we basically do an ssh from the Jump server. I wanted to put my script in Jump server, It will then use a loop function and in that function i can include ssh and print the version of the Apache on that server and exit from the server, Output should be needed in an email.

I tried something like this but not working properly :)

#!/bin/bash
MailList=saicharan.burle@centurylink.com
MDT_Apache_Server_List=/export/apps/foss/apache/support/Server_Lists

for i in `MDT_Apache_Server_List`
do
ssh foss@$i
version=`httpd -version`
echo "version for $i is $version" >> /tmp/version.txt
exit
done
cat /tmp/version.txt | mail -s "Apache Version" $MailList

Help me in writing a loop which will do to file1 and execute the commands and exit from the server's and print an email with the output.

evo2 02-10-2015 12:10 AM

Hi,
Quote:

Originally Posted by Saicharan Burle (Post 5314824)
Help me in writing a loop which will do to file1 and execute the commands and exit from the server's and print an email with the output.

I have shown you how to do the loop. Have you tried it? Exactly what problem are you having?

Evo2.

Saicharan Burle 02-10-2015 12:22 AM

Nothing is coming out.... I again modified script like below:

#!/bin/bash
set -x
MDT_Apache_Server_List=/export/apps/foss/apache/support/Server_Lists //This is the file name which contains servers
cd $MDT_Apache_Server_List
ls > /tmp/abc.txt //I am listing out the servers to one text file
for i in `awk -F: '{print $1}' /tmp/abc.txt` //Loop starts here and will check
do
ssh $server
version=`httpd -version`
echo "httpd on $server is:$version"
done
exit


Output is coming like below:

+ MDT_Apache_Server_List=/export/apps/foss/apache/support/Server_Lists
+ cd /export/apps/foss/apache/support/Server_Lists
+ ls
++ awk -F: '{print $1}' /tmp/abc.txt
+ for i in '`awk -F: '\''{print $1}'\'' /tmp/abc.txt`'
+ ssh
usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-i identity_file] [-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-w tunnel:tunnel] [user@]hostname [command]
++ httpd -version
+ version='Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49'
+ echo 'httpd on is:Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49'
httpd on is:Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49
+ for i in '`awk -F: '\''{print $1}'\'' /tmp/abc.txt`'
+ ssh
usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-i identity_file] [-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-w tunnel:tunnel] [user@]hostname [command]
++ httpd -version
+ version='Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49'
+ echo 'httpd on is:Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49'
httpd on is:Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49
+ for i in '`awk -F: '\''{print $1}'\'' /tmp/abc.txt`'
+ ssh
usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-i identity_file] [-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-w tunnel:tunnel] [user@]hostname [command]
++ httpd -version
+ version='Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49'
+ echo 'httpd on is:Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49'
httpd on is:Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49
+ exit


I do not see the servername , and moreover here in this case i just kept only one server in the MDT_Apache_Server_List file....

Saicharan Burle 02-10-2015 12:38 AM

Evo, i tried further

#!/bin/bash
set -x
MDT_Apache_Server_List=/export/apps/foss/apache/support/Server_Lists
for i in `awk -F: '{print $1}' /export/apps/foss/apache/support/Server_Lists/MDT_Apache_Server_List`
do
ssh $i
version=`httpd -version`
echo "version $i is:$version"
done
exit

Now the thing is , it is doing ssh to the server, But when i do manual exit then the version is printing.

./version.sh
+ MDT_Apache_Server_List=/export/apps/foss/apache/support/Server_Lists
++ awk -F: '{print $1}' /export/apps/foss/apache/support/Server_Lists/MDT_Apache_Server_List
+ for i in '`awk -F: '\''{print $1}'\'' /export/apps/foss/apache/support/Server_Lists/MDT_Apache_Server_List`'
+ ssh qtdenvmdt066.dev.qintra.com

I am doing manual exit here

logout
Connection to qtdenvmdt066.dev.qintra.com closed.
++ httpd -version
+ version='Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49'
+ echo 'httpd on qtdenvmdt066.dev.qintra.com is:Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49'
httpd on qtdenvmdt066.dev.qintra.com is:Server version: Apache/2.2.26 (Unix)
Server built: Jul 18 2014 10:38:49
+ exit

evo2 02-10-2015 12:48 AM

Hi,

Please always use [code] tags when posting scripts (or other code).
Start simple. Implement your script one step at a time.

Eg.

Step 1.
Write a script that can loop over your server names. Eg if you have a file call "servers.txt" containing

Code:

larry
curly
mo

Then write a script like

Code:

while read server
do
  echo "Server is $server"
done < servers.txt

Once that is working try...

Step 2.
ssh to each one.
Code:

while read server
do
  echo "Server is $server"
  ssh $server httpd -version
done < server.txt

Step 3.
Save the output to some file...

Step 4.
Send that file as an email...

Etc...

If you get stuck at some step, post back with a question.

Evo2.

evo2 02-10-2015 01:11 AM

Hi,
Quote:

Originally Posted by Saicharan Burle (Post 5314839)
Now the thing is , it is doing ssh to the server, But when i do manual exit then the version is printing.

You are getting a shell on the remote server, and then when you exit you are running "httpd -version" on the localhost. See the example in my previous post.

Evo2.

Saicharan Burle 02-10-2015 01:20 AM

Evo,

I used for loop and is working,and i am not using httpd -version in local host, I am using it after doing ssh to server... Output is generating properly,

See below script:

#!/bin/bash
set -x
MailList=saicharan.burle@centurylink.com
List=/export/apps/foss/apache/support/Server_Lists
for i in `awk -F: '{print $1}' /export/apps/foss/apache/support/Server_Lists/MDT_Apache_Server_List`
do
ssh $i // ssh to server A
version=`httpd -version`
echo "****************************************" >> /tmp/version.txt
echo "Hostname : $i" >> /tmp/version.txt
echo "****************************************" >> /tmp/version.txt
echo "version $i is:$version" >> /tmp/version.txt
done
cat /tmp/version.txt | mail -s "Apache Version" $MailList
rm /tmp/version.txt
exit // this should come out automatically when the script runs



Now the thing is when i ran the script , It is executing properly... But the exit command is not working along with the script.... I have to manually issue the command ctrl+d to come out,, then the mail generates.....


was ther any syntax for exiting from ssh ??

Saicharan Burle 02-10-2015 02:01 AM

Evo,

I even tried with your code, Null message in the body.

#!/bin/bash
set -x
MailList=saicharan.burle@centurylink.com
TEST_Apache_Server_List=/export/apps/foss/apache/support/Server_Lists // This is nothing but server.txt file for your assumption
while read TEST_Apache_Server_List
do
echo "Server is $TEST_Apache_Server_List"
ssh $TEST_Apache_Server_List `httpd -version`
done < /tmp/version.txt //Here i am appending it to this file
cat /tmp/version.txt | mail -s "Apache Version in Test Environment" $MailList
rm /tmp/version.txt
exit


Output:
---------

./version.sh
+ MailList=saicharan.burle@centurylink.com
+ TEST_Apache_Server_List=/export/apps/foss/apache/support/Server_Lists
+ read TEST_Apache_Server_List
+ cat /tmp/version.txt
+ mail -s 'Apache Version in Test Environment' saicharan.burle@centurylink.com
Null message body; hope that's ok
+ rm /tmp/version.txt
+ exit



Correct me if i am wrong... I wish if you could do something on my previous post if it works or not.... If not can you correct the script

I mentioned the tags in the above script to understand better....

evo2 02-10-2015 02:13 AM

Hi,
Quote:

Originally Posted by Saicharan Burle (Post 5314854)
I used for loop and is working,and i am not using httpd -version in local host, I am using it after doing ssh to server... Output is generating properly,

Really? Are you sure? As I said before start with something very simple to verify that it really is doing what you think. And please, please, please use [code] tags. Again, to understand and debug your script, reduce it to the bare essentials. The scripts you are posting don't make sense to me.

For example:

- "//" in bash will not comment out the rest of the line
"ssh $i // ssh to server A"
should return an error like:
"bash: //: Is a directory"

- Given what you said was the format of your text file with host names, the gymnastics you are doing with awk shouldn't work

Evo2.

Saicharan Burle 02-10-2015 02:22 AM

Evo,

I've used the code tags in my earlier post to understand to you, My apologies if i miss any tags.

To answer your question, Yes, It is not working with the while loop, I tried multiple times and of no use....

Let me re-arrange my script with neat codes....

#!/bin/bash
set -x
MailList=saicharan.burle@centurylink.com
TEST_Apache_Server_List=/export/apps/foss/apache/support/Server_Lists //This "TEST_Apache_Server_List" is a text file something like all the servers listed in the file //
while read TEST_Apache_Server_List //Reading the file
do
echo "Server is $TEST_Apache_Server_List" // this will print the first server say serverA
ssh $TEST_Apache_Server_List httpd -version // doing ssh to serverA and executing the httpd -version command
done < /tmp/version.txt // Output should be in some text file , Here i used version.txt
cat /tmp/version.txt | mail -s "Apache Version in Test Environment" $MailList
rm /tmp/version.txt
exit


Output:
---------------

Nothing is coming in the email, Empty Body.....

evo2 02-10-2015 02:29 AM

Hi,
Quote:

Originally Posted by Saicharan Burle (Post 5314864)
Correct me if i am wrong... I wish if you could do something on my previous post if it works or not.... If not can you correct the script

I can "fix" your script for you but I had assumed you actually wanted to learn this for yourself. Here it is, in all it's untested glory.
Code:

#!/bin/bash
f="/export/apps/foss/apache/support/Server_Lists/MDT_Apache_Server_List"
(while read server ; do
    echo $server
    ssh $server httpd -version
done < $f ) >> /tmp/version.txt
cat /tmp/version.txt | mail -s "Apache Versions" saicharan.burle@centurylink.com
rm /tmp/version.txt

Evo2.

Saicharan Burle 02-10-2015 02:40 AM

Evo,

I ran the above script, seems like , something need to look at "ssh $server httpd -version" command

it can print the server name from the file "f" but the httpd command not found
bash: httpd: command not found

./version.sh: line 5: httpd: command not found
Pseudo-terminal will not be allocated because stdin is not a terminal.

evo2 02-10-2015 07:32 AM

Hi,

Ok, now we are getting somewhere. Try using the full path. Eg /usr/sbin/httpd

Evo2.


All times are GMT -5. The time now is 11:52 PM.