Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paramiko.client
- import os.path
- import sys
- import configparser
- def get_ssh_config():
- config_file = os.path.expanduser("~/.ssh/config")
- if not os.path.exists(config_file):
- return None
- ssh_config = paramiko.SSHConfig()
- ssh_config.parse(config_file)
- return ssh_config
- def ssh_connect(host, ssh_config=None, known_hosts=None):
- config = { 'hostname': host, 'compress': True }
- if not ssh_config is None:
- user_config = ssh_config.lookup(host)
- for k in ('hostname', 'username', 'port'):
- if k in user_config:
- config[k] = user_config[k]
- if 'proxycommand' in user_config:
- config['sock'] = paramiko.ProxyCommand(user_config['proxycommand'])
- if 'identityfile' in user_config:
- config['key_filename'] = user_config['identityfile']
- client = paramiko.client.SSHClient()
- # client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
- client.connect(*config)
- return client
- def download_querylog(fleet, from_date, to_date):
- config_file = os.path.expanduser('~/.querylog.ini')
- if not os.path.exists(config_file):
- print("No config file ~/.querylog.ini", file=sys.stderr)
- return
- config = configparser.ConfigParser()
- config.read(config_file)
- fleet_section = "fleet." + fleet
- if not config.has_section(fleet_section):
- print("No fleet %s defined in ~/.querylog.ini" % fleet, file=sys.stderr)
- return
- fleet_hosts = config.get(fleet_section, "hosts")
- if fleet_hosts is None:
- print("Fleet %s didn't define any host" % fleet)
- return
- fleet_hosts = [ h.strip() for h in fleet_hosts.split(',')]
- fleet_package = config.get(fleet_service, "package")
- if package is None:
- print("Fleet %s didn't define a package to retrieve")
- ssh_config = get_ssh_config()
- for host in fleet_hosts:
- sftp_client = ssh_connect(host, ssh_config=ssh_config).open_sftp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement