Results 1 to 4 of 4

Thread: Shell script to ping/see if port 80 is open?

  1. #1
    Join Date
    Oct 2005
    Beans
    466

    Shell script to ping/see if port 80 is open?

    Hi, I'm looking to write a shell script that will ping port 80 of localhost

    If port 80 is closed then the script should send an email to email warning that apache has crashed.

    Can anyone help point me in the right direction?

  2. #2
    Join Date
    Apr 2008
    Beans
    286

    Re: Shell script to ping/see if port 80 is open?

    Code:
    echo -n "GET / HTTP/1.0\r\n\r\n" | nc 127.0.0.1 80 || /path/to/emailing/script
    This will try to send a GET request to 127.0.0.1 at port 80, if there is no reply it will run the script found at "/path/to/emailing/script"

    Schedule it as a cron job.

    Hope this helps...

  3. #3
    Join Date
    Mar 2007
    Location
    Your Closet
    Beans
    380
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell script to ping/see if port 80 is open?

    Great solution very nice and simple. But wouldn't you want echo to interprate the escapes? If I'm right then this might be the correct way.
    Code:
    echo -e "GET / HTTP/1.0\r\n\r" | nc 127.0.0.1 80 || /path/to/emailing/script
    I could be wrong.

    Edit:
    Another possibility...
    Code:
    echo GET / | nc 127.0.0.1 80
    Last edited by bashologist; April 28th, 2011 at 10:07 PM.
    ...

  4. #4
    Join Date
    Nov 2007
    Location
    Wisconsin
    Beans
    1,139

    Re: Shell script to ping/see if port 80 is open?

    Code:
    netstat -tupln | grep :80

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •