|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +""" |
| 4 | + Copyright 2022 Barrett Technology <[email protected]> |
| 5 | +
|
| 6 | + This file is part of libbarrett. |
| 7 | +
|
| 8 | + This version of libbarrett is free software: you can redistribute it |
| 9 | + and/or modify it under the terms of the GNU General Public License as |
| 10 | + published by the Free Software Foundation, either version 3 of the |
| 11 | + License, or (at your option) any later version. |
| 12 | +
|
| 13 | + This version of libbarrett is distributed in the hope that it will be |
| 14 | + useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + GNU General Public License for more details. |
| 17 | +
|
| 18 | + You should have received a copy of the GNU General Public License along |
| 19 | + with this version of libbarrett. If not, see |
| 20 | + <http://www.gnu.org/licenses/>. |
| 21 | +
|
| 22 | + Further, non-binding information about licensing is available at: |
| 23 | + <http://wiki.barrett.com/libbarrett/wiki/LicenseNotes> |
| 24 | +""" |
| 25 | + |
| 26 | +""" |
| 27 | +This utility can be installed on a computer to make it discoverable by the wamdiscover |
| 28 | +program. |
| 29 | +
|
| 30 | +The script listens for a UDP broadcast to request that any target systems identify |
| 31 | +themselves sent by wamdiscover and responds with the systems name and IP address. |
| 32 | +""" |
| 33 | + |
| 34 | +from sys import argv |
| 35 | +from os.path import basename |
| 36 | +from socket import socket, AF_INET, SOCK_DGRAM |
| 37 | +from syslog import openlog, syslog, LOG_ERR |
| 38 | + |
| 39 | + |
| 40 | +PORT = 1337 |
| 41 | +NAME_FILE = "/etc/barrett/serial" |
| 42 | +DEFAULT_NAME = "UNNAMED_WAM" |
| 43 | + |
| 44 | + |
| 45 | +openlog(basename(argv[0])) |
| 46 | + |
| 47 | +while True: |
| 48 | + # Make a socket, listen on PORT |
| 49 | + sock = socket(AF_INET, SOCK_DGRAM) |
| 50 | + sock.bind(("", PORT)) |
| 51 | + |
| 52 | + # Wait for a message (no data needed) |
| 53 | + remoteAddr = sock.recvfrom(0)[1] |
| 54 | + |
| 55 | + # Find the name of this computer |
| 56 | + try: |
| 57 | + name = open(NAME_FILE).readline().strip() |
| 58 | + except: |
| 59 | + name = DEFAULT_NAME |
| 60 | + |
| 61 | + # Find the local IP address associated with the interface that is used to |
| 62 | + # talk to remoteAddr |
| 63 | + sock.connect(remoteAddr) |
| 64 | + localIp = sock.getsockname()[0] |
| 65 | + |
| 66 | + # Reply with name and local IP address |
| 67 | + replyStr = name + "|" + localIp |
| 68 | + sock.send(replyStr.encode()) |
| 69 | + syslog(LOG_ERR, "Replied to %s with \"%s\"" % (remoteAddr[0], replyStr)) |
| 70 | + |
| 71 | + sock.close() |
0 commit comments