|
| 1 | +#!/usr/bin/python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +#Author: wanger |
| 4 | +#Email: cxf210210@163.com |
| 5 | +#File:shell.py |
| 6 | +#CreateTime:2020/6/15 16:10 |
| 7 | +# __Software__: PyCharm |
| 8 | +from django.http import HttpResponse, JsonResponse |
| 9 | +import json, re,paramiko |
| 10 | +from django.core.cache import cache |
| 11 | + |
| 12 | +class shell: |
| 13 | + def __init__(self,content): |
| 14 | + self.user='root' |
| 15 | + self.password='qwe@123..asd' |
| 16 | + self.host_port='22' |
| 17 | + self.content=content |
| 18 | + |
| 19 | + def ssh(self,exec_comm): |
| 20 | + self.ip = re.search('\[(.*?)\]', self.content).group(1) |
| 21 | + ssh = paramiko.SSHClient() |
| 22 | + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 23 | + ssh.connect(self.ip, self.host_port, self.user, self.password) |
| 24 | + stdin, stdout, stderr = ssh.exec_command(exec_comm) |
| 25 | + out = stdout.readlines() |
| 26 | + err = stderr.readlines() |
| 27 | + ssh.close() |
| 28 | + return out, err |
| 29 | + |
| 30 | + return out, err |
| 31 | + def getmem(self): |
| 32 | + exec_comm='free -m' |
| 33 | + result=self.ssh(exec_comm) |
| 34 | + print(result) |
| 35 | + return JsonResponse( |
| 36 | + {"msgtype": "text", |
| 37 | + "text": { |
| 38 | + "content": result |
| 39 | + } |
| 40 | + } |
| 41 | + ) |
| 42 | + |
| 43 | + def getdisk(self): |
| 44 | + exec_comm='df -h' |
| 45 | + result=self.ssh(exec_comm) |
| 46 | + print(result) |
| 47 | + return JsonResponse( |
| 48 | + {"msgtype": "text", |
| 49 | + "text": { |
| 50 | + "content": result |
| 51 | + } |
| 52 | + } |
| 53 | + ) |
| 54 | + def getport(self): |
| 55 | + port = re.search('\[(.*?)\]\[(.*?)\]', self.content).group(2) |
| 56 | + exec_comm = "netstat -ntlp |egrep -w %s" % port |
| 57 | + result = self.ssh(exec_comm) |
| 58 | + print(result) |
| 59 | + return JsonResponse( |
| 60 | + {"msgtype": "text", |
| 61 | + "text": { |
| 62 | + "content": result |
| 63 | + } |
| 64 | + } |
| 65 | + ) |
| 66 | + def getlog(self): |
| 67 | + logpath=re.search('\[(.*?)\]\[(.*?)\]', self.content).group(2) |
| 68 | + exec_comm='tail -n 20 %s' %logpath |
| 69 | + result = self.ssh(exec_comm) |
| 70 | + print(result) |
| 71 | + return JsonResponse( |
| 72 | + {"msgtype": "text", |
| 73 | + "text": { |
| 74 | + "content": result |
| 75 | + } |
| 76 | + } |
| 77 | + ) |
| 78 | + |
| 79 | + def getservice(self): |
| 80 | + domain=re.search('\[(.*?)\]\[(.*?)\]', self.content).group(2) |
| 81 | + exec_comm='systemctl status %s'%domain |
| 82 | + result = self.ssh(exec_comm) |
| 83 | + print(result) |
| 84 | + return JsonResponse( |
| 85 | + {"msgtype": "text", |
| 86 | + "text": { |
| 87 | + "content": result |
| 88 | + } |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + def getload(self): |
| 93 | + exec_comm = 'uptime' |
| 94 | + result = self.ssh(exec_comm) |
| 95 | + print(result) |
| 96 | + return JsonResponse( |
| 97 | + {"msgtype": "text", |
| 98 | + "text": { |
| 99 | + "content": result |
| 100 | + } |
| 101 | + } |
| 102 | + ) |
| 103 | + def restart(self): |
| 104 | + service=re.search('\[(.*?)\]\[(.*?)\]', self.content).group(2) |
| 105 | + exec_comm='systemctl restart %s'%service |
| 106 | + result = self.ssh(exec_comm) |
| 107 | + #return result |
| 108 | + return JsonResponse( |
| 109 | + {"msgtype": "text", |
| 110 | + "text": { |
| 111 | + "content": '重启结果'+str(result) |
| 112 | + } |
| 113 | + } |
| 114 | + ) |
| 115 | +def shellres(content): |
| 116 | + shells=shell(content) |
| 117 | + shellreflect={'内存信息':'getmem','磁盘信息':'getdisk','日志信息':'getlog','服务检测':'getservice','端口检测':'getport','负载信息':'getload','重启服务':'restart'} |
| 118 | + for i in shellreflect.keys(): |
| 119 | + |
| 120 | + if '重启服务' in content: |
| 121 | + |
| 122 | + split=content.split('|') |
| 123 | + print(split) |
| 124 | + if len(split)==2 and split[1]=='是': |
| 125 | + cache.delete('name') |
| 126 | + cache.delete('data') |
| 127 | + #del split |
| 128 | + return shells.restart() |
| 129 | + if len(split)==2 and split[1]=='否': |
| 130 | + cache.delete('name') |
| 131 | + cache.delete('data') |
| 132 | + #del split |
| 133 | + return JsonResponse( |
| 134 | + {"msgtype": "text", |
| 135 | + "text": { |
| 136 | + "content": '取消重启服务操作' |
| 137 | + } |
| 138 | + } |
| 139 | + ) |
| 140 | + else : |
| 141 | + cache.set('data',content) |
| 142 | + print(cache.get('data')) |
| 143 | + return JsonResponse( |
| 144 | + {"msgtype": "text", |
| 145 | + "text": { |
| 146 | + "content": '确定要重启服务吗?(是/否)' |
| 147 | + } |
| 148 | + } |
| 149 | + ) |
| 150 | + |
| 151 | + if i in content: |
| 152 | + rest = getattr(shells, shellreflect[i]) |
| 153 | + return rest() |
| 154 | + else: |
| 155 | + continue |
| 156 | + |
| 157 | + |
| 158 | + return JsonResponse( |
| 159 | + {"msgtype": "text", |
| 160 | + "text": { |
| 161 | + "content": "您输入的格式有错误,请重新输入" |
| 162 | + } |
| 163 | + } |
| 164 | + ) |
| 165 | + |
| 166 | + |
| 167 | + |
0 commit comments