刚工作的时候写的一个代码, 现在回头看,实在不忍直视, po出来试试这个文章
#!/usr/bin/env python
#sftp auto upload
#author:zhiguo
import paramiko,datetime,os,time
import socks
import socket
hostname='$ip'
username='yt-hunantv2'
#password='abc123'
port=19321
local_dir='/root/'
remote_dir='/'
t_id = str(time.time())
#最简单粗暴最sb的方式写日志
def loggs(content):
files = '/tmp/'+hostname+'.log'
f = open(files,'a')
f.write(content)
f.close()
try:
#socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, 'proxy_ip', 3306, True)
#t=paramiko.Transport((hostname,port))
#socket.socket = socks.socksocket
t=paramiko.Transport((hostname,port))
#t.connect(username=username,password=password)
#k = paramiko.RSAKey.from_private_key_file(filename=key_file)
private_key = paramiko.RSAKey.from_private_key_file("/root/.ssh/id_rsa")
#t.connect(username=username,password='', pkey=private_key)
t.connect(username=username,pkey=private_key)
sftp=paramiko.SFTPClient.from_transport(t)
#files=sftp.listdir(dir_path)
#files=sftp.listdir(remote_dir)
#for f in files:
f="hunantv.ts"
print ''
print '#########################################'
print 'Beginning to upload file from %s %s ' % (hostname,datetime.datetime.now())
print 'Downloading file:',os.path.join(remote_dir,f)
loggs('#########################################'+'\n')
a = 'tran id'+ t_id + ' : '+ 'Beginning to Uploading file from %s %s \n'%(hostname,datetime.datetime.now())
b = 'tran id'+ t_id + ' : '+ 'Uploading file:'+os.path.join(remote_dir,f)+'file size is 1001M'+'\n'
loggs(str(a))
loggs(str(b))
#sftp.get(os.path.join(remote_dir,f),os.path.join(local_dir,f))
fs='abc'
sftp.put(os.path.join(local_dir,f),os.path.join(remote_dir,f))
print 'Uploading file success %s ' % datetime.datetime.now()
print ''
print '##########################################'
a = 'tran id'+ t_id + ' : '+ 'Uploading file success %s \n' % datetime.datetime.now()
b = '########################################## \n'
loggs(str(a))
loggs(str(b))
t.close()
except Exception, e:
print "connect error!"
print e