Kodi Community Forum
Linux Delay shutdown when SSH or Samba connections are opened - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Support (https://forum.kodi.tv/forumdisplay.php?fid=33)
+--- Forum: General Support (https://forum.kodi.tv/forumdisplay.php?fid=111)
+---- Forum: OS independent / Other (https://forum.kodi.tv/forumdisplay.php?fid=228)
+---- Thread: Linux Delay shutdown when SSH or Samba connections are opened (/showthread.php?tid=143774)

Pages: 1 2 3


Delay shutdown when SSH or Samba connections are opened - cube - 2012-10-28

Hi, I'm running XBMC on Fedora 17 and I'm looking for a way to disable shutdown timer when there are active samba or ssh connections to the computer. Is there "the way" of doing this?

Thanks


RE: Delay shutdown when SSH or Samba connections are opened - Ned Scott - 2012-10-28

I don't think the XBMC timer knows about other processes, so probably not.


RE: Delay shutdown when SSH or Samba connections are opened - cube - 2012-10-28

Thanks for the answer, but I was thinking more about something like a python script that monitors the other services and blocks suspend when necessary.


RE: Delay shutdown when SSH or Samba connections are opened - cube - 2012-12-01

I wrote a service that does this: https://github.com/bluecube/service.inhibit_shutdown ... if anyone finds it usefull.


RE: Delay shutdown when SSH or Samba connections are opened - artrafael - 2012-12-01

Just as an FYI: If the client is running XBMC, you can use the Advanced WOL add-on to send continuous WOL magic packets, at user-defined intervals, to your XBMC "server" so the shutdown function timer doesn't kick in.


RE: Delay shutdown when SSH or Samba connections are opened - cube - 2012-12-01

In my usage scenario I have XBMC server that is set to suspend pretty often and connect to it from other machines at home.
Clients don't run XBMC or any other specific program.

... XBMC restarts its shutdown timer if the machine receives WOL packet? How does that work?


RE: Delay shutdown when SSH or Samba connections are opened - artrafael - 2012-12-01

(2012-12-01, 21:34)cube Wrote: ... XBMC restarts its shutdown timer if the machine receives WOL packet? How does that work?

That's a good question. I don't know exactly how it works, but my main XBMC system hosts all my media files and it's set to suspend via Shutdown function time after 30 minutes of inactivity. In the past, if I wanted to listen to music or watch a video from another system in the house, I would have to temporarily disable the shutdown function timer on the main system. Now, with Advanced WOL sending out periodic WOL packets from my client XBMC system every 60 seconds, the server XBMC stays up. I don't know if the WOL packet resets the timer (e.g., giving me another 30 minutes) or if the system goes into suspend, but is reawakened within 60 seconds and, because there's enough data buffered, I don't see/hear any interruption in video/audio playback.

But your question has piqued my curiosity and I'll have to run a test and leave my main XBMC sitting on the homepage and play a movie from my client (with AWOL sending out its periodic packets). When it gets close to the 30 minute mark, I'll keep an eye on the main system and see if the image on the TV it's connected to ever blanks out and then comes back on within 60 seconds (I have screensaver and display blanking disabled on that system).

Of course, if someone already knows the answer to this and can chime in, they can save me the trouble. Smile



RE: Delay shutdown when SSH or Samba connections are opened - artrafael - 2012-12-02

I tested this and the XBMC shutdown function timer is not reset when it receives a WOL packet. The host XBMC system goes into suspend after the specified period of inactivity, but is reawakened soon thereafter by the next WOL packet. There's enough data in the buffer to keep playback from getting interrupted, so I was under the mistaken impression that the host system never suspended. By the way, I rechecked my AWOL setting for the time interval between sending packets, and it's actually set to the default 30 seconds rather than 60 as I had stated above.


RE: Delay shutdown when SSH or Samba connections are opened - bilbonvidia - 2012-12-02

----


RE: Delay shutdown when SSH or Samba connections are opened - bilbonvidia - 2012-12-11

cool, can this be modified so that it only looks for three or instances of a certain port?

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)



RE: Delay shutdown when SSH or Samba connections are opened - Frank-NL - 2012-12-12

(2012-12-01, 20:09)cube Wrote: I wrote a service that does this: https://github.com/bluecube/service.inhibit_shutdown ... if anyone finds it usefull.

Thank you for that, really useful! I modified it for Usenet so XBMC won't idle while downloading Smile



RE: Delay shutdown when SSH or Samba connections are opened - cube - 2012-12-26

So I've made another update, this time you can specify the watched ports from GUI and there are two groups of ports watched -- local connection only (where local and remote address is the same) and remote connection only (where they are not the same, obviously).


RE: Delay shutdown when SSH or Samba connections are opened - bilbonvidia - 2013-01-04

Thanks I have just installed this to look for remote connection on port 9982 and ignore local connections.

So I have set watched remote connections to 9982. What should go in sleep time?

EDIT: works great.


RE: Delay shutdown when SSH or Samba connections are opened - cube - 2013-01-04

Huh ... there should be a default value of 60 ... I'll fix that :-)

It's a time between checks performed by the script, the exact value is not too important. What value is ok for you depends mainly on what type of connections you're using to your machine (10 seconds long ssh sessions would need somethig like 5 seconds to catch them, hour long samba sessions can have five minutes between checks with no problem) and on your timeout for xbmc idle shutdown.


RE: Delay shutdown when SSH or Samba connections are opened - Hobby-Student - 2013-02-17

Hey guys and girls,

I am slightly new to xbmc, so please be patient if some parts of my post are not that "pro" Wink

@cube:
Great plugin you have developed... I'm coming from another well known mediacenter-software running on Windows. There I managed to build my own plugins in C#, which now is not that much useful for XBMCbuntu. But (yeah, you knew it, there is a "but"...) I have some experience with linux, too. So I took a look at your python-script, because it always prevented standby on my machine.

Config:
remote ports: 22, 445, 9982
local ports: (none)
sleep time: 60

Problem:
The plugin always logs, that a connection from 127.0.0.1 to 127.0.0.1:9982 is preventing standby. I don't know if this only happens to me, but I added some lines to your script, so this problem won't exist anymore.

Modification:

from
Code:
if len(items) < 4:
    continue

local_addr, local_port = items[3].split(':')
remote_addr, remote_port = items[4].split(':')

local_port = int(local_port)

to
Code:
if len(items) < 4:
    continue

local_addr, local_port = items[3].split(':')
remote_addr, remote_port = items[4].split(':')

if (remote_addr == "127.0.0.1"):
    continue

local_port = int(local_port)

Is this one useful or am I missing something in configuration?

Kind regards
Chris


This forum uses Lukasz Tkacz MyBB addons.