#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Netstat detect number of connection on port


    I have no clue how to code but I need to modify the below script if found on xbmc forums. The script scans for connections on certain ports and aborts the shutdown process if present.

    I want modify it so it only aborts if there are three or more instances of port 9982 and that all. Can someone help? thanks.

    Code:
    #!/usr/bin/python
    from __future__ import print_function
    
    import xbmc
    import subprocess
    
    # Set of (protocol, local port) tuples.
    watched = {
        ('tcp', 22), # SSH
        ('tcp', 445), # samba
        }
    sleep_time = 60 * 1000 # sleep time between checks in miliseconds
    
    def log(msg):
        print("service.inhibit_shutdown: {}".format(msg))
    
    def check_services():
        """ Check if any of the watched services is running. """
    
        netstat = subprocess.check_output(['/bin/netstat', '--protocol=inet', '-n'], universal_newlines=True)
    
        for line in netstat.split('\n')[2:]:
            items = line.split()
            if len(items) < 4:
                continue
    
            proto = items[0]
            port = int(items[3].split(':')[-1])
    
            if (proto, port) in watched:
                log("Found {} connection from {} to port {}".format(proto, items[4], port))
                return True
    
        log("No connection found.")
        return False
    
    while not xbmc.abortRequested:
        if check_services():
            xbmc.executebuiltin('InhibitIdleShutdown(true)')
        else:
            xbmc.executebuiltin('InhibitIdleShutdown(false)')
        xbmc.sleep(sleep_time)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    39
    Rep Power
    3
    Change the watched protocols to the one you want (I've assumed it is a tcp not a udp protocol) as follows:
    Code:
    watched = { ('tcp', 9982) }
    Then change the check services function:
    Code:
    def check_services():
        # description and netstat line as before
        count = 0
    
        for line in netstat.split('\n')[2:]:
            # next 5 lines as before
    
            if (proto, port) in watched:
                count += 1
                log("Found {} connection number {} from {} to port {}".format(proto, count, items[4], port))
                if count >= 3:
                    return True
    
        if count == 0:
            log("No connection found.")
        return False
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    Many thanks

    Code:
    #!/usr/bin/python
    from __future__ import print_function
    
    import xbmc
    import subprocess
    
    # Set of (protocol, local port) tuples.
    watched = { ('tcp', 9982), }
    sleep_time = 60 * 1000 # sleep time between checks in miliseconds
    
    def log(msg):
        print("service.inhibit_shutdown: {}".format(msg))
    
    def check_services():
        """ Check if any of the watched services is running. """
    
        netstat = subprocess.check_output(['/bin/netstat', '--protocol=inet', '-n'], universal_newlines=True)
    
    count=0
    
        for line in netstat.split('\n')[2:]:
            items = line.split()
            if len(items) < 4:
                continue
    
            proto = items[0]
            port = int(items[3].split(':')[-1])
    
                   if (proto, port) in watched:
                count += 1
                log("Found {} connection number {} from {} to port {}".format(proto, count, items[4], port))
                if count >= 3:
                    return True
    
        if count == 0:
            log("No connection found.")
        return False
    
    while not xbmc.abortRequested:
        if check_services():
            xbmc.executebuiltin('InhibitIdleShutdown(true)')
        else:
            xbmc.executebuiltin('InhibitIdleShutdown(false)')
        xbmc.sleep(sleep_time))
    is giving an error:

    Error Contents: ('unexpected indent', ('/home/billy/.xbmc/addons/service.inhibit_shutdown/inhibit_shutdown.py', 21, 4, " for line in netstat.split('\\n')[2:]:\n"))
    IndentationError: ('unexpected indent', ('/home/billy/.xbmc/addons/service.inhibit_shutdown/inhibit_shutdown.py', 21, 4, " for line in netstat.split('\\n')[2:]:\n"))

    edit: i redid the spaces in notepad and it seems have loaded okay now.

IMN logo majestic logo threadwatch logo seochat tools logo