def script_init(sid,sfile,username): sfile=sfile.encode("utf-8") cheungssh_info={"content":"","status":False} tid=str(random.randint(90000000000000000000,99999999999999999999)) try: sfile=os.path.join(cheungssh_settings.script_dir,username,os.path.basename(sfile)) dfile=os.path.join('/tmp/',tid) host=cheungssh_modul_controler.CheungSSHControler.convert_id_to_ip(sid) if not host["status"]:raise CheungSSHError(host['content']) _host_info=host['content'] sftp=CheungSSHFileTransfer() login=sftp.login(**_host_info) if not login["status"]:raise CheungSSHError(login["content"]) _tmp_data={"status":True,"content":"","progress":"0","tid":tid} sftp.write_progress(_tmp_data) t=threading.Thread(target=sftp.upload,args=(sfile,dfile,tid)) t.start() cheungssh_info["status"]=True cheungssh_info["tid"]=tid cheungssh_info["dfile"]=dfile except Exception,e: cheungssh_info["status"]=False cheungssh_info["content"]=str(e)
def remote_file_content(super, username, id, action, file_content=""): #####username是那个用户请求 cheungssh_info = {"status": False, "content": ""} try: data = RemoteFileAdmin.get_remote_file_list(super, username) if not data["status"]: raise CheungSSHError(data["content"]) content = data["content"] try: ######检查权限 if not content[id]["owner"] == username: CheungSSHError("您无权查看该资源!") except KeyError: raise CheungSSHError("您指定的资源不存在!") path = content[id]["path"] path = re.sub(" ", "", path) #####空格问题 sid = content[id]["server"] ######sid host_info = CheungSSHControler.convert_id_to_ip(sid) if not host_info["status"]: raise CheungSSHError(host_info["content"]) host = host_info["content"] sftp = CheungSSHFileTransfer() login = sftp.login(**host) if not login["status"]: raise CheungSSHError(login["content"]) if action == "GET": cheungssh_info = sftp.get_filecontent(path) elif action == "WRITE": cheungssh_info = sftp.write_filecontent(path, file_content) else: raise CheungSSHError("CHB0000000024") except Exception, e: cheungssh_info["status"] = False cheungssh_info["content"] = str(e)
def get_logfile_date(logfile, realname, _type): # 获取整个日志文件的天日,格式为:日/月/年 cheungssh_info = {"content": "", "status": False} # 存放所有日期 date = [] try: #####判断是否是远程日志,如果是,则需要提前下载 ##### try: os.makedirs(os.path.dirname(realname)) except: pass if _type == "remote": tfile = realname sid = msgpack.unpackb( REDIS.hget("CHB-0383740494845", tfile.split("/")[-1]))["sid"] #####获取服务器sid conf = CheungSSHControler.convert_id_to_ip(sid) if not conf["status"]: raise IOError(conf["content"]) conf = conf["content"] ssh = CheungSSHFileTransfer() sftp = ssh.login(**conf) if not sftp["status"]: raise IOError(sftp["content"]) t = ssh.download(remote_file=logfile, local_file=realname, tid="") shutil.move( os.path.join(cheungssh_settings.download_dir, realname.split("/")[-1]), realname) if not t["status"]: raise IOError(t["content"]) else: tfile = logfile f = open(tfile) for line in f: try: _date = re.search( "\[([0-9]{1,2}/[A-Z][a-z]{2}/20[0-9]{2}):[0-9]{2}:[0-9]{2}:[0-9]{2}", line).group(1) # 判断是否重复过 if not _date in date: date.append(_date) except: pass data = { "date": date, "type": CheungSSHAnalyLog().logtype, "realname": realname } cheungssh_info = {"content": data, "status": True} except Exception, e: cheungssh_info = {"content": str(e), "status": False}
def deployment_script_init_and_execute(sid,sfile,username): cheungssh_info={"content":"","status":False} tid=str(random.randint(90000000000000000000,99999999999999999999)) try: sfile=os.path.join(cheungssh_settings.script_dir,username,os.path.basename(sfile)) dfile=os.path.join('/tmp/',tid) host=cheungssh_modul_controler.CheungSSHControler.convert_id_to_ip(sid) if not host["status"]:raise CheungSSHError(host['content']) _host_info=host['content'] sftp=CheungSSHFileTransfer() login=sftp.login(**_host_info) print 676767 if not login["status"]: print login["content"],"登录失败" raise CheungSSHError(login["content"]) cheungssh_info=sftp.upload(sfile,dfile,"") sftp.logout() if not cheungssh_info["status"]:raise CheungSSHError(cheungssh_info["content"]) cheungssh_info["dfile"]=dfile ssh=CheungSSH_SSH() login=ssh.login(**_host_info) if not login["status"]:raise CheungSSHError(login["content"]) cheungssh_info=ssh.execute(dfile,ignore=True) ssh.logout() except Exception,e: cheungssh_info["status"]=False cheungssh_info["content"]=str(e)
def __init__(self, stepid): self.stepid = stepid CheungSSHFileTransfer.__init__(self)