Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15
I want to create a script which verifies if my network is up every 5 minutes. If it's not up, trying to start my network. Does someone has that kinda ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Dec 2003
    Location
    Netherlands
    Posts
    193

    Networking scripts


    I want to create a script which verifies if my network is up every 5 minutes.

    If it's not up, trying to start my network.

    Does someone has that kinda script, or could someone tell me how to write that kinda script
    Computers Are Like Air Conditioners... They\'re both useless with Windows open!

  2. #2
    Linux Guru
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    3,284
    create directory:
    Code:
    mkdir /etc/cron.5
    open /etc/crontab, add the following lines to the file:

    Code:
    5 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    10 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    15 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    20 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    25 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    30 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    35 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    40 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    45 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    50 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    55 * * * * /usr/bin/run-parts /etc/cron.5 1> /dev/null
    Create file /etc/cron.5/mynet.sh:
    Code:
    #!/bin/sh
    
    /sbin/ifup eth0
    make executable:
    Code:
    chmod 755 /etc/cron.5/mynet.sh
    Done

  3. #3
    Linux Newbie
    Join Date
    Dec 2003
    Location
    Netherlands
    Posts
    193
    I've had only one problem with this scripts. On of my network devices is DHCP. So i've killed 1500 processes from dhclient. Could you please advise me how to deal with this?
    I know that dhclient has a pid file, but i don't know how to handle it.
    Computers Are Like Air Conditioners... They\'re both useless with Windows open!

  4. #4
    Linux Newbie
    Join Date
    Dec 2003
    Location
    Netherlands
    Posts
    193
    does no one has a answer for this problem?
    Computers Are Like Air Conditioners... They\'re both useless with Windows open!

  5. #5
    Linux Enthusiast Opnosforatou's Avatar
    Join Date
    Dec 2003
    Location
    Vleuten, The Netherlands
    Posts
    552
    put the following into you rc.local
    Code:
    # Start the connection watcher
    if [ -x /usr/local/bin/connection_watchdog.d ]; then
      . /usr/local/bin/connection_watchdog.d &
    fi
    Now bootup VI with vi /usr/local/bin/connection_watchdog.d and past the following into it.
    Code:
    #!/bin/bash
    #variables
    reconnect=0
    maxreconnect=4
    pingfailed=0
    maxpingfailed=6
    checkhost1="192.168.0.1" # An IP of one of your other machines will do.
    buildconnection="ifup eth0"
    stopconnection="ifdown eth0"
    lockfile="/var/lock/NetCheck.lock"
    logfile="/var/log/NetCheck.log" 
    log="logger -s -i -t$0"
     
     
    # this script may run only once ! (at a time)
    if test -f $lockfile; then
          if ps -p `cat $lockfile` 1>/dev/null 2>&1; then
             $log "Already running, this instance will exit now..."; exit 1
          fi
       else $log "stale lockfile exists, I will remove it and continue..."
       rm -f $lockfile
    fi
     
    #create a new lockfile
    echo $$ >$lockfile
     
    #check connection forever......
    while (true) ; do
            if ! ping -c 1 $checkhost1 2>/dev/null 1>&2
               then $log "ping to $checkhost1 failed, I'll try to ping $checkhost2..."
               if ! ping -c 1 $checkhost2 2>/dev/null 1>&2
                   then $log "ping to $checkhost2 failed too... Assuming connection is lost.... (Re)connecting..."
                   pingfailed=`expr $pingfailed + 1`
                   $log "ping failed $pingfailed times in a row...."
                   if test -$pingfailed -eq -$maxpingfailed; then
                      $log "ping failed maximum number of times ($pingfailed), waiting for 5 minutes before attempting reconnect..."
                      sleep 300
                      pingfailed=0
                   fi
                   # log this message in a special logfile to count disconnects and do this only once with every disconnect
                   if test $connectfailed !="true"
                      then date >>$logfile; echo "disconnect">>$logfile
                   fi
                   # now try to reconnect....
                   if ! $buildconnection
                      then $log "reconnect failed...retrying in about 20 seconds..." 2>>$logfile 1>&2
                      reconnect=`expr $reconnect + 1`
                      if test -$reconnect -eq -$maxreconnect ; then                    
                            $log "reconnect failed $reconnect times, waiting for 5 minutes before retrying.."
                            sleep 300
                            $reconnect=0
                      fi
                   fi
               fi
               # ping success
               else
                            pingfailed=0
            #uncomment the followingline to flood your logfile.
            #               $log "Ping success. Waiting for 5min for next ping."
            fi
    sleep 300
    done
    now make it executable with: chmod +x /usr/local/bin/connection_watchdog.d

    Next time you'll bootup you'll see the program active with the command ps -ef

    Enjoy.

    Every time the eth0 is initialized it will create a DHCP call. That's why your other system is flooded with all those entries...
    That's why i check if it's down by using ping instead of forcing the eth0 to go up every 5 min.
    ---[ MS09-99896 - Vulnerability in All MS Windows OS ; Using Windows Could Allow Remote Code Execution. ]---
    Hardware: Asus P4P800, 1GB, P4-3Ghz, Asus V9950, Maxtor ATA HD\'s, 3Com GBit lan, Audigy ZS Plat.

  6. #6
    Linux Newbie
    Join Date
    Dec 2003
    Location
    Netherlands
    Posts
    193
    Thanks Opnosforatou

    It works perfect. Because I'm still in learning phase, could you tell me where I can find information about scripting, etc?
    Computers Are Like Air Conditioners... They\'re both useless with Windows open!

  7. #7
    Linux Enthusiast Opnosforatou's Avatar
    Join Date
    Dec 2003
    Location
    Vleuten, The Netherlands
    Posts
    552
    Quote Originally Posted by Mystic_Slayer
    Thanks Opnosforatou

    It works perfect. Because I'm still in learning phase, could you tell me where I can find information about scripting, etc?
    Try you local bookstore for books by publisher : O'Reilly.
    And ofcourse google: Keywords scrip linux, or bash script.
    If you are using a bash shell, usualy default in linux install.
    There is tons of info available on the net...
    I'm thinking about writing a starters guido to bash scripting.
    Hope I can find the time...
    Until then, goolge and Linux forum are your friends
    ---[ MS09-99896 - Vulnerability in All MS Windows OS ; Using Windows Could Allow Remote Code Execution. ]---
    Hardware: Asus P4P800, 1GB, P4-3Ghz, Asus V9950, Maxtor ATA HD\'s, 3Com GBit lan, Audigy ZS Plat.

  8. #8
    Linux Newbie
    Join Date
    Dec 2003
    Location
    Netherlands
    Posts
    193
    Allright thanks. I will run to the bookstore, have some money left to spend.
    Computers Are Like Air Conditioners... They\'re both useless with Windows open!

  9. #9
    Linux Newbie
    Join Date
    Dec 2003
    Location
    Netherlands
    Posts
    193
    Hmm after three days i was looking how good it works, but I found a problem again.

    The dhclient still startsup everytime. I've had to delete 2300 dhclients. That's alot of work you know.

    Did i something wrong?

    I've looked in my NetCheck.log and see every 5 minutes:

    Wed May 5 09:55:15 CEST 2004
    disconnect
    Wed May 5 10:00:18 CEST 2004
    disconnect
    Wed May 5 10:05:20 CEST 2004
    disconnect
    Wed May 5 10:15:24 CEST 2004
    disconnect
    Wed May 5 10:20:26 CEST 2004
    disconnect
    Wed May 5 10:25:31 CEST 2004
    disconnect
    Wed May 5 10:30:33 CEST 2004
    disconnect
    Wed May 5 10:35:36 CEST 2004
    disconnect
    Wed May 5 10:40:39 CEST 2004
    disconnect
    Wed May 5 10:50:43 CEST 2004
    disconnect
    Wed May 5 10:55:45 CEST 2004
    disconnect
    Wed May 5 11:00:46 CEST 2004
    disconnect
    Wed May 5 11:05:48 CEST 2004
    disconnect
    Wed May 5 11:10:51 CEST 2004
    disconnect
    Wed May 5 11:15:53 CEST 2004
    disconnect
    Wed May 5 11:25:57 CEST 2004
    disconnect
    Computers Are Like Air Conditioners... They\'re both useless with Windows open!

  10. #10
    Linux Enthusiast Opnosforatou's Avatar
    Join Date
    Dec 2003
    Location
    Vleuten, The Netherlands
    Posts
    552
    # log this message in a special logfile to count disconnects and do this only once with every disconnect
    if test $connectfailed !="true"
    then date >>$logfile; echo "disconnect">>$logfile
    fi
    Remove these lines... These are flooding your log file.
    It's a counter how often the checkfile runs...

    Sorry :o
    ---[ MS09-99896 - Vulnerability in All MS Windows OS ; Using Windows Could Allow Remote Code Execution. ]---
    Hardware: Asus P4P800, 1GB, P4-3Ghz, Asus V9950, Maxtor ATA HD\'s, 3Com GBit lan, Audigy ZS Plat.

Page 1 of 2 1 2 LastLast

Posting Permissions

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