def ssh_mkdir(self, path, host='127.0.0.1', username=None, password=None, port=22): """远程创建目录 通过传入的 path 和远程OS用户信息在远程创建一个目录 Args: path: 目录路径 host: OS IP username: 用户名 password: 密码 port: 端口 """ # 执行远程命令创建相关目录 cmd = ('source ~/.bash_profile && ' 'mkdir -p {path}'.format(path=path)) is_ok, stdout, stderr = self.ssh_exec_cmd(cmd, host, username, password) if is_ok: ToolLog.log_info('mkdir: {path} successful!'.format(path=path)) else: ToolLog.log_error('mkdir: {path} failure!!!'.format(path=path)) return is_ok
def ssh_trans(self, host='127.0.0.1', username=None, password=None, local_file=None, remote_file=None, port=22): """将文件传输到远程 或目录 将输入的一个文件从本地传输到远程, 这个文件必须是全路径的 Args: host: IP username: 远程OS用户名 password: 指定username密码 local_file: 本地的文件路径和文件名 remote_file: 传输到远程的文件(在远程的路径和文件名) port: ssh传输的端口 Return: True/False 是否传输成功 Raise: None """ is_ok = False # 如果是文件则就只需要传输一个文件 if os.path.isfile(local_file): ToolLog.log_info('is file') is_ok = self.ssh_trans_file(host, username, password, local_file, remote_file) # 如果是目录则就需要传输这个目录下的所有文件 elif os.path.isdir(local_file): ToolLog.log_info('is dir') is_ok = self.ssh_trans_dir(host, username, password, local_file, remote_file) return is_ok
def exec_cmd(self, cmd): """执行命令 执行通过给与的命令 Args: cmd: 需要执行的操作系统命令 Return: True/False 返回这个命令是否执行成功了 Raise: None """ cmd = '{prefix} {cmd}'.format( prefix = self.CMD_PREFIX, cmd = cmd) ToolLog.log_info(cmd) child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) child.wait() err_code = child.poll() stdout = child.stdout.read() stderr = child.stderr.read() if err_code != 0: ToolLog.log_error(stdout) ToolLog.log_error(stderr) return False else: ToolLog.log_info(stdout) ToolLog.log_info(stderr) return True
def ssh_mkdir(self, path, host='127.0.0.1', username=None, password=None, port=22): """远程创建目录 通过传入的 path 和远程OS用户信息在远程创建一个目录 Args: path: 目录路径 host: OS IP username: 用户名 password: 密码 port: 端口 """ # 执行远程命令创建相关目录 cmd = ('source ~/.bash_profile && ' 'mkdir -p {path}'.format(path=path)) is_ok, stdout, stderr = self.ssh_exec_cmd( cmd, host, username, password) if is_ok: ToolLog.log_info('mkdir: {path} successful!'.format(path=path)) else: ToolLog.log_error('mkdir: {path} failure!!!'.format(path=path)) return is_ok
def exec_cmd(self, cmd): """执行命令 执行通过给与的命令 Args: cmd: 需要执行的操作系统命令 Return: True/False 返回这个命令是否执行成功了 Raise: None """ cmd = '{prefix} {cmd}'.format(prefix=self.CMD_PREFIX, cmd=cmd) ToolLog.log_info(cmd) child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) child.wait() err_code = child.poll() stdout = child.stdout.read() stderr = child.stderr.read() if err_code != 0: ToolLog.log_error(stdout) ToolLog.log_error(stderr) return False else: ToolLog.log_info(stdout) ToolLog.log_info(stderr) return True
def ssh_trans_file(self, host='127.0.0.1', username=None, password=None, local_file=None, remote_file=None, port=22): """将文件传输到远程 将输入的一个文件从本地传输到远程, 这个文件必须是全路径的 Args: host: IP username: 远程OS用户名 password: 指定username密码 local_file: 本地的文件路径和文件名 remote_file: 传输到远程的文件(在远程的路径和文件名) port: ssh传输的端口 Return: True/False 是否传输成功 Raise: None """ is_ok = False try: # 创建sftp实例用于远程传输 t = paramiko.Transport((host, port)) t.connect(username=username, password=password) ToolLog.log_info('connected {host} successful'.format(host=host)) sftp = paramiko.SFTPClient.from_transport(t) # 获得远程文件路径 parent_path, file_name = self.get_file_path_and_name(remote_file) # 执行远程命令创建相关目录 mkdir_is_ok = self.ssh_mkdir(parent_path, host, username, password) # 传输文件 sftp.put(local_file, remote_file) ToolLog.log_info('Upload file successful') is_ok = True except Exception, e: ToolLog.log_error('Upload file failure!!!') s = traceback.format_exc() ToolLog.log_error(s)
def ssh_exec_cmd(self, cmd, host='127.0.0.1', username=None, password=None, port=22): """将命令在远程执行 Args: cmd: 需要远程执行的命令 host: 远程的ip username: 用户名 password: 密码 Return: stdout, stderr, True/False 返回执行命令后的相关输出 Raise: None """ # 创建SSH对象 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # 创建 SSH 链接 ssh.connect(host, port, username, password, key_filename=None, timeout=2) ToolLog.log_info('connect host: {host}'.format(host=host)) ToolLog.log_info('connect username: {u}'.format(u=username)) ToolLog.log_info('connect password: {p}'.format(p=password)) ToolLog.log_info('want exec cmd: {cmd}'.format(cmd=cmd)) except Exception, e: ToolLog.log_error('connect host: {host}'.format(host=host)) ToolLog.log_error('connect username: {u}'.format(u=username)) ToolLog.log_error('connect password: {p}'.format(p=password)) ToolLog.log_error('want exec cmd: {cmd}'.format(cmd=cmd)) ToolLog.log_error(e)
def ssh_trans_dir(self, host='127.0.0.1', username=None, password=None, local_dir=None, remote_dir=None, port=22): """将目录传输到远程 将输入的一个目录从本地传输到远程, 这个目录必须是全路径的 Args: host: IP username: 远程OS用户名 password: 指定username密码 local_file: 本地的文件路径 remote_file: 传输到远程的路径(在远程的路径) port: ssh传输的端口 Return: True/False 是否传输成功 Raise: None """ is_ok = False # 构造一个同一个的目录结构: /aaa/bbb/ccc/ local_dir = local_dir.rstrip('/') local_dir = '{local_dir}/'.format(local_dir=local_dir) remote_dir = remote_dir.rstrip('/') remote_dir = '{remote_dir}/'.format(remote_dir=remote_dir) # 执行远程命令创建相关目录 mkdir_is_ok = self.ssh_mkdir(remote_dir, host, username, password) try: # 创建sftp实例用于远程传输 t = paramiko.Transport((host, port)) t.connect(username=username, password=password) ToolLog.log_info('connected {host} successful'.format(host=host)) sftp = paramiko.SFTPClient.from_transport(t) ToolLog.log_info('upload file start {time} '.format( time=datetime.datetime.now())) # 获得本地文件的路径已经文件 for root, dirs, files in os.walk(local_dir): is_ok = False for filespath in files: local_file = os.path.join(root, filespath) # 构造临时的文件 如: /tmp/bb/a.txt -> bb/a.txt a = local_file.replace(local_dir, '', 1) remote_file = os.path.join(remote_dir, a) try: sftp.put(local_file, remote_file) except Exception, e: tmp_dir, tmp_file = self.get_file_path_and_name( remote_file) sftp.mkdir(tmp_dir) sftp.put(local_file, remote_file) ToolLog.log_info( 'upload {local_file} to remote {remote_file}'.format( local_file=local_file, remote_file=remote_file)) for name in dirs: local_path = os.path.join(root, name) a = local_path.replace(local_dir, '', 1) remote_path = os.path.join(remote_dir, a) try: sftp.mkdir(remote_path) ToolLog.log_info('mkdir path {remote_path}'.format( remote_path=remote_path)) except Exception, e: ToolLog.log_error('mkdir {remote_path}'.format( remote_path=remote_path)) ToolLog.log_error(e) ToolLog.log_error(traceback.format_stack())
class ToolSSH(object): """这是一个可以通过ssh远程执行一些东西的类 """ def __init__(self): pass @classmethod def ssh_exec_cmd(self, cmd, host='127.0.0.1', username=None, password=None, port=22): """将命令在远程执行 Args: cmd: 需要远程执行的命令 host: 远程的ip username: 用户名 password: 密码 Return: stdout, stderr, True/False 返回执行命令后的相关输出 Raise: None """ # 创建SSH对象 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # 创建 SSH 链接 ssh.connect(host, port, username, password, key_filename=None, timeout=2) ToolLog.log_info('connect host: {host}'.format(host=host)) ToolLog.log_info('connect username: {u}'.format(u=username)) ToolLog.log_info('connect password: {p}'.format(p=password)) ToolLog.log_info('want exec cmd: {cmd}'.format(cmd=cmd)) except Exception, e: ToolLog.log_error('connect host: {host}'.format(host=host)) ToolLog.log_error('connect username: {u}'.format(u=username)) ToolLog.log_error('connect password: {p}'.format(p=password)) ToolLog.log_error('want exec cmd: {cmd}'.format(cmd=cmd)) ToolLog.log_error(e) # 执行命令并输出 stdin, stdout, stderr = ssh.exec_command(cmd) err_code = stdout.channel.recv_exit_status() out_msg = stdout.readlines() err_msg = stderr.readlines() ssh.close() # 返回 True/False is_ok = False if err_code != 0: ToolLog.log_error(out_msg) ToolLog.log_error(err_msg) ToolLog.log_error('error code: {code}'.format(code=err_code)) else: is_ok = True ToolLog.log_info(out_msg) ToolLog.log_info(err_msg) ToolLog.log_info('error code: {code}'.format(code=err_code)) return is_ok, out_msg, err_msg
sftp = paramiko.SFTPClient.from_transport(t) # 获得远程文件路径 parent_path, file_name = self.get_file_path_and_name(remote_file) # 执行远程命令创建相关目录 mkdir_is_ok = self.ssh_mkdir(parent_path, host, username, password) # 传输文件 sftp.put(local_file, remote_file) ToolLog.log_info('Upload file successful') is_ok = True except Exception, e: ToolLog.log_error('Upload file failure!!!') s = traceback.format_exc() ToolLog.log_error(s) finally: t.close() ToolLog.log_info('sftp closed!') return is_ok @classmethod def ssh_trans_dir(self, host='127.0.0.1', username=None, password=None, local_dir=None, remote_dir=None, port=22): """将目录传输到远程 将输入的一个目录从本地传输到远程, 这个目录必须是全路径的 Args: host: IP
def ssh_trans_dir(self, host='127.0.0.1', username=None, password=None, local_dir=None, remote_dir=None, port=22): """将目录传输到远程 将输入的一个目录从本地传输到远程, 这个目录必须是全路径的 Args: host: IP username: 远程OS用户名 password: 指定username密码 local_file: 本地的文件路径 remote_file: 传输到远程的路径(在远程的路径) port: ssh传输的端口 Return: True/False 是否传输成功 Raise: None """ is_ok = False # 构造一个同一个的目录结构: /aaa/bbb/ccc/ local_dir = local_dir.rstrip('/') local_dir = '{local_dir}/'.format(local_dir=local_dir) remote_dir = remote_dir.rstrip('/') remote_dir = '{remote_dir}/'.format(remote_dir=remote_dir) # 执行远程命令创建相关目录 mkdir_is_ok = self.ssh_mkdir(remote_dir, host, username, password) try: # 创建sftp实例用于远程传输 t = paramiko.Transport((host, port)) t.connect(username=username, password=password) ToolLog.log_info('connected {host} successful'.format(host=host)) sftp = paramiko.SFTPClient.from_transport(t) ToolLog.log_info( 'upload file start {time} '.format( time=datetime.datetime.now()) ) # 获得本地文件的路径已经文件 for root, dirs, files in os.walk(local_dir): is_ok = False for filespath in files: local_file = os.path.join(root, filespath) # 构造临时的文件 如: /tmp/bb/a.txt -> bb/a.txt a = local_file.replace(local_dir, '', 1) remote_file = os.path.join(remote_dir, a) try: sftp.put(local_file, remote_file) except Exception, e: tmp_dir, tmp_file = self.get_file_path_and_name(remote_file) sftp.mkdir(tmp_dir) sftp.put(local_file, remote_file) ToolLog.log_info( 'upload {local_file} to remote {remote_file}'.format( local_file = local_file, remote_file = remote_file ) ) for name in dirs: local_path = os.path.join(root, name) a = local_path.replace(local_dir, '', 1) remote_path = os.path.join(remote_dir, a) try: sftp.mkdir(remote_path) ToolLog.log_info( 'mkdir path {remote_path}'.format( remote_path = remote_path ) ) except Exception, e: ToolLog.log_error( 'mkdir {remote_path}'.format( remote_path = remote_path ) ) ToolLog.log_error(e) ToolLog.log_error(traceback.format_stack())
# 执行远程命令创建相关目录 mkdir_is_ok = self.ssh_mkdir(parent_path, host, username, password) # 传输文件 sftp.put(local_file, remote_file) ToolLog.log_info('Upload file successful') is_ok = True except Exception, e: ToolLog.log_error('Upload file failure!!!') s = traceback.format_exc() ToolLog.log_error(s) finally: t.close() ToolLog.log_info('sftp closed!') return is_ok @classmethod def ssh_trans_dir(self, host='127.0.0.1', username=None, password=None, local_dir=None, remote_dir=None, port=22): """将目录传输到远程 将输入的一个目录从本地传输到远程, 这个目录必须是全路径的 Args: host: IP username: 远程OS用户名