def collect(request): response = dict() if request.method == 'POST': ip = request.POST.get('ip') if not validIPV4(ip): messages.add_message(request, messages.ERROR, u'IP地址不合法或格式不正确') return HttpResponseRedirect(reverse('collect')) for i in ['ipv4', 'num_cpus', 'mem_total', 'oscodename']: cmd = 'salt --out=json {0} grains.item {1}'.format(ip, i) try: p = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() response.update(json.loads(stdout)[ip]) except Exception as e: break messages.add_message(request, messages.ERROR, e) messages.add_message(request, messages.ERROR, stderr) return HttpResponseRedirect(reverse('collect')) cmd = '/usr/bin/lsblk -d' p = Popen(shlex.split(cmd), stdout=PIPE) stdout = p.communicate()[0] response.update({'disk': stdout}) return JsonResponse(response) return render(request, 'salts/collect.html')
def validate_other_ip(self, value): if value: if not validIPV4(value): raise serializers.ValidationError(u'IP地址不合法') return value
def customValidIP(self, ip): if ip: if not validIPV4(ip): raise forms.ValidationError(u'IP地址不合法') return ip