def Runscript(request): response = {'status': True, 'message': 'None'} hosts_list = request.POST.get('hosts') jf_list = request.POST.get('jid') jfile = models.Tools.objects.filter(id=jf_list).all() # 脚本文件信息 for s in jfile: print(s.script) # 服务器信息 hosts = models.Server.objects.filter(id=hosts_list).all() for h in hosts: print(h.hostname, h.hostIP) #创建并执行脚本文件 from luban.plugins.test import SSHConnection try: obj=SSHConnection(hostname=h.hostname, hostip=h.hostIP, port=h.hostport, user=h.hostuser, pwd=h.hostpwd) cmd = "echo '%s' > /tmp/%s && sh /tmp/%s"%(s.script,s.name,s.name) ret = obj.commands(cmd) #response['message'] = ret.split('\n') response['message'] = str(ret) + '%s'%h.hostname return HttpResponse(json.dumps(response,ensure_ascii=False)) except Exception as e: response['status'] = False response['message'] = '%s执行失败!错误信息:%s' % (h.hostname, e) return HttpResponse(json.dumps(response,ensure_ascii=False))
def CmdShell(request): response = {'status': True, 'message': 'None'} cmd = request.POST.get('cmd') host_list = request.POST.get('hosts') print(host_list) hosts = models.Server.objects.filter(id=host_list).all() for host in hosts: print(host.hostIP) from luban.plugins.test import SSHConnection try: obj=SSHConnection(hostname=host.hostname, hostip=host.hostIP, port=host.hostport, user=host.hostuser, pwd=host.hostpwd) ret = obj.commands(cmd) response['message'] = ret.split('\n') return HttpResponse(json.dumps(response,ensure_ascii=False)) except Exception as e: response['status'] = False response['message'] = '%s执行失败!错误信息:%s' % (host.hostname, e) return HttpResponse(json.dumps(response,ensure_ascii=False)) result = json.dumps(response, ensure_ascii=False) return HttpResponse(result)
def CodesBackSqlJson(request): response = {'status':True,'message':None} print(request.POST) hid = request.POST.get('hid') obj = models.Server.objects.filter(id=hid).all() for i in obj: print(i.sqluser,i.sqlpwd) # backsql='--all-databases > /tmp/all.sql && echo $? && netstat -ntpl | grep 3306' # cmd='mysqldump -h%s -u%s -p%s %s'%(i.hostIP,i.sqluser,i.sqlpwd,backsql) cmd = 'netstat -ntpl | grep 3306' print(cmd) from luban.plugins.test import SSHConnection try: obj=SSHConnection(hostname=i.hostname, hostip=i.hostIP, port=i.hostport, user=i.hostuser, pwd=i.hostpwd) ret = obj.commands(cmd) print(ret) response['message']=str(ret) except Exception as e: response['status']=False response['message']=str(e) return HttpResponse(json.dumps(response,ensure_ascii=False))
def CodesJsonRestartNginx(request): response = {'status':True,'message':'None'} print(request.POST.get('hosts')) cmd = '/data/nginx/sbin/nginx -s reload&&netstat -ntpl | grep nginx ' #cmd = 'uname -a' #获取到服务器 hid_list = request.POST.get('hosts') hobj = models.Server.objects.filter(id=hid_list).all() for h in hobj: print(h.hostIP,h.hostuser,h.hostport) from luban.plugins.test import SSHConnection obj = SSHConnection(hostname=h.hostname, hostip=h.hostIP, port=h.hostport, user=h.hostuser, pwd=h.hostpwd) try: ret = obj.commands(cmd=cmd) print(ret) response['message'] = "主机[%s]"%h.hostname + ret result = json.dumps(response) return HttpResponse(result) except Exception as e: response['status']=False response['message']='主机[%s]:'%h.hostname + str(e) return HttpResponse(json.dumps(response,ensure_ascii=False))
def ServerCheckJson(request): response = {'status': False, 'message': 'None'} #获取前端发来的命令 cmd = request.POST.get('cmd') print(cmd) #获取选中的所有主机的id hosts = request.POST.get('hosts') hosts_list = models.Server.objects.filter(id=hosts).all() print(hosts_list) for host in hosts_list: print(host.hostIP) from luban.plugins.test import SSHConnection try: obj = SSHConnection(hostname=host.hostname, hostip=host.hostIP, port=host.hostport, user=host.hostuser, pwd=host.hostpwd) ret = obj.commands(cmd=cmd) response['status']=True response['message'] = '主机[%s]:状态正常 '%host.hostname return HttpResponse(json.dumps(response,ensure_ascii=False)) except Exception as e: response['message']='主机[%s]: %s'%(host.hostname,str(e)) return HttpResponse(json.dumps(response,ensure_ascii=False))
def Batchexecution(request): response = {'status': True, 'message': 'None'} try: # print(request.POST) # print(request.POST.get('hosts')) # print(request.POST.get('sfid')) host_list = request.POST.get('hosts') sfid_list = request.POST.get('sfid') from Deployment import settings filepath = settings.LOCAL_PATH file = models.ServType.objects.filter(id=sfid_list) for row in file: from Deployment import settings filename = row.softname print(filepath) print(filename) localfile = '%s/%s'%(filepath,filename) obj = models.Server.objects.filter(id=host_list).all() for i in obj: print(i.hostIP, i.hostport, i.hostuser, i.hostpwd) print('-----------------------------------') from luban.plugins.test import SSHConnection obj = SSHConnection(hostname=i.hostname, hostip=i.hostIP, port=i.hostport, user=i.hostuser, pwd=i.hostpwd) # 发送文件 remote_path = '/data/%s' % filename # print(remote_path) ret = obj.transfile(localfile, remote_path) response['message'] = ret # print(obj) result = json.dumps(response, ensure_ascii=False) return HttpResponse(result) except Exception as e: response['status'] = False response['message'] = '执行失败!错误信息:%s' % e # response['message']='%s执行失败!错误信息:%s'%(host,e) result = json.dumps(response, ensure_ascii=False) return HttpResponse(result)
def sendremoteserver(request): response = {'status': True, 'message': 'None'} from Deployment import settings try: hid = request.POST.get('remothid') filepath = settings.LOCAL_PATH filename = request.POST.get('softw') print(filename) print(filepath) localfile ='%s/%s'%(filepath,filename) print(localfile) sid = request.POST.get('sid') obj = models.Server.objects.filter(id=hid).all() for row in obj: host = row.hostname print(host) ip = row.hostIP user = row.hostuser pwd = row.hostpwd port = row.hostport from luban.plugins.test import SSHConnection obj = SSHConnection(hostname=host, hostip=ip, port=port, user=user, pwd=pwd) # 设置远程目录 # cmd = "'remote_data='/data'\nif [ -d $mysqldir ]then\necho '目录已经创建'\nelse\nmkdir $mysqldir;fi'" # obj.commands(cmd) remote_path = '/data/%s' % filename # print(remote_path) ret = obj.transfile(localfile, remote_path) response['message'] = ret result = json.dumps(response, ensure_ascii=False) return HttpResponse(result) except Exception as e: response['status'] = False response['message'] = '执行失败!错误信息:%s' % e # response['message']='%s执行失败!错误信息:%s'%(host,e) result = json.dumps(response, ensure_ascii=False) return HttpResponse(result)
def runserver(request): # print(request.POST) # print(request.POST.get('cmd')) response = {'status': True, 'message': 'None'} try: cmd = request.POST.get('cmd') hid = request.POST.get('server') sid = request.POST.get('software') print(cmd) print(hid) print(sid) file = models.ServType.objects.all() for item in file: print(item.software) obj = models.Server.objects.filter(id=hid).all() for row in obj: host = row.hostname ip = row.hostIP user = row.hostuser pwd = row.hostpwd port = row.hostport print(ip, user, pwd, port, cmd) # ret = commands(host=ip,port=port,user=user,pwd=pwd,cmd=cmd) # response['message']=ret # result = json.dumps(response) # return HttpResponse(result) from luban.plugins.test import SSHConnection obj = SSHConnection(hostname=host, hostip=ip, port=port, user=user, pwd=pwd) ret = obj.commands(cmd=cmd) response['message'] = ret result = json.dumps(response) return HttpResponse(result) except Exception as e: response['status'] = False response['message'] = '%s执行失败!错误信息:%s' % (host, e) result = json.dumps(response, ensure_ascii=False) return HttpResponse(result)
def CodesJsonRelease(request): response = {'status':True,'message':'None'} print(request.POST) hid_list = request.POST.get('hosts') hobj = models.Server.objects.filter(id=hid_list).all() for h in hobj: print(h.hostIP,h.hostuser,h.hostport) cid = request.POST.get('cid') cobj = models.CodesDB.objects.filter(id=cid).first() # filepath = str(cobj.codefile) # #linux # filename = str(cobj.codefile).split('/')[1] from Deployment import settings filepath = settings.LOCAL_CODE_PATH filename = cobj.codename from Deployment.settings import REMOTE_WEBROOT_PATH remote_path = REMOTE_WEBROOT_PATH+'%s'%filename print(remote_path) localpathname='%s/%s'%(filepath,filename) from luban.plugins.test import SSHConnection try: obj = SSHConnection(hostname=h.hostname, hostip=h.hostIP, port=h.hostport, user=h.hostuser, pwd=h.hostpwd) # 打包的命令 #cmd = 'cd /data/nginx/html/;touch `date+%Y%m%d`.txt; tar -czf `date +%Y-%m-%d_%H_%M_%S`.old.tar.gz * && mv * /tmp/' cmd = 'cd /data/nginx/html/&&tar -czf `date +%Y-%m-%d_%H_%M_%S`.old.tar.gz * ; mv * /tmp/' retcmd = obj.commands(cmd) ret = obj.transfile(localpathname, remote_path) # 解压缩的命令 cmd2 = 'cd /data/nginx/html/&&tar xf %s && rm -rf %s && /data/nginx/sbin/nginx -s reload'%(filename,filename) obj.commands(cmd2) response['message']=ret except Exception as e: response['status']=False response['message']= '主机[%s]'%h.hostname + str(e) #链接服务器 print(response) return HttpResponse(json.dumps(response,ensure_ascii=False))