[<<][python][>>][..]
Sun Oct 12 16:12:30 EDT 2014

pexpect ssh support phone home

import pexpect # http://pexpect.readthedocs.org/en/latest/
import os

class channel:
    def __init__(self):
        self.px = False
        self.connect()

    def connect(self):
        # the other end is a 'command=' line in .ssh/authorized_keys
        # starting socat on a unix socket.
        # to test this can be run here directly
        # command = "socat - UNIX-LISTEN:/tmp/channel.sock"
        command = "ssh -q -F python/support.config support"
        print("%s" % ['connect',command])
        self.px = pexpect.spawn(command)
        self.px.setecho(False)

    def next(self):
        self.px.expect('\r\n', timeout=60*60*24)
        return(self.px.before)

    def loop(self):
        while True:
            cmd = self.next().decode("utf8")
            print("%s" % ['command',cmd])
            val = eval(cmd)
            msg = '%s' % ['value',val]
            print(msg)
            self.px.sendline(msg)

    def start(self):
        while True:
            try:
                self.loop()
            except pexpect.EOF:
                msg = '%s' % ['error','EOF']
                print("%s" % msg)
                self.connect()
            except Exception as e:
                msg = '%s' % ['error',e]
                self.px.sendline(msg)
                print("%s" % msg)

## support.config is a ssh config file containing something like:
## support_key and support_key.pub are rsa keys
# Host support
# Port 443
# User supportuser
# Hostname support.enterprise.com
# PasswordAuthentication no
# IdentitiesOnly yes
# IdentityFile ./support_key



[Reply][About]
[<<][python][>>][..]