# Path: opsmgr/common/utils.py
# def entry_exit(exclude_index=None, exclude_name=None, log_name=None, level=logging.INFO):
#     """
#     it's a decorator that to add entry and exit log for a function
# 
#     input:
#     excludeIndex -- the index of params that you don't want to be record
#     excludeName -- the name of dictionary params that you don't wnat to be record
#     log_name -- name of the log to write the logging to.
#     level -- the logging level to log the entry/exit with.  default is INFO level
#     """
# 
#     def f(func):
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
# 
#             args_for_print = []
#             tmp_index = 0
#             arg_len = len(args)
#             while tmp_index < arg_len:
#                 if tmp_index not in exclude_index:
#                     args_for_print.append(args[tmp_index])
#                 tmp_index += 1
# 
#             kwargs_for_print = {}
#             for a in kwargs:
#                 if a in exclude_name:
#                     continue
#                 else:
#                     kwargs_for_print[a] = kwargs[a]
# 
#             logging.getLogger(log_name).log(level,
#                                             "%s::Entry params %s %s", func.__name__, "{}".format(
#                                                 args_for_print), "{}".format(kwargs_for_print))
#             result = func(*args, **kwargs)
#             logging.getLogger(log_name).log(level,
#                                             "%s::Exit %s ", func.__name__, "{}".format(result))
#             return result
#         return wrapper
#     return f


import logging
import select
import socket
import sys
import termios
import tty
import paramiko
    from StringIO import StringIO
    from io import StringIO
from paramiko.py3compat import u
from opsmgr.inventory import persistent_mgr
from opsmgr.common.utils import entry_exit
def _create_remote_connection(remote_access_label, address, userid, password, ssh_key_string):
    """ create a remote ssh connection based on the information provided.

    Args:
        remote_access_label: label of device we are connecting to.
        address: IPv4 address of device to connect to
        userid:  userid to authenticate with
        password: password of userid or ssh_key_string
        ssh_key_string: String containing an ssh private key
    """
    _method_ = 'remote_access._create_remote_connection'
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    if ssh_key_string:
        private_key = paramiko.RSAKey.from_private_key(ssh_key_string, password)
        ssh.connect(address, username=userid, pkey=private_key, look_for_keys=False)
    else:
        ssh.connect(address, username=userid, password=password, look_for_keys=False)
    chan = ssh.invoke_shell()
    print(_("Remote connection to device (%s) established successfully.") %
          remote_access_label)
    logging.info("%s::Remote connection to device (%s) established successfully.",
                 _method_, remote_access_label)
    _open_interactive_shell(chan)
    print(_("Remote connection to device (%s) disconnected.") %
          remote_access_label)
    logging.info("%s::Remote connection to device (%s) disconnected.",
                 _method_, remote_access_label)



# Path: opsmgr/common/utils.py
# def entry_exit(exclude_index=None, exclude_name=None, log_name=None, level=logging.INFO):
#     """
#     it's a decorator that to add entry and exit log for a function
# 
#     input:
#     excludeIndex -- the index of params that you don't want to be record
#     excludeName -- the name of dictionary params that you don't wnat to be record
#     log_name -- name of the log to write the logging to.
#     level -- the logging level to log the entry/exit with.  default is INFO level
#     """
# 
#     def f(func):
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
# 
#             args_for_print = []
#             tmp_index = 0
#             arg_len = len(args)
#             while tmp_index < arg_len:
#                 if tmp_index not in exclude_index:
#                     args_for_print.append(args[tmp_index])
#                 tmp_index += 1
# 
#             kwargs_for_print = {}
#             for a in kwargs:
#                 if a in exclude_name:
#                     continue
#                 else:
#                     kwargs_for_print[a] = kwargs[a]
# 
#             logging.getLogger(log_name).log(level,
#                                             "%s::Entry params %s %s", func.__name__, "{}".format(
#                                                 args_for_print), "{}".format(kwargs_for_print))
#             result = func(*args, **kwargs)
#             logging.getLogger(log_name).log(level,
#                                             "%s::Exit %s ", func.__name__, "{}".format(result))
#             return result
#         return wrapper
#     return f


# Path: opsmgr/inventory/remote_access.py
import logging
import select
import socket
import sys
import termios
import tty
import paramiko
    from StringIO import StringIO
    from io import StringIO
from paramiko.py3compat import u
from opsmgr.inventory import persistent_mgr
from opsmgr.common.utils import entry_exit

def _create_remote_connection(remote_access_label, address, userid, password, ssh_key_string):
    """ create a remote ssh connection based on the information provided.

    Args:
        remote_access_label: label of device we are connecting to.
        address: IPv4 address of device to connect to
        userid:  userid to authenticate with
        password: password of userid or ssh_key_string
        ssh_key_string: String containing an ssh private key
    """
    _method_ = 'remote_access._create_remote_connection'
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    if ssh_key_string:
        private_key = paramiko.RSAKey.from_private_key(ssh_key_string, password)
        ssh.connect(address, username=userid, pkey=private_key, look_for_keys=False)
    else:
        ssh.connect(address, username=userid, password=password, look_for_keys=False)
    chan = ssh.invoke_shell()
    print(_("Remote connection to device (%s) established successfully.") %
          remote_access_label)
    logging.info("%s::Remote connection to device (%s) established successfully.",
                 _method_, remote_access_label)
    _open_interactive_shell(chan)
    print(_("Remote connection to device (%s) disconnected.") %
          remote_access_label)
    logging.info("%s::Remote connection to device (%s) disconnected.",
                 _method_, remote_access_label)



@entry_exit(exclude_index=[], exclude_name=[])