Python远程获取MD5校验码并在web上显示

一、编写python脚本,远程执行命令获取MD5校验码,脚本放到CGI路径下

1#!/usr/bin/env python 2 3#-*- coding: utf-8 -*- 4 5 6""" 7 8This runs a command on a remote host using SSH. At the prompts enter hostname, 9 10user, password and the command. 11 12""" 13 14 15print "Content-type:text/html" 16 17print 18 19print '<html>' 20 21print '<head>' 22 23print '<title>Hello</title>' 24 25print '</head>' 26 27print '<body>' 28 29 30import pexpect 31 32import getpass, os 33 34import traceback 35 36#user: ssh 主机的用户名 37 38#host:ssh 主机的域名 39 40#password:ssh 主机的密码 41 42#command:即将在远端 ssh 主机上运行的命令 43 44def ssh_command (user, host, password, command): 45 46 """ 47 48 This runs a command on the remote host. This could also be done with the 49 50 pxssh class, but this demonstrates what that class does at a simpler level. 51 52 This returns a pexpect.spawn object. This handles the case when you try to 53 54 connect to a new host and ssh asks you if you want to accept the public key 55 56 fingerprint and continue connecting. 57 58 """ 59 60 ssh_newkey = 'Are you sure you want to continue connecting' 61 62 # 为 ssh 命令生成一个 spawn 类的子程序对象. 63 64 child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command)) 65 66 i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: ']) 67 68 # 如果登录超时,打印出错信息,并退出. 69 70 if i == 0: # Timeout 71 72 print 'ERROR!' 73 74 print 'SSH could not login. Here is what SSH said:' 75 76 print child.before, child.after 77 78 return None 79 80 # 如果 ssh 没有 public key,接受它. 81 82 if i == 1: # SSH does not have the public key. Just accept it. 83 84 child.sendline ('yes') 85 86 child.expect ('password: ') 87 88 i = child.expect([pexpect.TIMEOUT, 'password: ']) 89 90 if i == 0: # Timeout 91 92 print 'ERROR!' 93 94 print 'SSH could not login. Here is what SSH said:' 95 96 print child.before, child.after 97 98 return None 99 100 # 输入密码. 101 102 child.sendline(password) 103 104 return child 105 106 107def main (): 108 109 # 获得用户指定 ssh 主机域名. 110 111 #host = raw_input('Hostname: ') 112 113 host = "192.168.1.100" 114 115 # 获得用户指定 ssh 主机用户名. 116 117 #user = raw_input('User: ') 118 119 user = "centos" 120 121 # 获得用户指定 ssh 主机密码. 122 123 #password = getpass.getpass() 124 125 password = "************" 126 127 # 获得用户指定 ssh 主机上即将运行的命令. 128 129 #command = raw_input('Enter the command: ') 130 131 command = "md5sum /usr/local/tomcat8.0.15/webapps/ROOT/caijinquanInHouse/caijinquan.plist | awk '{print\$1}'" 132 133 child = ssh_command (user, host, password, command) 134 135 # 匹配 pexpect.EOF 136 137 child.expect(pexpect.EOF) 138 139 # 输出命令结果. 140 141 print child.before 142 143 144if __name__ == '__main__': 145 146 try: 147 148 main() 149 150 except Exception, e: 151 152 print str(e) 153 154 traceback.print_exc() 155 156 os._exit(1) 157 158 159print '</body>' 160 161print '</html>'

二、配置Apache调用python脚本

1开启CGI功能

2配置CGI路径,设置用户名验证方式

密码文件用Apache自带命令htpaddwd生成

给userA创建密码认证

htpaddwd /var/www/html/loganalyzer/passwd/.passwd userA

3报错解决

查看日志显示权限不足

chown apache.apache ssh.py

修改后成功访问

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Python远程获取MD5校验码并在web上显示 - HelloWorld