diff --git a/cf_remote/main.py b/cf_remote/main.py index d97f016..47615df 100644 --- a/cf_remote/main.py +++ b/cf_remote/main.py @@ -1,6 +1,8 @@ import argparse import os import sys +import re +import socket from cf_remote import log from cf_remote import version @@ -504,7 +506,7 @@ def validate_command(command, args): if command in ["sudo", "run"]: if len(args.remote_command) != 1: raise CFRExitError( - "cf-remote sude/run requires exactly 1 command (use quotes)" + "cf-remote sudo/run requires exactly 1 command (use quotes)" ) args.remote_command = args.remote_command[0] @@ -613,6 +615,13 @@ def get_cloud_hosts(name, bootstrap_ips=False): return ret +def dns_lookup(name): + try: + return bool(socket.getaddrinfo(name, 22)) + except socket.gaierror as e: + raise CFRUserError("DNS lookup failed for '{}': {}".format(name, e)) + + def resolve_hosts(string, single=False, bootstrap_ips=False): log.debug("resolving hosts from '{}'".format(string)) if is_file_string(string): @@ -627,8 +636,12 @@ def resolve_hosts(string, single=False, bootstrap_ips=False): hosts = get_cloud_hosts(name, bootstrap_ips) ret.extend(hosts) log.debug("found in cloud, adding '{}'".format(hosts)) - else: + elif name.startswith("@"): + raise CFRUserError("'{}' does not exist.".format(name)) + elif re.search(r"[@:.]", name) or dns_lookup(name): ret.append(name) + else: + raise CFRUserError("'{}' does not exist.".format(name)) if single: if len(ret) != 1: