Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save libraplanet/099673fb88ed39e12520c83da23a68d8 to your computer and use it in GitHub Desktop.
Save libraplanet/099673fb88ed39e12520c83da23a68d8 to your computer and use it in GitHub Desktop.
!Check available network node from mac address using arp and ping.
USAGE: ping2mac.sh [MAC address]
USAGE: ping2mac.py? [MAC address]
"ping2mac.sh" is shell script.
"ping2mac.py" is CGI. Usable http method is GET only.
MAC address : Mac address to you want to confirm.
#!/bin/python
import os
import subprocess
def exec_arp(mac_addr_str):
s = mac_addr_str.replace('-', ':')
t = s.replace(':', '')
if len(t) != 2 * 6:
raise TypeError('Invalid MAC address length')
command = 'sh ping2mac.sh ' + s
proc = subprocess.Popen(
command,
shell = True,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
stdout_data, stderr_data = proc.communicate()
retcode = proc.returncode
return retcode, stdout_data, stderr_data
def app(env):
mac_addr_str = env.get('QUERY_STRING')
retcode, stdout_data, stderr_data = exec_arp(mac_addr_str)
if retcode == 0:
print 'Content-Type: text/plain'
print
print 'OK'
print stdout_data
else:
print 'Status: 404 Not Found'
print 'Content-Type: text/plain'
print
if __name__ == '__main__':
app(os.environ)
#!/bin/sh
echo "args = $*"
macaddr=${1}
while IFS= read line;
do
if [ "${line}" != "" ] ; then
echo ""
echo "----------------------------------"
echo " ip:${line}"
echo "----------------------------------"
ping -c 4 ${line} && exit 0
else
echo "IP of MAC address is not found. (${macaddr})" 1>&2
exit 1
fi
done << __END__
$(
arp | sort | grep -i "${macaddr}" | while IFS= read line;
do
echo "$line" | awk '{print $1}'
done
)
__END__
echo "no available IP address." 1>&2
exit 1
() => {
var url = 'ping2mac.py' + '?' + 'XX-XX-XX-XX-XX-XX'
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = () => {
if(Math.floor(req.status / 100) == 2) {
//online event
} else {
//offline event
}
}
req.onerror = () => {
//error event
}
req.send();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment