Please refer the following script for, ping the Ip-addresses in the network and it shows whether the corresponding Ip-addresses is offline or online.
import os
import re
def ecmd(CMD, r=True):
import ctypes
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
from subprocess import PIPE, Popen
with disable_file_system_redirection():
OBJ = Popen(CMD, shell = True, stdout = PIPE, stderr = PIPE)
out, err = OBJ.communicate()
ret=OBJ.returncode
if r:
return ret
else:
if ret==0:
return out
else:
return ret
a=os.popen('arp -a').read()
reg=re.findall('([0-9]{2}.[0-9]{3}.[0-9]{2}.[0-9]+)',a)
length= len(reg)
print 'The total number of IPADDRESSES present in your netwrok is: %d' %length
print '
'
print 'The following IP ADDRESSES in your network went OFFLINE :'
for i in reg:
c='ping %s'%i
if ecmd(c, True)!=0:
print '%s is offline'%i
print '
'
print 'The following IP ADDRESSES in your network went ONLINE :'
for i in reg:
if ecmd(c, True)==0:
print '%s is online'%i
SAMPLE OUTPUT:
20170524-ip_monitoring.json (2.05 KB)