-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbhostat.py
477 lines (453 loc) · 21.4 KB
/
bhostat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# Copyright © 2021-2024 Björn Victor ([email protected])
# Tool for exploring Chaosnet using various (simple) protocols.
# Demonstrates the high-level python library for Chaosnet.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: rename this to "simple.py", semi-ironically. Or just "hostat.py", less ironic.
import sys, time, socket
import string, re
import functools
from struct import unpack
from datetime import datetime, timedelta
from chaosnet import BroadcastSimple, Simple, BroadcastConn, StreamConn, ChaosError
from chaosnet import dns_name_of_address, dns_resolver_name, dns_resolver_address, set_dns_resolver_address
# pip3 install dnspython
import dns.resolver
debug = False
verbose = False
# Prefer not to ask for a host's name
no_host_names = False
@functools.cache
def host_name(addr, timeout=2):
if isinstance(addr,int):
if addr < 0o400:
return addr
addr = "{:o}".format(addr)
# if no_host_names:
# return addr
# if addr in host_names:
# return host_names[addr]
try:
s = Simple(addr, "STATUS", options=dict(timeout=timeout))
src, data = s.result()
except ChaosError as msg:
if debug:
print("Error while getting STATUS of {}: {}".format(addr,msg), file=sys.stderr)
# host_names[addr] = "????"
# return addr
src = None
if src:
# BGDFAX pads with spaces (instead of nulls)
name = str(data[:32].rstrip(b'\x00 '), "ascii")
# host_names[addr] = name
return name
else:
if debug:
print("No STATUS from {:s}".format(addr), file=sys.stderr)
name = dns_name_of_address(addr, timeout=timeout, onlyfirst=True)
if debug:
print("Got DNS for {:s}: {}".format(addr,name), file=sys.stderr)
if name is None:
# host_names[addr] = addr
return addr
else:
# host_names[addr] = name
return name
# name = "{}".format(addr)
# return host_names[addr]
#### Application protocols (@@@@ perhaps move to chaosnet.py?)
# Oxymoron ("simple" means "datagram" in Chaosnet lingo), but here it means:
# given a host and contact, copy its output until EOF
class SimpleStreamProtocol:
timeout = None
def __init__(self,host,contact,options=None,args=[]):
if options is not None and 'timeout' in options.keys():
self.timeout = options['timeout']
self.conn = StreamConn()
self.conn.connect(host,contact,options=options,args=args)
def copy_until_eof(self):
try:
if self.timeout is not None:
self.conn.sock.settimeout(self.timeout)
return self.conn.copy_until_eof()
except socket.timeout as m:
raise ChaosError(m)
# Implement methods for header, printer (of each ANS received) and nonprinter (to print e.g. free lispms)
# If the printer methods returns False, the data is assumed non-printed and passed to the nonprinter at the end.
class SimpleProtocol:
contact = None
options = None
def __init__(self,subnets,options=None):
self.options = options
# Allow mix of subnets (for broadcast) and host/addresses (for unicast)
snargs = list(filter(lambda x: isinstance(x,int) and x < 0o400,subnets))
hargs = list(filter(lambda x: not(isinstance(x,int) and x < 0o400),subnets))
hdr = self.header() if callable(getattr(self.__class__,'header',None)) else None
ftr = self.footer() if callable(getattr(self.__class__,'footer',None)) else None
prtr = self.printer if callable(getattr(self.__class__,'printer',None)) else None
nprtr = self.nonprinter if callable(getattr(self.__class__,'nonprinter',None)) else None
s = None
# First do unicast, if any
if len(hargs) > 0:
s = Simple(hargs,self.contact,options=options,header=hdr,footer=ftr,printer=prtr,nonprinter=nprtr)
# then do broadcast, if any
if len(snargs) > 0:
# Avoid printing the header twice and repeating unicast hosts in the broadcast results
BroadcastSimple(snargs,self.contact,options=options,
header=hdr if s is None or s.hdr_printed is False else None,
footer=ftr,
printer=prtr,nonprinter=nprtr,
already_printed=None if s is None else s.printed_sources)
# The STATUS protocol
class Status(SimpleProtocol):
contact = "STATUS"
def header(self):
return ("{:<25s}{:>6s} "+"{:>8} "*8).format("Name","Net", "In", "Out", "Abort", "Lost", "crcerr", "ram", "Badlen", "Rejected")
def parse_status_data(self,src,data):
# First is the name of the node
# BGDFAX pads with spaces (instead of nulls)
hname = str(data[:32].rstrip(b'\x00 '),'ascii')
fstart = 32
dlen = len(data)
statuses = dict()
# Parse the data
try:
while fstart+4 < dlen:
# Two 16-bit words of subnet and field length
subnet,flen = unpack('H'*2,data[fstart:fstart+4])
# But subnet is +0400
assert (subnet > 0o400) and (subnet < 0o1000)
subnet -= 0o400
# Then a number of doublewords of info
if fstart+flen >= dlen:
break
fields = unpack('{}I'.format(int(flen/2)), data[fstart+4:fstart+4+(flen*2)])
statuses[subnet] = dict(zip(('inputs','outputs','aborted','lost','crc_errors','hardware','bad_length','rejected'),
fields))
fstart += 4+flen*2
except AssertionError:
print('{} value error at {}: {!r}'.format(hname,fstart,data[fstart:]))
return hname,statuses
def printer(self,src,data):
hname,statuses = self.parse_status_data(src,data)
# Now print it
if " " in hname and not no_host_names:
# This must be a "pretty name", so find the DNS name if possible
dname = dns_name_of_address(src,onlyfirst=True,timeout=2)
if dname is None:
first = "{} ({:o})".format(hname, src)
else:
first = "{} [{}] ({:o})".format(hname, dname, src)
else:
first = "{} ({:o})".format(hname, src)
if statuses is not None:
for s in statuses:
if len(first) >= 26:
# Make the numeric columns aligned at the cost of another line of output
print("{:s}".format(first))
first = "" #
print(("{:<25s}{:>6o}"+" {:>8}"*len(statuses[s])).format(first,s,*statuses[s].values()))
# Only print the name for the first subnet entry
first = ""
else:
print("{} not responding".format(first))
# The TIME protocol
class ChaosTime(SimpleProtocol):
contact = "TIME"
def printer(self,src,data):
# cf RFC 868
t = unpack("I",data[0:4])[0]-2208988800
dt = t-time.time()
hname = "{} ({:o})".format(host_name("{:o}".format(src)), src)
if verbose:
print("{:16} {} (delta {}{})".format(hname,datetime.fromtimestamp(t),"+" if dt >= 0 else "-",timedelta(milliseconds=abs(1000*dt))))
else:
print("{:16} {}".format(hname,datetime.fromtimestamp(t)))
# The UPTIME protocol
class ChaosUptime(SimpleProtocol):
contact = "UPTIME"
def printer(self, src, data):
hname = "{} ({:o})".format(host_name("{:o}".format(src)), src)
# cf RFC 868
print("{:16} {}".format(hname,timedelta(seconds=int(unpack("I",data[0:4])[0]/60))))
# The FINGER protocol (note: not NAME)
class ChaosFinger(SimpleProtocol):
contact = "FINGER"
def header(self):
return "{:15s} {:1s} {:22s} {:10s} {:5s} {:s}".format("User","","Name","Host","Idle","Location")
def printer(self,src,data):
hname = host_name("{:o}".format(src))
fields = list(map(lambda x: str(x,'ascii'),data.split(b"\215")))
if fields[0] == "":
return False
# uname affiliation pname hname idle loc
print("{:15s} {:1s} {:22s} {:10s} {:5s} {:s}".format(fields[0],fields[4],fields[3],hname,fields[2],fields[1]))
return True
def nonprinter(self,freelist):
if len(freelist) > 0:
print("\nFree (lisp) machines:")
for src,data in freelist:
hname = host_name("{:o}".format(src))
f = list(map(lambda x: str(x,'ascii'),data.split(b"\215")))
print("{:17s} {:s}{:s}".format(hname,f[1]," (idle {:s})".format(f[2]) if f[2] != "" else ""))
else:
print("\nNo free lisp machines.")
# The LOAD protocol
class ChaosLoad(SimpleProtocol):
contact = "LOAD"
def printer(self, src, data):
try:
hname = host_name("{:o}".format(src))
except:
hname = src
fields = ", ".join(list(map(lambda x: str(x,'ascii'),data.split(b"\r\n"))))
print("{}: {}".format(hname,fields))
# Hack: use LOAD to see if it's worth fingering (with NAME)
class ChaosLoadName(SimpleProtocol):
contact = "LOAD"
def printer(self, src, data):
try:
# Get full name, for this purpose
hname = dns_name_of_address(src,onlyfirst=False,timeout=2)
if hname is None:
hname = "{:o}".format(src)
except:
hname = src
# Parse the second line of LOAD
nmatch = re.match(r"Users: (\d+)", str(data.split(b"\r\n")[1],"ascii"))
if nmatch:
n = int(nmatch.group(1))
if n == 0:
# No users, just report it's empty
return False
# Print a header to show what's about to happen
print("[{:s}]".format(hname))
nout = 0
try:
s = SimpleStreamProtocol(hname,"NAME",options=self.options)
nout = s.copy_until_eof()
except ChaosError as msg:
if debug:
print(msg, file=sys.stderr)
return False
# In case there was a response to LOAD, but nothing from NAME, give *some* info.
if nout == 0:
print("Users: {}".format(n))
return True
elif debug:
print("Can't parse LOAD output from {}: {!r}".format(src,data), file=sys.stderr)
def nonprinter(self,datas):
if len(datas) > 0:
hnames = []
# Collect the host names of empty hosts - use short name here
for src,d in datas:
hnames.append(host_name("{:o}".format(src)))
# Report it.
print("\nNo users on "+", ".join(hnames)+".")
# The DUMP-ROUTING-TABLE protocol
class ChaosDumpRoutingTable(SimpleProtocol):
contact = "DUMP-ROUTING-TABLE"
def header(self):
return "{:<20} {:>6} {:>6} {}".format("Host","Net","Meth","Cost")
def printer(self, src, data):
hname = "{} ({:o})".format(host_name("{:o}".format(src)), src)
rtt = dict()
# Parse routing table info
for sub in range(0,int(len(data)/4)):
sn = unpack('H',data[sub*4:sub*4+2])[0]
if sn != 0:
rtt[sub] = dict(zip(('method','cost'),unpack('H'*2,data[sub*4:sub*4+4])))
first = hname
for sub in rtt:
print("{:<20} {:>6o} {:>6o} {}".format(first,sub,rtt[sub]['method'],rtt[sub]['cost']))
first = ""
# The LASTCN protocol
class ChaosLastSeen(SimpleProtocol):
contact = "LASTCN"
def header(self):
if not no_host_names:
return("{:<20} {:20} {:>8} {:10} {:>2} {}".format("Host","Seen","#in","Via","FC","Age"))
else:
return("{:<20} {:>8} {:>8} {:>8} {:>2} {}".format("Host","Seen","#in","Via","FC","Age"))
def printer(self, src, data):
hname = "{} ({:o})".format(host_name("{:o}".format(src)), src) if not(no_host_names) else "{:o}".format(src)
cn = dict()
i = 0
while i < int(len(data)/2):
flen = unpack('H',data[i*2:i*2+2])[0]
assert flen >= 7
addr = unpack('H',data[i*2+2:i*2+4])[0]
inp = unpack('I',data[i*2+4:i*2+4+4])[0]
via = unpack('H',data[i*2+4+4:i*2+4+4+2])[0]
age = unpack('I',data[i*2+4+4+2:i*2+4+4+2+4])[0]
if (flen > 7):
fc = unpack('H',data[i*2+4+4+2+4:i*2+4+4+2+4+2])[0]
cn[addr] = dict(input=inp,via=via,age=age,fc=fc)
else:
cn[addr] = dict(input=inp,via=via,age=age,fc='')
i += flen
first = hname
for addr in cn:
e = cn[addr]
a = timedelta(seconds=e['age'])
if not no_host_names:
print("{:<20} {:<20} {:>8} {:<10} {:>2} {}".format(first,"{} ({:o})".format(host_name(addr),addr),e['input'],host_name(e['via']),e['fc'],a))
else:
print("{:<20} {:>8o} {:>8} {:>8o} {:>2} {}".format(first,addr,e['input'],e['via'],e['fc'],a))
first = ""
# @@@@ rewrite like above
class ChaosDNS:
def __init__(self,subnets, options=None, name=None, qtype=None):
self.subnets = subnets
self.options = options
self.name = name
self.qtype = dns.rdatatype.from_text(qtype)
# self.get_dns(subnets, options=options, name=name, qtype=qtype)
def get_values(self):
hlist = []
values = []
msg = dns.message.make_query(self.name, self.qtype, rdclass=dns.rdataclass.CH)
w = msg.to_wire()
if debug:
print("> {!r}".format(msg.to_text()))
print("> {} {!r}".format(len(w), w))
# print("> {!r}".format(msg.to_wire().from_wire().to_text()))
for src,resp in BroadcastConn(self.subnets,"DNS", args=[w], options=self.options):
if src not in hlist:
r = dns.message.from_wire(resp)
if r.rcode() == dns.rcode.NXDOMAIN:
print("Non-existing domain: {}".format(self.name), file=sys.stderr)
if not debug:
return None
if debug:
print("< {:o} {!r}".format(src,r.to_text()))
for t in r.answer:
print("Answer from {:o}: {}".format(src,t.to_text()))
if self.qtype == t.rdtype:
v = []
if self.qtype == dns.rdatatype.PTR:
for d in t:
v.append(d.target.to_text())
elif self.qtype == dns.rdatatype.A:
for d in t:
v.append(d.address)
elif self.qtype == dns.rdatatype.TXT:
for d in t:
v.append(d.strings)
elif self.qtype == dns.rdatatype.HINFO:
for d in t:
v.append(d.to_text())
if len(v) > 0:
if self.qtype == dns.rdatatype.A:
# hack hack
print(("{}: "+", ".join(["{:o}"]*len(v))).format(dns.rdatatype.to_text(self.qtype), *v))
else:
print("{}: {}".format(dns.rdatatype.to_text(self.qtype), v))
values += v
if not debug:
# first response is sufficient
return values
hlist.append(src)
return values
contact_handlers = { 'status': Status,
'time': ChaosTime,
'uptime': ChaosUptime,
'finger': ChaosFinger,
'load': ChaosLoad,
'loadname': ChaosLoadName,
'lastcn': ChaosLastSeen,
'routing': ChaosDumpRoutingTable,
'dump-routing-table': ChaosDumpRoutingTable }
special_contact_handlers = dict(dns=ChaosDNS)
if __name__ == '__main__':
import argparse
service_names = ", ".join(contact_handlers.keys()).upper()+", "+", ".join(special_contact_handlers.keys()).upper()
parser = argparse.ArgumentParser(description='Chaosnet simple protocol client',
epilog="If the service is unknown and a host is given, "+\
"tries to contact the service at the host and prints its output. "+\
"If no service arg is given, but the first subnet arg is the name of a known service, "+\
"and another subnet arg exists, the first subnet arg is used as service arg.")
parser.add_argument("subnets", metavar="SUBNET/HOST", nargs='+', #type=int,
help="Hosts to contact or Subnets (octal) to broadcast on, -1 for all subnets, or 0 for the local subnet")
parser.add_argument("-t","--timeout", type=int, default=5,
help="Timeout in seconds")
parser.add_argument("-r","--retrans", type=int, default=500,
help="Retransmission interval in milliseconds")
parser.add_argument("-s","--service", # default="STATUS",
help="Service to ask for ("+service_names+"), default: STATUS")
parser.add_argument("-d",'--debug',dest='debug',action='store_true',
help='Turn on debug printouts')
parser.add_argument("-v",'--verbose',dest='verbose',action='store_true',
help='Turn on verbose printouts')
parser.add_argument("-n",'--no-host-names', dest='no_host_names', action='store_true',
help="Prefer not to ask hosts for their names")
parser.add_argument("-R","--resolver", default=dns_resolver_name,
help="DNS resolver to use (over IP) for Chaosnet data, default "+dns_resolver_name)
parser.add_argument("--name", help="Name to ask for Chaosnet data of (DNS)", default="Router.Chaosnet.NET")
parser.add_argument("--rtype", help="Resource to ask for (DNS)", default="A")
args = parser.parse_args()
if args.debug:
print(args)
debug = True
if args.verbose:
verbose = True
if args.no_host_names:
no_host_names = True
if args.resolver:
dns_resolver_address = set_dns_resolver_address(args.resolver)
if -1 in args.subnets and len(args.subnets) != 1:
# "all" supersedes all other
args.subnets = [-1]
elif 0 in args.subnets and len(args.subnets) != 1:
# "local" supersedes all other
args.subnets = [0]
# Parse "subnet" args as octal numbers (if they can be)
args.subnets = list(map(lambda x: int(x,8) if isinstance(x,str) and (x.isdigit() or x =="-1") else x, args.subnets))
# if no service explicitly given, but first "subnet" is a service (and more given), use that
# Example: "bhostat.py finger 0"
if args.service is None and len(args.subnets) > 1 and (args.subnets[0] in contact_handlers or args.subnets[0].lower() == "hostat"):
args.service = args.subnets[0] if args.subnets[0].lower() != "hostat" else "status"
args.subnets = args.subnets[1:]
# maybe if only a service is given, use "local" as subnet? But just a host name for STATUS is useful.
elif args.service is None:
args.service = "STATUS"
try:
if args.service.lower() in contact_handlers:
c = contact_handlers[args.service.lower()]
opts = dict(timeout=args.timeout, retrans=args.retrans)
c(args.subnets,opts)
elif args.service.lower() in special_contact_handlers:
if args.service.upper() == "DNS":
# Broadcast a DNS quuery and show the result
if args.name.isdigit():
# Ask for a pointer instead
args.name += ".CH-ADDR.NET"
args.rtype = "ptr"
c = ChaosDNS(args.subnets,options=dict(timeout=args.timeout, retrans=args.retrans),
name=args.name, qtype=args.rtype)
print("Values: {}".format(c.get_values()))
exit(0)
elif len(args.subnets) == 1 and (isinstance(args.subnets[0],str) or args.subnets[0] > 0xff):
# Hack: try connecting to the service at the host
# Example: "bhostat.py -s bye up"
# @@@@ Could parse the service, split at spaces and put things in contact args.
# (To make it work with broadcast destination (and open the first OPN-sender) requires cbridge work.)
s = SimpleStreamProtocol(args.subnets[0],args.service)
s.copy_until_eof()
exit(0)
else:
print("Bad service arg {}, please use {} (in any case)".format(args.service, service_names))
exit(1)
except ChaosError as msg:
print(msg, file=sys.stderr)
except KeyboardInterrupt:
pass