def DownFile(dfile, sfile, username, password, ip, port, su, supassword, sudo, sudopassword, loginmethod, keyfile, fid, user): socket.setdefaulttimeout(3) dfile = os.path.basename(dfile) dfile = os.path.join(download_dir, dfile) model = "transfile_downfile" info = {"status": True, "content": "", "status": "running", "progres": "0"} translog = [] lasttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) logline = { "fid": fid, "action": "download", "time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), "ip": ip, "sfile": sfile, "dfile": dfile, "result": False, "user": user, "msg": "", "size": "0KB", "lasttime": lasttime } try: t = paramiko.Transport((ip, int(port))) if loginmethod == "key".upper(): keyfile = key_resolv.key_resolv(keyfile, cache) key = paramiko.RSAKey.from_private_key_file(keyfile) t.connect(username=username, pkey=key) else: t.connect(username=username, password=password) callback_info = functools.partial(set_progres, fid, 1, 1, False) sftp = paramiko.SFTPClient.from_transport(t) try: sftp.listdir(sfile) cheungssh_sftp(fid, ip, username, sfile, dfile, set_progres, port, loginmethod, password, keyfile) return except Exception, e: if e.errno == 2: pass else: raise IOError(e) sftp.get(sfile, "%s.%s" % (dfile, ip), callback=callback_info) log(model, True) info['status'] = 'True' logline["result"] = True cache_size_id = "fid:size:%s" % (fid) cache_size = cache.get(cache_size_id) if cache_size is None: cache_size = 0 t_size = float(cache_size) / float(1024) logline['size'] = "%0.2fKB" % t_size cache_translog = cache.get("translog") if cache_translog: cache_translog.append(logline) else: translog.append(logline) cache_translog = translog cache.set("translog", cache_translog, 3600000000)
def UploadFile(dfile,sfile,username,password,ip,port,su,supassword,sudo,sudopassword,loginmethod,keyfile,fid,user): print "开始上传..........." socket.setdefaulttimeout(3) model="transfile_getfile_upload" info={"msgtype":"OK","content":"","status":"running","progres":"0"} translog=[] lasttime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) logline={"fid":fid,"action":"upload","time":time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()),"ip":ip,"sfile":sfile,"dfile":dfile,"result":"ERR","user":user,"msg":"","size":"0KB","lasttime":lasttime} try: t=paramiko.Transport((ip,int(port))) if loginmethod=="key".upper(): print "key登陆" keyfile=key_resolv.key_resolv(keyfile,cache) key=paramiko.RSAKey.from_private_key_file(keyfile) t.connect(username = username,pkey=key) else: print "密码登陆" t.connect(username = username,password = password) ######################测试################### if os.path.isdir(sfile): #(sdir,ddir,username,password,ip,loginmethod,keyfile,port=22,force=True,callback_info): print " [%s]" % password sync_dir.UploadFile(sfile,dfile,username,password,ip,loginmethod,keyfile,fid,set_progres,port,True) pass else: sftp = paramiko.SFTPClient.from_transport(t) if dfile.endswith('/'): try: sftp.listdir(dfile) except Exception,e: raise IOError("%s 目录不存在" %(dfile)) try: sftp.listdir(dfile) dfile=os.path.join(dfile,os.path.basename(sfile)) except Exception,e: pass print sfile,dfile callback_info = functools.partial(set_progres,fid,1,1,False) sftp.put(sfile,dfile,callback=callback_info) log(model,"OK") logline["result"]="OK" info["status"]="OK" info["msgtype"]="OK" cache_size_id="fid:size:%s" %(fid) cache_size=cache.get(cache_size_id) if cache_size is None: cache_size=0 t_size=float(cache_size)/float(1024) logline['size']="%0.2fKB" %t_size cache_translog=cache.get("translog") if cache_translog: print 888888888 cache_translog.append(logline) else: print 9999999999 translog.append(logline) cache_translog=translog cache.set("translog",cache_translog,3600000000)
def getconf(host,fid,user,action): model="getconf" try: if not type({})==type(host): host=eval(host) if not type(host)==type({}): log(model,"配置信息错误, 不是一个dict格式") return False except Exception,e: log(model,str(e)) print "有错误",e return False
def getconf(host, fid, user, action): model = "getconf" try: if not type({}) == type(host): host = eval(host) if not type(host) == type({}): log(model, "配置信息错误, 不是一个dict格式") return False except Exception, e: log(model, str(e)) print "有错误", e return False
def DownFile(dfile,sfile,username,password,ip,port,su,supassword,sudo,sudopassword,loginmethod,keyfile,fid,user): socket.setdefaulttimeout(3) dfile=os.path.basename(dfile) dfile=os.path.join(download_dir,dfile) model="transfile_downfile" info={"msgtype":"OK","content":"","status":"running","progres":"0"} translog=[] lasttime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) logline={"fid":fid,"action":"download","time":time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()),"ip":ip,"sfile":sfile,"dfile":dfile,"result":"ERR","user":user,"msg":"","size":"0KB","lasttime":lasttime} try: t=paramiko.Transport((ip,int(port))) if loginmethod=="key".upper(): keyfile=key_resolv.key_resolv(keyfile,cache) key=paramiko.RSAKey.from_private_key_file(keyfile) t.connect(username = username,pkey=key) else: t.connect(username = username,password = password) callback_info = functools.partial(set_progres,fid) sftp = paramiko.SFTPClient.from_transport(t) print dfile sftp.get(sfile,dfile,callback=callback_info) log(model,"OK") info['status']='OK' logline["result"]="OK" cache_size_id="fid:size:%s" %(fid) cache_size=cache.get(cache_size_id) if cache_size is None: cache_size=0 t_size=float(cache_size)/float(1024) logline['size']="%0.2fKB" %t_size cache_translog=cache.get("translog") if cache_translog: print 888888888 cache_translog.append(logline) else: print 9999999999 translog.append(logline) cache_translog=translog cache.set("translog",cache_translog,3600000000) except Exception,e: msg=str(e) print msg info["content"]=msg info["status"]='ERR' log(model,msg) cache.set("info:%s"%(fid),info,360000) logline["result"]="ERR" logline["msg"]=msg cache_translog=cache.get("translog") print "抓取到异常...",e logline["result"]=msg
def getconf(host, fid, user, action): model = "getconf" try: if not type({}) == type(host): host = eval(host) if not type(host) == type({}): log(model, "GXXCXXF0000000001") return False, "GXXCXXF0000000001" except Exception, e: log(model, str(e)) print 22222222222, e return False, "未知错误"
def getconf(host, fid, user, action): ########host 格式是一个字典 ########fid 一个随机生成的数字 ########请求的用户 , action download upload 上传下载 model = "getconf" try: if not type({}) == type(host): host = eval(host) if not type(host) == type({}): log(model, "GXXCXXF0000000001") ######已经转码,请求的格式不是一个字典 return False, "GXXCXXF0000000001" except Exception, e: log(model, str(e)) print 22222222222, e return False, "未知错误"
def resove_conf(conf,fid,user,action): print conf,99999999999999999999999999999999999999999999999999999999,"开始解析" #conf= {} model="transfile_getfile_resove_conf" info={"msgtype":"ERR","content":""} try: id=conf["id"] dfile=conf["dfile"] sfile=conf["sfile"] username=conf["username"] password=conf["password"] ip=conf["ip"] port=conf["port"] try: su=conf["su"] except KeyError: su='N' try: supassword=conf["sudopassword"] except KeyError: supassword="" try: sudo=conf["sudo"] except KeyError: sudo="N" try: sudopassword=conf["password"] except KeyError: sudopassword="" loginmethod=conf["loginmethod"] try: keyfile=conf["keyfile"] except KeyError: keyfile="" print "解析wanle ",5555555555555555555555555,conf except Exception,e: print "报错",e,11111111111111111111111111111111111111111111 msg=str(e) log(model,msg) info["content"]=msg cache.set("info:%s" % (fid),info,360000) return error_linenumber.get_linen_umber_function_name()[1]
def resove_conf(conf, fid, user, action): model = "transfile_getfile_resove_conf" info = {"status": False, "content": ""} try: id = conf["id"] dfile = conf["dfile"] sfile = conf["sfile"] username = conf["username"] password = conf["password"] ip = conf["ip"] port = conf["port"] try: su = conf["su"] except KeyError: su = 'N' try: supassword = conf["sudopassword"] except KeyError: supassword = "" try: sudo = conf["sudo"] except KeyError: sudo = "N" try: sudopassword = conf["password"] except KeyError: sudopassword = "" loginmethod = conf["loginmethod"] try: keyfile = conf["keyfile"] except KeyError: keyfile = "" except Exception, e: msg = str(e) log(model, msg) info["content"] = msg cache.set("info:%s" % (fid), info, 360000) ###########return error_linenumber.get_linen_umber_function_name()[1] return False
logline['size']="%0.2fKB" %t_size cache_translog=cache.get("translog") if cache_translog: print 888888888 cache_translog.append(logline) else: print 9999999999 translog.append(logline) cache_translog=translog cache.set("translog",cache_translog,3600000000) except Exception,e: msg=str(e) print msg info["content"]=msg info["status"]='ERR' log(model,msg) cache.set("info:%s"%(fid),info,360000) logline["result"]="ERR" logline["msg"]=msg cache_translog=cache.get("translog") print "抓取到异常...",e logline["result"]=msg def UploadFile(dfile,sfile,username,password,ip,port,su,supassword,sudo,sudopassword,loginmethod,keyfile,fid,user): print "开始上传..........." socket.setdefaulttimeout(3) model="transfile_getfile_upload"
def UploadFile(dfile, sfile, username, password, ip, port, su, supassword, sudo, sudopassword, loginmethod, keyfile, fid, user): print "开始上传..........." socket.setdefaulttimeout(3) model = "transfile_getfile_upload" info = { "msgtype": "OK", "content": "", "status": "running", "progres": "0" } translog = [] lasttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) logline = { "fid": fid, "action": "upload", "time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), "ip": ip, "sfile": sfile, "dfile": dfile, "result": "ERR", "user": user, "msg": "", "size": "0KB", "lasttime": lasttime } try: t = paramiko.Transport((ip, int(port))) if loginmethod == "key".upper(): print "key登陆" keyfile = key_resolv.key_resolv(keyfile, cache) key = paramiko.RSAKey.from_private_key_file(keyfile) t.connect(username=username, pkey=key) else: print "密码登陆" t.connect(username=username, password=password) if os.path.isdir(sfile): #(sdir,ddir,username,password,ip,loginmethod,keyfile,port=22,force=True,callback_info): print " [%s]" % password sync_dir.UploadFile(sfile, dfile, username, password, ip, loginmethod, keyfile, fid, set_progres, port, True) pass else: sftp = paramiko.SFTPClient.from_transport(t) if dfile.endswith('/'): try: sftp.listdir(dfile) except Exception, e: raise IOError("%s 目录不存在" % (dfile)) try: sftp.listdir(dfile) dfile = os.path.join(dfile, os.path.basename(sfile)) except Exception, e: pass print sfile, dfile callback_info = functools.partial(set_progres, fid, 1, 1, False) sftp.put(sfile, dfile, callback=callback_info) log(model, "OK") logline["result"] = "OK" info["status"] = "OK" info["msgtype"] = "OK" cache_size_id = "fid:size:%s" % (fid) cache_size = cache.get(cache_size_id) if cache_size is None: cache_size = 0 t_size = float(cache_size) / float(1024) logline['size'] = "%0.2fKB" % t_size cache_translog = cache.get("translog") if cache_translog: print 888888888 cache_translog.append(logline) else: print 9999999999 translog.append(logline) cache_translog = translog cache.set("translog", cache_translog, 3600000000)
logline['size'] = "%0.2fKB" % t_size cache_translog = cache.get("translog") if cache_translog: print 888888888 cache_translog.append(logline) else: print 9999999999 translog.append(logline) cache_translog = translog cache.set("translog", cache_translog, 3600000000) except Exception, e: msg = str(e) print msg info["content"] = msg info["status"] = 'ERR' log(model, msg) cache.set("info:%s" % (fid), info, 360000) logline["result"] = "ERR" logline["msg"] = msg cache_translog = cache.get("translog") print "抓取到异常...", e logline["result"] = msg def UploadFile(dfile, sfile, username, password, ip, port, su, supassword, sudo, sudopassword, loginmethod, keyfile, fid, user): print "开始上传..........." socket.setdefaulttimeout(3) model = "transfile_getfile_upload" info = { "msgtype": "OK",
#!/usr/bin/env python #coding:utf-8 import os, sys, random from cheunglog import log model = "daemon-transfile" sys.path.append('/home/cheungssh/mysite') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") from mysite.cheungssh.FileTransfer import getconf if __name__ == '__main__': host = " ".join(sys.argv[1:]) try: host = eval(host) if not type({}) == type(host): log(model, "参数类型不是dict") sys.exit(1) else: log(model, "started") if host.has_key('fid'): fid = host['fid'] else: fid = str( random.randint(90000000000000000000, 99999999999999999999)) try: getconf(host, fid, host["user"], host["runtype"]) except Exception, e: log(model, str(e)) except Exception, e: print e
#!/usr/bin/env python #coding:utf-8 import os,sys,random from cheunglog import log model="daemon-transfile" sys.path.append('/home/cheungssh/mysite') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") from mysite.cheungssh.FileTransfer import getconf if __name__ == '__main__': #'{"sfile":"/tmp/abc","dfile":"/tmp/cbd","id":"1","user":"******","runtype":"upload","fid":111111111111111111}' host = " ".join(sys.argv[1:]) try: host=eval(host) if not type({})==type(host): log(model,"参数类型不是dict") sys.exit(1) else: log(model,"started") if host.has_key('fid'): fid=host['fid'] else: fid=str(random.randint(90000000000000000000,99999999999999999999)) try: getconf(host,fid,host["user"],host["runtype"]) except Exception,e: log(model,str(e)) except Exception,e: print e msg=str(e) log(model,msg)
t.connect(username = username,password = password) callback_info = functools.partial(set_progres,fid) sftp = paramiko.SFTPClient.from_transport(t) if dfile.endswith('/'): try: sftp.listdir(dfile) except Exception,e: raise IOError("%s 目录不存在" %(dfile)) try: sftp.listdir(dfile) dfile=os.path.join(dfile,os.path.basename(sfile)) except Exception,e: pass print sfile,dfile sftp.put(sfile,dfile,callback=callback_info) log(model,"OK") logline["result"]="OK" info["status"]="OK" info["msgtype"]="OK" cache_size_id="fid:size:%s" %(fid) cache_size=cache.get(cache_size_id) if cache_size is None: cache_size=0 t_size=float(cache_size)/float(1024) logline['size']="%0.2fKB" %t_size cache_translog=cache.get("translog") if cache_translog: print 888888888 cache_translog.append(logline) else: print 9999999999