Last active
April 21, 2019 08:27
-
-
Save libraplanet/099673fb88ed39e12520c83da23a68d8 to your computer and use it in GitHub Desktop.
!Check available network node from mac address using arp and ping.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 'OK' | |
print stdout_data | |
else: | |
print 'Status: 404 Not Found' | |
print 'Content-Type: text/plain' | |
if __name__ == '__main__': | |
app(os.environ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
() => { | |
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