堡垒机如何传输文件_mac 堡垒机传文件
安裝zssh
brew install zssh
上傳文件
zssh登陸上跳板機
在跳板機上ssh到相應(yīng)服務(wù)器
在服務(wù)器上cd至相應(yīng)要放上傳文件的目錄
rz -bye //在遠程服務(wù)器的相應(yīng)目錄上運行此命令,表示做好接收文件的準備
ctrl+@ //運行上面命令后,會出現(xiàn)一些亂碼字符,不要怕,按此組合鍵,進入zssh
zssh > //這里切換到了本地機器
zssh > pwd //看一下本地機器的目錄在那
zssh > ls //看一下有那些文件
zssh > sz 123.txt //上傳本地機器的當前目錄的123.txt到遠程機器的當前目錄
下載文件
sz filename //在遠程機器上,啟動sz, 準備發(fā)送文件,看到一堆亂碼,不要怕,這會按下組合鍵
cd 切換到合適的目錄
zssh > rz //接住對應(yīng)的文件
#!/usr/bin/env python
# coding=utf-8
from optparse import OptionParser
import paramiko
import os,sys,time
"""
這個腳本的作用是實現(xiàn)堡壘機模式下,文件上傳
"""
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()
總結(jié)
以上是生活随笔為你收集整理的堡垒机如何传输文件_mac 堡垒机传文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机本科毕业论文仓库管理,计算机本科毕
- 下一篇: Halcon每月授权license如何更