ssh.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 from contextlib import contextmanager
5 import os
6 
7 import paramiko
8 
9 
10 def connect_ssh(host, username=None, password=None):
11  return _connect_ssh_context(host, username, password)
12 
13 
14 @contextmanager
15 def _connect_ssh_context(host, username, password):
16  try:
17  ssh = paramiko.SSHClient()
18  ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
19  ssh.connect(host, username=username, password=password)
20  yield ssh
21  finally:
22  ssh.close()
23 
24 
25 def get_user_by_hostname(hostname):
26  ssh_config_file = os.path.expanduser('~/.ssh/config')
27  if not os.path.exists(ssh_config_file):
28  return
29  with open(ssh_config_file) as f:
30  ssh_config = paramiko.util.parse_ssh_config(f)
31  for entry in ssh_config._config:
32  if 'config' not in entry:
33  continue
34  config = entry['config']
35  if config.get('hostname') == hostname:
36  return config.get('user')
def connect_ssh(host, username=None, password=None)
Definition: ssh.py:10
def get_user_by_hostname(hostname)
Definition: ssh.py:25
def _connect_ssh_context(host, username, password)
Definition: ssh.py:15


jsk_data
Author(s):
autogenerated on Tue Feb 6 2018 03:45:36