Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268603
  • 博文数量: 103
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-02 16:15
文章分类

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: 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

 

    Bootstrap 101 Template

   

   

   

 

  

 

   {{ form }}

   

-------------------------

{{ username }}

{{ ip }}

{{ cmd }}

-------------------------

---------------------------

{% for line in cmd_result %}

   

{{ line }}

 

{% endfor %}





阅读(1306) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~