Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to use a Foxboard a simple network monitor for multiple routers (all routers are connected to the internet). Foxboard is a mini pc with an embedded version of Debian.

My idea is to use multiple virtual network devices like this:

  • eth0 192.168.2.10
  • eth0:1 192.168.3.10
  • eth0:2 192.168.4.10

I found a nice Python script to ping an external host here (the solution from Ryan Cox): http://stackoverflow.com/questions/316866/ping-a-site-in-python

Is it possible to configure Debian to use eth0 when I ping www.site-a.com and eth0:1 when I ping www.site-b.com?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

That depends on what IP addresses the various sites are on, but in general this is doable through use of static routes. You configure the static routes on Debian through the /etc/network/routes file.

One caveat: if the sites are all Virtual Hosts on the same IP address you won't be able to do it.

# Destination  Gateway           Netmask              Interface
192.0.2.25     192.168.2.1       255.255.255.255      eth0
198.51.100.92  192.168.3.1       255.255.255.255      eth0.1
share|improve this answer
    
Exactly what I needed, thanks! –  het.oosten Sep 2 '12 at 12:56

Here is the complete script, perhaps it can be useful for anybody who is looking for a cheap and low energy network monitor (thanks to Ryan Cox, see topic start). I think it will also run on a Raspberry board.

I made a cronjob to run this script every 5 minutes.

Remember to set a route for the smtp servers as well, otherwise the mail message won't reach you :-)


from threading import Thread
import subprocess
from Queue import Queue

num_threads = 4
queue = Queue()
ips = ["8.8.8.8","8.8.4.4","173.194.67.94","217.10.79.9"]
modem = ["modem1 - entrance - network 10.0.0.1","modem2 - basement - network 10.2.0.1","modem3 - office - network 10.3.0.1", "modem4 - office2 - network 10.4.0.1"]
#wraps system ping command
def mail_error(ip):
    password = False
    if ip == "173.194.67.94" or "8.8.8.8":
        smtp_host = "smtphost1"
        poort = 25
    else:
        smtp_host = "smtphost2"
        poort = 587
        password = True
    import smtplib

    from email.mime.text import MIMEText
    text = "Network error ip address %s" % modem[ips.index(ip)]
    msg = MIMEText(text)
    msg['Subject'] = 'Network wrror %s' % modem[ips.index(ip)]
    me = "mymailaddress"
    you = "mymailaddress"
    msg['From'] = me
    msg['To'] = you
    s = smtplib.SMTP(smtp_host,poort)
    if password:
        s.login("username", "pass")
    s.sendmail(me, [you], msg.as_string())
    s.quit()


def pinger(i, q):
    """Pings subnet"""
    while True:
        ip = q.get()
        print "Thread %s: Pinging %s" % (i, ip)
        ret = subprocess.call("ping -c 1 %s" % ip,
                        shell=True,
                        stdout=open('/dev/null', 'w'),
                        stderr=subprocess.STDOUT)
        if ret == 0:
            print "%s: is alive" % ip
        else:
            print "%s: did not respond" % ip
            mail_error(ip)
        q.task_done()
#Spawn thread pool
for i in range(num_threads):

    worker = Thread(target=pinger, args=(i, queue))
    worker.setDaemon(True)
    worker.start()
#Place work in queue
for ip in ips:
    queue.put(ip)
#Wait until worker threads are done to exit    
queue.join()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.