Created script which pings a specific host-type and writes to the ip4address object when it was last seen. Also made a change to the models in order to facilitate the logging.
This commit is contained in:
1 parent
83bbfbe1a8
commit
99a931c199
2 files changed
+50
No files matched your search
@@ -372,6 +372,7 @@ class DhcpCustomField(models.Model):
|
||||
class Ip4Address(models.Model):
|
||||
subnet = models.ForeignKey(Ip4Subnet)
|
||||
address = models.IPAddressField()
|
||||
last_contact = models.DateTimeField()
|
||||
|
||||
def __unicode__(self):
|
||||
if self.interface_set.count() == 0:
|
||||
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
import os,sys,datetime
|
||||
|
||||
try:
|
||||
host_type = sys.argv[1]
|
||||
except:
|
||||
print "USAGE: %s <host type>" % sys.argv[0]
|
||||
sys.exit(1)
|
||||
|
||||
from django.core.management import setup_environ
|
||||
from django.core.mail import mail_admins
|
||||
from dns_mdb import settings
|
||||
|
||||
setup_environ(settings)
|
||||
|
||||
from mdb.models import *
|
||||
|
||||
debugging = False
|
||||
|
||||
|
||||
|
||||
def do_ping(ipaddr):
|
||||
if os.system("ping -c 1 -W 1 %s > /dev/null 2>&1" % ipaddr) == 0:
|
||||
return 1
|
||||
else:
|
||||
return None
|
||||
|
||||
hosts = Host.objects.filter(host_type__host_type = host_type)
|
||||
|
||||
|
||||
for host in hosts:
|
||||
interfaces = host.interface_set.all()
|
||||
|
||||
for interface in interfaces:
|
||||
if do_ping(interface.ip4address.address):
|
||||
interface.ip4address.last_contact = datetime.datetime.now()
|
||||
interface.ip4address.save()
|
||||
if debugging:
|
||||
print "%s (%s) : success" % (host, interface.ip4address.address)
|
||||
else:
|
||||
if debugging:
|
||||
print "%s (%s) : fail " % (host, interface.ip4address.address)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user