I just read the following post Python – Script – Which Webserver Does That Site Run? by blogger Corey Goldberg.

I prefer the shell version:

$ what-http-server() { curl -s -I "http://$1" | awk -F': ' '/^Server:/ {print $2}'; }
$ what-http-server www.pylot.org
Apache/2.0.52
$ what-http-server() { curl -s -I "$@" | awk -F': ' '/^Server:/ {print $2}'; }
$ what-http-server www.pylot.org google.com bashcurescancer.com
Apache/2.0.52
gws
Apache/2.2.6 (Unix)

That works but this version is more correct:

what-http-server() { curl -s -I $(for h in "$@"; do printf "http://%s " "$h"; done) | awk -F': ' '/^Server:/ {print $2}'; }

In the version which works for multiple hosts, we are letting curl assume the protocol is HTTP. This works fine most of the time. However, there are exceptions:

If you specify URL without protocol:// prefix, curl will attempt to guess what protocol you might want. It will then default to HTTP but try other protocols based on often-used host name prefixes. For example, for host names starting with “ftp.” curl will assume you want to  speak FTP. – man curl

8 Responses to “Shell Function – Which Webserver Does That Site Run?”

  1. Andrey Klyachkin Says:

    what-http-server() { while test “$1″ ; do exec 3/dev/tcp/$1/80; echo -e “GET / HTTP/1.0\n\n” >&3; cat <&3 | awk -F’: ‘ ‘/^Server:/ {print $2}’; shift ; done ; }

    will not work on debian-based distributions

  2. Andrey Klyachkin Says:

    lost some code:
    exec 3 <>/dev/tcp/$1/80

  3. Mathias Says:

    Try httprint. It’s way more reliable…

  4. Brock Noland Says:

    Andrey,

    See this BASH only http client.

    Mathias,

    Thanks for the link. I think he was just showing a very basic identifier as there are many full fledged options out there.

    Brock

  5. ubersoldat Says:

    Hi!

    curl in not default on some distros, so might want to use
    $wget -d 2>&1 | grep Server

    Bye!

  6. ubersoldat Says:

    Sorry, the command should be:

    $wget http://www.apache.org –spider -d 2>&1 | grep Server

    Bye

  7. Brock Noland Says:

    ubersoldat,

    Yep, if curl is not available, then this works:

    wget -S -O - --spider http://bashcurescancer.com/ 2>&1 | awk -F': ' '/^ *Server:/ {print $2}'
    
  8. Brock Noland Says:

    I think I need to hack on wordpress’s input filter. It sucks for those of us trying to input stuff other than text.

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) brock (at) gmail dot com.