-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


timeout & strace Shellinabox

, 06 2017 . 13:15 +

, web-ssh -. , ssh- web-. , web-ssh , , , IP, , ( ) . .


-, , Guacamole, , , Guacamole .

, shellinabox , . , Docker, GitHub Dockerhub, .


, , Python, . , , web ssh - , , . :


  1. - , ;
  2. ssh .

, , , shellinabox , (stdin) stdout .


Google , timeout strace. timeout , , .


strace , , , , - . , stdin, stdout , strace :


strace -e write=1,2 -e trace=write -p 

, , . :


  1. shellinabox python-;
  2. Python- (fork) - , PID;
  3. python- (exec) ssh ( ssh).

, , . .


, .


monitor_daemon(inactivity_interval, identity_file)
...
...
os.execv("/usr/bin/ssh", ["/usr/bin/ssh"] + identity_args + ["-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-p", str(peer_port), "%s@%s" % (peer_login, peer_ip)])

. monitor_daemon:


def monitor_daemon(inactivity_interval, identity_file):
    orig_pid = os.getpid()
    try:
        pid = os.fork()
        if pid > 0:
            return
    except OSError as e:
        print("Fork #1 failed: %d (%s)" % (e.errno, e.strerror))
        sys.exit(1)

    os.chdir("/")
    os.setsid()
    os.umask(0)

    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError as e:
        print("Fork #2 failed: %d (%s)" % (e.errno, e.strerror))
        sys.exit(1)

    if identity_file != "":
        time.sleep(1)
        os.unlink(identity_file)

    try:
        while True:
            proc = subprocess.Popen('timeout %d strace -e write=1,2 -e trace=write -p %d' % (inactivity_interval, orig_pid), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            proc.poll()
            counter = 0
            for line in proc.stderr.readlines():
                counter += 1
            if(counter <= 3):
                os.kill(orig_pid, signal.SIGKILL)
                sys.exit(0)
    except Exception as e:
        pass

    sys.exit(0)

:


while True:
  proc = subprocess.Popen('timeout %d strace -e write=1,2 -e trace=write -p %d' % (inactivity_interval, orig_pid), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  proc.poll()
  counter = 0
  for line in proc.stderr.readlines():
    counter += 1
    if(counter <= 3):
      os.kill(orig_pid, signal.SIGKILL)
      sys.exit(0)

, ssh, .


. , .


PS: python-, .
PPS: - , , PR-.

Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/332544/

:  

: [1] []
 

:
: 

: ( )

:

  URL