实现堡垒机模式下的远程文件上传

[root@node110 paramiko]# cat simple4.py 
#!/usr/bin/env python
import paramiko
import os,sys,time


hostname="192.168.2.121"
username="root"
password="123456"


blip="192.168.2.120"
bluser="root"
blpasswd="123456"


tmpdir="/tmp"
remotedir="/data"
localpath="/home/nginx_access.tar.gz"
tmppath=tmpdir+"/nginx_access.tar.gz"
remotepath=remotedir+"/nginx_access_hd.tar.gz"


port=22
passinfo='\'s password: '
paramiko.util.log_to_file('syslogin.log')


t = paramiko.Transport((blip, port))
t.connect(username=bluser, password=blpasswd)
sftp =paramiko.SFTPClient.from_transport(t)
sftp.put(localpath, tmppath)
sftp.close()


ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=blip,username=bluser,password=blpasswd)


#new session
channel=ssh.invoke_shell()
channel.settimeout(10)


buff = ''
resp = ''
channel.send('scp '+tmppath+' '+username+'@'+hostname+':'+remotepath+'\n')


while not buff.endswith(passinfo):
    try:
        resp = channel.recv(9999)
    except Exception,e:
        print 'Error info:%s connection time.' % (str(e))
        channel.close()
        ssh.close()
        sys.exit()
    buff += resp
    if not buff.find('yes/no')==-1:
        channel.send('yes\n')
buff=''


channel.send(password+'\n')


buff=''
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()




结果:
[root@node110 paramiko]# python simple4.py 


nginx_access.tar.gz                           100%  343KB 342.7KB/s   00:00   


堡垒机:
[root@node120 ~]# cd /tmp
[root@node120 tmp]# ls
nginx_access.tar.gz  node110.txt  node120.txt  paramiko1.py  pramiko1.py


业务主机:
[root@node121 data]# ls
3307  array.php        fdfs_storage     for.php       gettime.php  loop.php                null          sysinfo.php
3308  each.php         fdfs_tracker     foreach.php   index.php    mysql_data              operator.php  variables.php
3309  fdfs_client_log  flowcontrol.php  function.php  list.php     nginx_access_hd.tar.gz  rsyncd

分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS