The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Netstat detect number of connection on port
Discuss Netstat detect number of connection on port in the Python Programming forum on Dev Shed. Netstat detect number of connection on port Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 11th, 2012, 02:36 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 18 m 41 sec
Reputation 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)
|

December 14th, 2012, 04:21 AM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 39
Time spent in forums: 10 h 49 m 43 sec
Reputation Power: 1
|
|
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
|

December 16th, 2012, 06:48 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 18 m 41 sec
Reputation 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|