def connection(host): cfg = Config(overrides={"run": {'echo': True, 'hide': False}}) with Connection( host, user=env.user, connect_kwargs={"key_filename": env.key_file_name, "allow_agent": True}, gateway=bastion_connection(), config=cfg, forward_agent=True, ) as conn: yield conn
def validate_host(host: str) -> None: for validator in [ipv4, ipv6, domain]: if validator(host) is True: break else: if not Config: return patterns = Config().base_ssh_config.get_hostnames() # TODO: more complex filter known_hosts = [p for p in patterns if '*' not in p] known_hosts += ['localhost'] if host not in known_hosts: raise ValidationError(f'"{host}" is not a valid host')
""" biz is stand for business e.g: /opt/<company abbr>/<sub-biz abbr>/{data, log} :return: """ pass if __name__ == '__main__': for host in hosts_ssh_config.strip().split("\n"): # get host config from 'hosts_ssh_config' name, ip, port, username, password, is_sudo, tag, description = host.strip( ).split(",") # type: (str,) # connection config fabric_config = Config() fabric_config['load_ssh_config'] = False fabric_config['port'] = int(port) fabric_config['user'] = username fabric_config['sudo'] = {'password': password} fabric_config['connect_kwargs'] = { 'password': password, "key_filename": r"C:\Users\dgden\.ssh\ebt-linux-centos-ssh-root-key.pem", } # Superuser privileges via auto-response sudo_pass_auto_respond = Responder( pattern=r'\[sudo\] password:'******'\n',
from fabric import Connection from fabric.group import ThreadingGroup from fabric.exceptions import GroupException from fabric.config import Config from invoke import task import boto3 config = Config(user_ssh_path="/Users/rajgouravj/.ssh/oregon_config") @task(iterable=["tag_key","tag_value"]) def execute(c,tag_key,tag_value,**kwargs): for key,value in kwargs.items(): print(key,value) target_hosts=[] client = boto3.client("ec2") response = client.describe_instances( Filters=[ { 'Name': "tag:" + tag_key, 'Values': [tag_value] }, { 'Name': "instance-state-name", 'Values': ["running"] } ] ) for r in response['Reservations']: for i in r["Instances"]: target_hosts.append(i["PrivateIpAddress"])