Browse Source

add features

micha 3 months ago
parent
commit
e65b495320
1 changed files with 183 additions and 95 deletions
  1. 183 95
      tool/nodescan2.py

+ 183 - 95
tool/nodescan2.py

@@ -7,6 +7,7 @@ SPDX-URL: https://spdx.org/licenses/GPL-2.0-only.html
 (c) 2012 micha@librelight.de
 """
 import time
+import json
 import socket, struct
 import sys
 import os
@@ -180,47 +181,108 @@ def test():
         print(NODE)
     assert NODE == node
 
+def os_get_MAC(_filter=["vmbr0","br0"]):
+    print(sys._getframe().f_code.co_name)
+    cmd = "ip -j -o l l"
+    r=os.popen(cmd)
+    out="fa:00:00:00:00:00"
+    out={}
+    if not r:
+        return out
+
+    try:
+        txt = r.read()
+        data = json.loads(txt)
+        for i in data:
+            #print(i)
+            if "ifname" not in i:
+                continue
+
+            #if i["ifname"] in _filter:
+            if 1:
+                dev = "none"
+                if "ifname" in i:
+                    dev = i["ifname"]
+                #if "." in dev:
+                #    continue
+
+                if dev not in out:
+                    out[dev] = []
+                if "address" in i:
+                    out[dev].append( i["address"])
+    except Exception as e:
+        print(e,e.args[0])
+
+    return out
+def os_list_routing():
+    print(sys._getframe().f_code.co_name)
+    cmd="ip -j -o r l"
+    r=os.popen(cmd)
+    out={}
+    if not r:
+        return ips
+
+    txt=r.read()
+    jdata = json.loads(txt)
+    for data in jdata:
+        #print(data)
+        dev = "none"
+        if "dev" in data:
+            dev = data["dev"]
+        if dev not in out:
+            out[dev] = []
+        if "dst" in data:
+            out[dev].append(data["dst"])
+    return out
+
+
+def os_list_ip():
+    print(sys._getframe().f_code.co_name)
+    ips = {}
+    cmd="ip -o -j -4 a l "
+    r=os.popen(cmd)
+    if not r:
+        return ips
+
+    txt=r.read()
+    jdata = json.loads(txt)
+    for data in jdata:
+        #print(data)
+        if "addr_info" in data:
+            infos = data["addr_info"]
+            for info in infos:
+                dev = "None"
+                if "dev" in info:
+                    dev = info["dev"]
+                if "local" in info:
+                    ip = info["local"]
+                    if "prefixlen" in info:
+                        ip += "/"+str(info["prefixlen"])
+                    #if "." in dev:
+                    #    continue
+
+                    if dev not in ips:
+                        ips[dev] = []
+                    ips[dev].append(ip)
+                    #print("  ",[dev,ip])
+    return ips
+def get_mask(IP):
+    print(sys._getframe().f_code.co_name)
+    import ipaddress
+    #mask=ipaddress.IPv4Network(IP+'/8',False)
+    #print(mask,mask.netmask)
+    #mask=ipaddress.IPv4Network(IP+'/24',False)
+    mask=ipaddress.IPv4Network(IP,False)
+    #print(IP,mask.netmask)
+    return mask.netmask
 
 def ArtPollReply(sock=None):
-    print("ArtPollReply()")
+    print(sys._getframe().f_code.co_name)
     if not sock:
         sock=UDP_Socket()
     
     port = 6454
     content = []
-    header = []
-
-    # Name, 7byte + 0x00
-    content.append(b"Art-Net\x00")
-    # OpCode ArtPollReply -> 0x2100, Low Byte first
-    content.append(struct.pack('<H', 0x2100))
-    # Protocol Version 14, High Byte first
-    content.append( b"\xff\xff\xff\xff")
-
-    # Port
-    content.append(struct.pack('<H', 0x1936))
-    # Firmware Version
-    content.append(struct.pack('>H', 200))
-    # Net and subnet of this node
-    net = 0
-    subnet = 0
-    content.append(chr(net))
-    content.append(chr(subnet))
-    # OEM Code (E:Cue 1x DMX Out)
-    content.append(struct.pack('>H', 0x0360))
-    # UBEA Version -> Nope -> 0
-    content.append(chr(0))
-    # Status1
-    content.append(struct.pack('>H', 0b11010000))
-    # Manufacture ESTA Code
-    content.append(chr(0))
-
-    hostname=os.popen("hostname").read().strip()
-    # Short Name, len == 30
-    content.append(hostname.ljust(18,"\x00")[:30])
-    # Long Name, len == 64
-    content.append(("LibreLight "+hostname).ljust(64,"\x00")[:64] )
-
     content2=[]
     for c in content:
         if type(c) is not bytes:
@@ -233,9 +295,6 @@ def ArtPollReply(sock=None):
     if fill > 0:
         content = content + b"\x00"*fill
 
-    def inject(i,v,content):
-        content = content[:i]+ v + content[i+1:]
-        return content
     def create_ip(IP,content):
         #CONF["IP"]         = [10,14+1]
         x=[]
@@ -248,69 +307,73 @@ def ArtPollReply(sock=None):
             j+=1
             x.append([_ip,i])
         print(IP,x)
+
+
+    def patch(content,index,patch):
+        _patch = patch[:]
+        if type(_patch) != bytes:
+            _patch=bytes(_patch,"ascii")
+        content[index:index+len(_patch)] = _patch
+
+    def pad(val,count,fill="\x00"):
+        return val.ljust(count,fill)[:count] 
+
+    def ip_to_byte(IP):
+        out=[]
+        for i in IP.split("."):
+            out.append( int(i) )
+        return bytes(out)
     
-    #CONF["MAC"]        = [201,201+6]
-    content = inject(201,b"\xfa",content)
-    content = inject(201+1,b"\xfa",content)
-    content = inject(201+2,b"\xfa",content)
-    content = inject(201+3,b"\xfa",content)
-    content = inject(201+4,b"\xfa",content)
-    content = inject(201+5,b"\xfa",content)
+    print()
+    #IP="2.0.0.255"
+    #create_ip(IP,content)
+    #sock.sendto(content, (IP, port))
+
+    # =======================================
+    content = [0]*(282-42) # new empty PKG bytes([0,0,0,..])
+    patch(content,0, b"Art-Net\x00")
+    patch(content,8, b"\x00\x21") # revers 0x2100 protocol
+
+    hostname=os.popen("hostname").read().strip()
+    #hostname+="0123456789012345678901234567890123456789012345678901234567890"
+
+    sname=pad(hostname,18) # Short Name, len == 30
+    patch(content,26,sname)
+    lname=pad("LibreLight "+hostname,64) # Long Name, len == 64
+    patch(content,26+18,lname)
 
-    #CONF["oem"]        = [20,21+1]
-    content = inject(20,b"\x11",content)
-    content = inject(20+1,b"\x10",content)
+    Report="LibreLight is ready CPU:40%"
+    #Report+="12345678901234567890123456789012345123456789012345678901234567890"
+    Report=pad(Report,64)
+    patch(content,26+28+54,Report)
 
-    content = inject(100,b"\x11",content)
+    ips=os_list_ip()
 
+    mac= os_get_MAC()
+    if "vmbr0" in mac:
+        mac = mac["vmbr0"][0]
     print()
-    IP="2.0.0.255"
-    create_ip(IP,content)
-    sock.sendto(content, (IP, port))
-
-    IP="10.10.10.255"
-    content = [0]*(282-42) # new PKG
-    content[0:0]   = b"Art-Net\x00"
-    content[8:9+1]   = b"\x00\x21"         # Protocol 0x2100
-    content[10:13+1] = b"\xaa\xaa\xaa\xaa" # IP
-    content[20:21+1] = b"\x11\x10"         # oem
-    content[26] = ord("A")  # SHORT NAME
-    content[26+18] = ord("B") #0x42  # LONG NAME
-    content[108:108] = b"HalloX" #0x42  # MSG  26+18+64
+    print([mac])
+    print()
+    mac=mac.replace(":","")
+    print([mac])
+    if len(mac) == 12:
+        mac=bytes.fromhex(mac)
+        #content[201:] = mac 
+        patch(content,201,mac)
+        #print([mac])
+
     print("send" ,[content])
-    j=0
-    content2 = content[:]
-    for i in IP.split("."):
-        content2[10+j] = int(i)
-        j+=1
-    content2 = bytes(content2)
 
-    #create_ip(IP,content)
-    sock.sendto(content2, (IP, port))
+
+    for IP in ["192.168.2.255","2.0.0.255","10.10.10.255"]:
+        content2 = content[:]
+        _IP = ip_to_byte(IP)
+        patch(content2,10,_IP)
+        content2 = bytes(content2)
+        sock.sendto(content2, (IP, port))
     
     print("send" ,[content])
-    IP="192.168.2.255"
-    j=0
-    content2 = content[:]
-    for i in IP.split("."):
-        content2[10+j] = int(i)
-        j+=1
-    #create_ip(IP,content)
-    content2 = bytes(content2)
-    sock.sendto(content2, (IP, port))
-    
-    IP="2.0.0.255"
-    j=0
-    content2 = content[:]
-    for i in IP.split("."):
-        content2[10+j] = int(i)
-        j+=1
-    #create_ip(IP,content)
-    content2 = bytes(content2)
-    sock.sendto(content2, (IP, port))
-
-    print("send" ,[content2])
-    #print(dir(sock),sock.getsockname())
 
 def main():
     print("start main()")
@@ -321,6 +384,8 @@ def main():
         opcode=""
         if len(data) >= 10:
             opcode=convert_to_hex("<h",data[8:10])
+        if opcode != '0x2100': #OpPollReplay
+            continue
         print("-",addr,len(data),opcode)
         print()
 
@@ -329,14 +394,37 @@ def main():
             print("-",[k,v])
     print("end main()")
 
+def print_help():
+    print("help -h --help ")
+    print("  --main")
+    print("  --ArtPoll")
+    print("  --ArtPollReplay")
+
 if __name__ == "__main__":
     test()
-    if "--ArtPoll" in sys.argv:
+    if "-h" in sys.argv or "--help" in sys.argv:
+        print_help()
+    elif "--ArtPoll" in sys.argv:
         ArtPoll()
-    if "--ArtPollReplay" in sys.argv:
+    elif "--ArtPollReplay" in sys.argv:
         ArtPollReply()
-    else:
+    elif "-MAC" in sys.argv:
+        mac= os_get_MAC()
+        for k,v in mac.items():
+            print("-",k,v)
+        print()
+        for k,v in os_list_ip().items():
+            print("-",k,v)
+        print()
+        for k,v in os_list_routing().items():
+            print("-",k,v)
+        print()
+        print( get_mask("192.168.2.2/24"))
+        print( get_mask("192.168.2.2/25"))
+        print( get_mask("192.168.2.2/8"))
+    elif "--main" in sys.argv:
         main()
-
+    else:
+        print_help()