コード例 #1
0
def main():  # pragma: no cover
    args = parse_args()
    if os.access(args.hosts_file, os.R_OK):
        known_hosts = asyncssh.read_known_hosts(args.hosts_file)
        match = known_hosts.match(args.dst_address, "0.0.0.0",
                                  args.dst_port)[0]
        if match:
            print("Host already added to known_hosts", file=sys.stderr)
            exit(4)
    loop = asyncio.get_event_loop()
    hostkey = loop.run_until_complete(
        asyncssh.get_server_host_key(args.dst_address, args.dst_port))
    loop.close()
    if hostkey is None:
        print("Unable to retrieve hostkey", file=sys.stderr)
        exit(3)
    print("%s key fingerprint is %s." %
          (hostkey.get_algorithm(), hostkey.get_fingerprint()))
    inp = input("Do you want to trust this key (yes/no)? ").lower()
    while True:
        if inp == 'yes':
            hostkey_export = hostkey.export_public_key('openssh').\
                decode('ascii').\
                rstrip('\n')
            os.makedirs(os.path.dirname(args.hosts_file),
                        mode=0o700,
                        exist_ok=True)
            with open(args.hosts_file, 'a') as f:
                print("%s %s" % (args.dst_address, hostkey_export), file=f)
                exit(0)
        elif inp == 'no':
            exit(0)
        else:
            inp = input("Please type 'yes' or 'no': ").lower()
コード例 #2
0
ファイル: __main__.py プロジェクト: todokku/rsp
async def amain(args, loop):  # pragma: no cover
    logger = logging.getLogger('MAIN')

    try:
        known_hosts = asyncssh.read_known_hosts(args.hosts_file)
    except Exception as exc:
        logger.error("Host keys loading failed with error: %s", str(exc))
        host_keys, ca_keys, x509_certs, x509_subjects = [], [], [], []
    else:
        host_keys, ca_keys, _, x509_certs, _, x509_subjects, _ = \
            known_hosts.match(args.dst_address, "", args.dst_port)
    if not (host_keys or ca_keys or x509_certs or x509_subjects):
        logger.critical(
            "Specified host is not found in known hosts. "
            "Please run following command: "
            "rsp-trust '%s' %d", args.dst_address, args.dst_port)
        return
    options = partial(ssh_options_from_args, args, known_hosts)

    ratelimit = Ratelimit(args.connect_rate)
    pool = SSHPool(dst_address=args.dst_address,
                   dst_port=args.dst_port,
                   ssh_options=options,
                   timeout=args.timeout,
                   backoff=args.backoff,
                   ratelimit=ratelimit,
                   size=args.pool_size,
                   loop=loop)
    async with pool:
        logger.warning(
            "SSH connection pool is starting up. Pool target: "
            "%d steady connections. It will take at least %.2f "
            "seconds to reach it's full size.", args.pool_size,
            args.pool_size * 1. / args.connect_rate)
        if args.transparent:
            from .transparentlistener import TransparentListener as Listener
        else:
            from .sockslistener import SocksListener as Listener
        server = Listener(listen_address=args.bind_address,
                          listen_port=args.bind_port,
                          timeout=args.timeout,
                          pool=pool,
                          loop=loop)
        async with server:
            logger.info("Server started.")

            exit_event = asyncio.Event()
            async with utils.Heartbeat():
                sig_handler = partial(utils.exit_handler, exit_event)
                signal.signal(signal.SIGTERM, sig_handler)
                signal.signal(signal.SIGINT, sig_handler)
                async with AsyncSystemdNotifier() as notifier:
                    await notifier.notify(b"READY=1")
                    await exit_event.wait()

                    logger.debug(
                        "Eventloop interrupted. Shutting down server...")
                    await notifier.notify(b"STOPPING=1")
コード例 #3
0
    def test_read_known_hosts(self):
        """Test connecting with known hosts object from read_known_hosts"""

        known_hosts_path = os.path.join('.ssh', 'known_hosts')
        known_hosts = asyncssh.read_known_hosts(known_hosts_path)

        with (yield from self.connect(known_hosts=known_hosts)) as conn:
            pass

        yield from conn.wait_closed()