• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

jwang106


脚踏实地,日拱一卒。 建立新的神经链接,可不像公园散步那样简单。
  • 博客园
  • 联系
  • 管理

View Post

mac 堡垒机传文件

用python脚本传 原文: https://blog.51cto.com/wutengfei/2176738

#!/usr/bin/env python
# coding=utf-8

from optparse import OptionParser
import paramiko
import os,sys,time

"""
这个脚本的作用是实现堡垒机模式下,文件上传
"""

parser = OptionParser()
parser.add_option('-j', '--jumperuser', dest='jumperuser', help='Company jumper machine account like wutengfei, ..')
parser.add_option('-u', '--username', dest='username', help='Target machine account like wutengfei, ..')
parser.add_option('-p', '--port', dest='port', help='Target machine port')
parser.add_option('-m', '--hostname', dest='hostname', help='Target machine ip address like 192.168.246.168')
parser.add_option('-l', '--localpath', dest='localpath', help="Client local file path like '/Users/test.py'")
parser.add_option('-d', '--destpath', dest='destpath', help="Jumper server file path like '/tmp/test.py'")
parser.add_option('-t', '--targetpath', dest='targetpath', help="remote server file path like '/tmp/test.py'")
(opts,args) = parser.parse_args()

#定义跳板机信息
jumpername = "jumper.shuju.com" # 跳板机ip/域名
jumperport = 22 # 跳板机ssh端口
paramiko.util.log_to_file('syslogin.log')

class JumperInfo(object):
    """
    将文件从客户端上传至跳板机
    """
    def __init__(self,username,localpath,destpath):
        self.username = str(username)
        self.localpath = str(localpath)
        self.destpath = str(destpath)

    def jumper_ftp(self,jumperuser,localpath,destpath):
        private_key = os.path.expandvars('$HOME/.ssh/id_rsa')
        private_key = paramiko.RSAKey.from_private_key_file(private_key)
        t = paramiko.Transport(('jumper.shuju.com', 22))
        t.connect(username=jumperuser, pkey=private_key)
        sftp = paramiko.SFTPClient.from_transport(t)
        sftp.put(localpath,destpath)
        sftp.close()

passinfo='\'s password: '

class Jumper_put(JumperInfo):
    """
    将跳板机上的文件上传至目标机
    """

    def __init__(self,hostname,username,port,targetpath):
        self.hostname = str(hostname)
        self.username = str(username)
        self.port = str(port)
        self.targetpath = str(targetpath)

    def jumper_scp(self,jumperuser,destpath,username,hostname,targetpath,port):
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        privatekey = os.path.expandvars('$HOME/.ssh/id_rsa')
        key = paramiko.RSAKey.from_private_key_file(privatekey)
        ssh.connect(hostname='jumper.shuju.com', username=jumperuser, port=22, pkey=key)
        channel = ssh.invoke_shell()
        channel.settimeout(10)
        buff = ''
        resp = ''
        channel.send('scp ' + ' ' + '-P' + ' ' + port + ' ' + destpath + ' ' + username + '@' + hostname + ':' + targetpath + '\n')

        while not buff.endswith('$ '):
            resp = channel.recv(9999)
            if not resp.find(passinfo)==-1:
                print 'Error info: Authentication failed.'
                channel.close()
                ssh.close()
                sys.exit()
            buff += resp
        print buff
        channel.close()
        ssh.close()

def main():
    jumper_ssh = JumperInfo(username=opts.jumperuser,localpath=opts.localpath,destpath=opts.destpath)
    jumper_ssh.jumper_ftp(opts.jumperuser,opts.localpath,opts.destpath)
    target_ssh = Jumper_put(hostname=opts.hostname,username=opts.username,port=opts.port,targetpath=opts.targetpath)
    target_ssh.jumper_scp(opts.jumperuser,opts.destpath,opts.username,opts.hostname,opts.targetpath,opts.port)

if __name__ == '__main__':
    if opts.jumperuser == None or opts.username == None or opts.hostname == None or opts.localpath == None or opts.destpath == None or opts.targetpath == None or opts.port == None:
        parser.print_help()
        exit(-1)

main()

posted on 2019-05-17 16:59  jwang106  阅读(916)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3