LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2015, 11:51 PM   #1
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Rep: Reputation: Disabled
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.
 
Old 02-10-2015, 12:21 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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.
 
Old 02-10-2015, 12:38 AM   #3
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-10-2015, 01:10 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by Saicharan Burle View Post
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.
 
Old 02-10-2015, 01:22 AM   #5
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
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....
 
Old 02-10-2015, 01:38 AM   #6
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-10-2015, 01:48 AM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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.
 
Old 02-10-2015, 02:11 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by Saicharan Burle View Post
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.
 
Old 02-10-2015, 02:20 AM   #9
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
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 ??
 
Old 02-10-2015, 03:01 AM   #10
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
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....
 
Old 02-10-2015, 03:13 AM   #11
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by Saicharan Burle View Post
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.
 
Old 02-10-2015, 03:22 AM   #12
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
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.....
 
Old 02-10-2015, 03:29 AM   #13
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by Saicharan Burle View Post
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.

Last edited by evo2; 02-10-2015 at 03:36 AM. Reason: Use a tmp file to make it more like what the OP was doing.
 
Old 02-10-2015, 03:40 AM   #14
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
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.

Last edited by Saicharan Burle; 02-10-2015 at 03:45 AM.
 
Old 02-10-2015, 08:32 AM   #15
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

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

Evo2.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find older versions of libx264.so BobNutfield Slackware 16 03-21-2012 08:13 PM
Find Legacy Versions of SUSE wagscat123 SUSE / openSUSE 5 10-15-2011 08:16 PM
where I can find older versions of lirc?. mac1234mac Linux - Hardware 1 04-05-2006 08:32 AM
How to find Versions uma_mahesh_2005 Linux - Software 7 03-31-2006 06:30 AM
how do I find my glibc and gtk+ versions? butthead Linux - Software 1 08-09-2004 01:29 PM


All times are GMT -5. The time now is 01:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration