import os,sys,datetime
import paramiko
from optparse import OptionParser
def cpFileSingleIp(sfile,dfile,ip):
username = "root"
password = "123456"
port = 22
t=paramiko.Transport((ip,port))
t.connect(username=username,password=password)
sftp=paramiko.SFTPClient.from_transport(t)
print "Begin to upload file to %s" %ip
print datetime.datetime.now()
try:
sftp.put(sfile,dfile)
except:
s='----ERR\n'
err_info="\033[1;32;40m%s %s %s\033[0m"%(e,ip,s)
f=file('sftp_err.log','a+')
f.write(err_info)
f.close()
print err_info
print datetime.datetime.now()
t.close()
def a():
print al
parser = OptionParser()
parser.add_option("-s",
action="store",
dest="sfile",
type="string",
help="source file path,like:/var/www/html/file01",
metavar="FILE"
)
parser.add_option("-d",
action="store",
dest="dfile",
type="string",
help="destination directory,like:/var/www/html/file01",
metavar="FILE"
)
parser.add_option("--ip",
action="store",
dest="dip",
type="string",
help="destination ip which you want to transfer your file ",
)
parser.add_option("--all",
# action="store",
dest="allserver",
type="string",
help="transport the file to all servers,like:python trfile.py --all",
)
(options,args)=parser.parse_args()
if __name__ == "__main__":
if sys.argv[1] == "--all":
print "aa"
else:
sfile=options.sfile
dfile=options.dfile
ip=options.dip
cpFileSingleIp(sfile,dfile,ip)
阅读(1164) | 评论(0) | 转发(0) |