Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use curl to get web server type?

Tags:

bash

shell

curl

Here's some of the lines from my shell script:

curl -I --connect-timeout 60 google.com|grep "Server:" >> 1000-server.txt
curl -I --connect-timeout 60 yahoo.com|grep "Server:" >> 1000-server.txt
curl -I --connect-timeout 60 bing.com|grep "Server:" >> 1000-server.txt

My script is getting the web server type correctly in most cases:

Server: gws
Server: ATS
Server: Microsoft-IIS/8.5

However, if the curl connection times out or get an error, it doesn't output any message. Also, if for some reason grep doesn't find anything, it doesn't output any error message.

If curl or grep fails, is there any way to output an error message that just says something like "ERROR: NOT FOUND"?

Sorry if this is a newbie questions, just can't figure it out...

like image 520
Edward Avatar asked Jan 21 '26 21:01

Edward


1 Answers

curl -I --connect-timeout 60 google.com|grep "Server:" >> 1000-server.txt || echo ERROR | tee -a 1000-server.txt

The curl ... | grep ... command will return false if grep doesn't find the string. In that case, the || echo ERROR | tee -a 1000-server.txt will execute.

The output from echo is piped into tee, which, in turn, will append (-a) the output to the file 1000-server.txt also printing the same output to STDOUT.

like image 55
Bruno Negrão Zica Avatar answered Jan 23 '26 12:01

Bruno Negrão Zica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!