堡垒机如何传输文件_mac 堡垒机传文件

安装zssh

brew install zssh

上传文件

zssh登陆上跳板机

在跳板机上ssh到相应服务器

在服务器上cd至相应要放上传文件的目录

rz -bye //在远程服务器的相应目录上运行此命令,表示做好接收文件的准备

ctrl+@ //运行上面命令后,会出现一些乱码字符,不要怕,按此组合键,进入zssh

zssh > //这里切换到了本地机器

zssh > pwd //看一下本地机器的目录在那

zssh > ls //看一下有那些文件

zssh > sz 123.txt //上传本地机器的当前目录的123.txt到远程机器的当前目录

下载文件

sz filename //在远程机器上,启动sz, 准备发送文件,看到一堆乱码,不要怕,这会按下组合键

cd 切换到合适的目录

zssh > rz //接住对应的文件

#!/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()

你可能感兴趣的:(堡垒机如何传输文件)