分类: Python/Ruby
2013-09-16 18:02:21
vim views.py
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django import forms
from sshCMD.models import SerInfo
import paramiko
import os,sys
import re
class SerForm(forms.Form):
username = forms.CharField()
ip = forms.CharField()
cmd = forms.CharField()
def index(request):
return render_to_response('index.html',)
def list_ip(req):
if req.method == "POST":
form = SerForm(req.POST)
if form.is_valid():
username = form.cleaned_data['username']
host_ip = form.cleaned_data['ip']
cmd = form.cleaned_data['cmd']
password = SerInfo.objects.filter(ip=host_ip,user=username).get(user=username).password
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
key = 1
if key == 1:
s.connect(host_ip,22,username,password,timeout=5)
stdin,stdout,stderr = s.exec_command(cmd)
cmd_result = stdout.read(),stderr.read()
#
cmd_output = cmd_result[0].split('\n')
s.close()
return render_to_response('server.html',{
'username':username,
'ip':host_ip,
'cmd':cmd,
'cmd_result':cmd_output,
}
)
else:
form =SerForm()
return render_to_response('server.html',{'form':form,})
vim
sshCMD/Templates/server.html
{{
form }}
-------------------------
{{ username }}
{{ ip }}
{{ cmd }}
-------------------------
---------------------------
{% for line in cmd_result %}
{% endfor %}
{{ line }}