Example #1
0
    def test_list_basic(self):
        l = PortList()

        self.assertTrue(l.add('42-47'))
        self.assertEqual(l.to_arglist(), ['42-47'])
        self.assertEqual(list(iter(l)), list(xrange(42, 48)))

        self.assertTrue(l.add('101-120'))
        self.assertTrue(l.add('200'))
        self.assertEqual(
                list(iter(l)),
                list(xrange(42, 48)) + list(xrange(101, 121)) + [200])
        self.assertEqual(str(l), '42-47,101-120,200')
Example #2
0
 def test_list_compact(self):
     l = PortList()
     self.assertTrue(l.add('42-47'))
     self.assertEqual(l._ranges, [(42, 47)])
     self.assertTrue(l.add('101-120'))
     self.assertEqual(l._ranges, [(42, 47), (101, 120)])
     self.assertTrue(l.add('45-52'))
     self.assertEqual(l._ranges, [(42, 52), (101, 120)])
     self.assertTrue(l.add('53-57'))
     self.assertEqual(l._ranges, [(42, 57), (101, 120)])
     self.assertTrue(l.add('30-44'))
     self.assertEqual(l._ranges, [(30, 57), (101, 120)])
Example #3
0
 def test_list_compact(self):
     l = PortList()
     self.assertTrue(l.add('42-47'))
     self.assertEqual(l._ranges, [(42, 47)])
     self.assertTrue(l.add('101-120'))
     self.assertEqual(l._ranges, [(42, 47), (101, 120)])
     self.assertTrue(l.add('45-52'))
     self.assertEqual(l._ranges, [(42, 52), (101, 120)])
     self.assertTrue(l.add('53-57'))
     self.assertEqual(l._ranges, [(42, 57), (101, 120)])
     self.assertTrue(l.add('30-44'))
     self.assertEqual(l._ranges, [(30, 57), (101, 120)])
Example #4
0
    def test_list_basic(self):
        l = PortList()

        self.assertTrue(l.add('42-47'))
        self.assertEqual(l.to_arglist(), ['42-47'])
        self.assertEqual(list(iter(l)), list(xrange(42, 48)))

        self.assertTrue(l.add('101-120'))
        self.assertTrue(l.add('200'))
        self.assertEqual(list(iter(l)),
                         list(xrange(42, 48)) + list(xrange(101, 121)) + [200])
        self.assertEqual(str(l), '42-47,101-120,200')
Example #5
0
 def test_bad_spec(self):
     l = PortList()
     self.assertRaises(ValueError, l.add, '47-42')
     self.assertFalse(l.add('42-45-47'))
Example #6
0
def run():
    argc = len(sys.argv)

    if argc > 1 and sys.argv[1] == _CMD_SWITCH_SERVER:
        if argc > 2 and sys.argv[2] == '-l':
            assert argc > 3
            rev_host = sys.argv[3]
            i = 4
        else:
            try:
                rev_host = os.environ['SSH_CLIENT']
                rev_host = rev_host.split(' ')[0]
            except KeyError:
                raise ConfigurationError(
                    _(u"Couldn't guess local hostname from SSH environment "
                      "-- please use -l"))
            i = 2
        rports = PortList()
        while i < argc:
            rports.add(sys.argv[i])
            i += 1
        remote = Remote(sys.stdin, sys.stdout)
        runner = PortKnocker(remote, rev_host)
        runner.run(False, rports)
        remote.close()
        sys.exit(0)

    # Parse the command line
    lports = PortList()
    rports = PortList()
    ssh_args = None
    ssh_prog = None
    host = None
    rev_host = None
    i = 1
    while i < argc:
        arg = sys.argv[i]

        # Options
        if arg == '--':
            ssh_args = sys.argv[i + 1:]
            break
        elif arg == '-h':
            usage(sys.stdout)
            sys.exit(0)
        elif arg == '-s':
            if i + 1 >= argc:
                raise ConfigurationError(_(u"Option -s requires an argument"))
            elif ssh_prog is not None:
                raise ConfigurationError(
                    _(u"Option -s was specified more "
                      "than once"))
            ssh_prog = sys.argv[i + 1]
            i += 1
        elif arg == '-r':
            if i + 1 >= argc:
                raise ConfigurationError(_(u"Option -r requires an argument"))
            elif host is not None:
                raise ConfigurationError(
                    _(u"Option -r was specified more "
                      "than once"))
            host = sys.argv[i + 1]
            i += 1
        elif arg == '-l':
            if i + 1 >= argc:
                raise ConfigurationError(_(u"Option -l requires an argument"))
            elif host is not None:
                raise ConfigurationError(
                    _(u"Option -l was specified more "
                      "than once"))
            rev_host = sys.argv[i + 1]
            i += 1
        else:
            if arg and arg[0] == '-':
                arg = arg[1:]
                port_list = lports
            else:
                port_list = rports

            if not port_list.add(arg):
                # Not ports: assume beginning of remaining parameters
                ssh_args = sys.argv[i:]
                break
        i += 1

    if not ssh_args:
        raise ConfigurationError(_(u"ssh arguments were not specified"))

    if not ssh_prog:
        ssh_prog = 'ssh'

    if not host:
        m = re.match(r'^(?:[^@: ]+@)?([^@: ]+)$', ssh_args[0])
        if m is None:
            raise ConfigurationError(
                _(u"Couldn't guess remote hostname from "
                  "SSH command -- please use -r"))
        host = m.group(1)

    # Run the command on the other side
    try:
        ssh_cmd = [ssh_prog] + ssh_args + [_CMD_SWITCH_SERVER]
        if rev_host:
            ssh_cmd += ['-l', rev_host]
        ssh_cmd += rports.to_arglist()
        proc = subprocess.Popen(ssh_cmd,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)
    except OSError:
        raise ConfigurationError(_(u"Couldn't start the ssh process"))

    # Create a communication wrapper
    remote = Remote(proc.stdout, proc.stdin)

    # Run
    try:
        runner = PortKnocker(remote, host)
        runner.run(True, lports)
    except EndOfStream:
        sys.stderr.write(_(u"Server exited prematurely") + u"\n")
        sys.exit(1)
    finally:
        remote.close()
        sys.exit(0)
Example #7
0
 def test_bad_spec(self):
     l = PortList()
     self.assertRaises(ValueError, l.add, '47-42')
     self.assertFalse(l.add('42-45-47'))
Example #8
0
 def test_simplelist(self):
     l = PortList()
     self.assertEqual(len(l._ranges), 0)
Example #9
0
 def test_portlist(self):
     PortList()
Example #10
0
def run():
    argc = len(sys.argv)

    if argc > 1 and sys.argv[1] == _CMD_SWITCH_SERVER:
        if argc > 2 and sys.argv[2] == '-l':
            assert argc > 3
            rev_host = sys.argv[3]
            i = 4
        else:
            try:
                rev_host = os.environ['SSH_CLIENT']
                rev_host = rev_host.split(' ')[0]
            except KeyError:
                raise ConfigurationError(_(
                        u"Couldn't guess local hostname from SSH environment "
                        "-- please use -l"))
            i = 2
        rports = PortList()
        while i < argc:
            rports.add(sys.argv[i])
            i += 1
        remote = Remote(sys.stdin, sys.stdout)
        runner = PortKnocker(remote, rev_host)
        runner.run(False, rports)
        remote.close()
        sys.exit(0)

    # Parse the command line
    lports = PortList()
    rports = PortList()
    ssh_args = None
    ssh_prog = None
    host = None
    rev_host = None
    i = 1
    while i < argc:
        arg = sys.argv[i]

        # Options
        if arg == '--':
            ssh_args = sys.argv[i+1:]
            break
        elif arg == '-h':
            usage(sys.stdout)
            sys.exit(0)
        elif arg == '-s':
            if i + 1 >= argc:
                raise ConfigurationError(_(u"Option -s requires an argument"))
            elif ssh_prog is not None:
                raise ConfigurationError(_(u"Option -s was specified more "
                                           "than once"))
            ssh_prog = sys.argv[i + 1]
            i += 1
        elif arg == '-r':
            if i + 1 >= argc:
                raise ConfigurationError(_(u"Option -r requires an argument"))
            elif host is not None:
                raise ConfigurationError(_(u"Option -r was specified more "
                                           "than once"))
            host = sys.argv[i + 1]
            i += 1
        elif arg == '-l':
            if i + 1 >= argc:
                raise ConfigurationError(_(u"Option -l requires an argument"))
            elif host is not None:
                raise ConfigurationError(_(u"Option -l was specified more "
                                           "than once"))
            rev_host = sys.argv[i + 1]
            i += 1
        else:
            if arg and arg[0] == '-':
                arg = arg[1:]
                port_list = lports
            else:
                port_list = rports

            if not port_list.add(arg):
                # Not ports: assume beginning of remaining parameters
                ssh_args = sys.argv[i:]
                break
        i += 1

    if not ssh_args:
        raise ConfigurationError(_(u"ssh arguments were not specified"))

    if not ssh_prog:
        ssh_prog = 'ssh'

    if not host:
        m = re.match(r'^(?:[^@: ]+@)?([^@: ]+)$', ssh_args[0])
        if m is None:
            raise ConfigurationError(_(u"Couldn't guess remote hostname from "
                                       "SSH command -- please use -r"))
        host = m.group(1)

    # Run the command on the other side
    try:
        ssh_cmd = [ssh_prog] + ssh_args + [_CMD_SWITCH_SERVER]
        if rev_host:
            ssh_cmd += ['-l', rev_host]
        ssh_cmd += rports.to_arglist()
        proc = subprocess.Popen(ssh_cmd,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)
    except OSError:
        raise ConfigurationError(_(u"Couldn't start the ssh process"))

    # Create a communication wrapper
    remote = Remote(proc.stdout, proc.stdin)

    # Run
    try:
        runner = PortKnocker(remote, host)
        runner.run(True, lports)
    except EndOfStream:
        sys.stderr.write(_(u"Server exited prematurely") + u"\n")
        sys.exit(1)
    finally:
        remote.close()
        sys.exit(0)