Skip to content

Instantly share code, notes, and snippets.

@chyanju
Last active April 20, 2024 10:21
Show Gist options
  • Save chyanju/f63803884b06ae8190813eb55786f0b0 to your computer and use it in GitHub Desktop.
Save chyanju/f63803884b06ae8190813eb55786f0b0 to your computer and use it in GitHub Desktop.
Returning basic machine specs via http
import urllib.request
import json
import argparse
import ipaddress
from tabulate import tabulate
ap = argparse.ArgumentParser()
ap.add_argument(
"-t", "--timeout", default=0.1, type=float, help="timeout, default: 0.1"
)
ap.add_argument(
"-n",
"--network",
default="192.168.0.0/24",
type=str,
help="network to scan (in CIDR notation), default: 192.168.0.0/24",
)
ap.add_argument(
"-p",
"--port",
default=7777,
type=int,
help="port of the info service, default: 7777",
)
args = ap.parse_args()
all_ips = [str(ip) for ip in ipaddress.IPv4Network(args.network)]
all_machines = []
for ip in all_ips:
print(f"\r\bfetching {ip}", end="")
try:
contents = urllib.request.urlopen(
f"http://{ip}:{args.port}/", timeout=args.timeout
).read()
jc = json.loads(contents.decode("utf-8"))
# filter out 127.0.0.1
jc["ips"] = [p for p in jc["ips"] if p != "127.0.0.1"]
all_machines.append(jc)
except:
continue
print(f"\r\b", end="")
headers = ["hostname", "system", "distribution", "release", "user", "ips"]
pcs = [
(p["hostname"], p["system"], p["distribution"], p["release"], p["user"], p["ips"])
for p in all_machines
]
print(tabulate(pcs, headers=headers, showindex=True, tablefmt="github"))
import web
import json
import socket
import argparse
import platform
import os
import web
import getpass
import distro
# from netifaces import interfaces, ifaddresses, AF_INET
import netifaces
ap = argparse.ArgumentParser()
ap.add_argument(
"-p",
"--port",
default=7777,
type=int,
help="port of the info service, default: 7777",
)
args = ap.parse_args()
urls = (
'/(.*)', 'info'
)
app = web.application(urls, globals())
class info:
def GET(self, name):
# ip_list = []
# for interface in interfaces():
# for link in ifaddresses(interface)[AF_INET]:
# ip_list.append(link['addr'])
ip_list = [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.ifaddresses(iface)]
info = {
"hostname": socket.gethostname(),
# "ip": socket.gethostbyname(socket.gethostname()),
"ips": ip_list,
"system": platform.system(),
"distribution": distro.id(),
"release": platform.release(),
"user": getpass.getuser(),
}
web.header('Content-Type', 'application/json')
return json.dumps(info)
if __name__ == "__main__":
web.httpserver.runsimple(app.wsgifunc(), ("0.0.0.0", args.port))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment