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. |
Hi,
how about something like the following? Code:
#!/bin/bash |
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. |
Hi,
Quote:
Evo2. |
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.... |
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 |
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 Code:
while read server Step 2. ssh to each one. Code:
while read server 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. |
Hi,
Quote:
Evo2. |
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 ?? |
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.... |
Hi,
Quote:
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. |
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..... |
Hi,
Quote:
Code:
#!/bin/bash |
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. |
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. |