简体   繁体   English

从php执行python脚本

[英]Executing python script from php

I have the following php code which is calling a python script nmapdb.py located in the same folder as the php file. 我有以下php代码,它调用与php文件位于同一文件夹中的python脚本nmapdb.py。

function banner($name) {

$output = exec('python nmapdb.py $name');
echo $output;

}

the python scipt is as follows: python scipt如下:

ip=sys.argv[1]
print("Please get cup of coffee...Coz I am gonna take some time...")
nm=nmap.PortScanner()
nm.scan(hosts=ip , arguments='-sV --script=banner')
result=nm.get_nmap_last_output()
fo=open("result.xml","w")
fo.write(result)
fo.close()

con = mdb.connect('localhost', 'testuser', 'test623', 'testdb');


tree=etree.parse('result.xml')
root=tree.getroot()
root.tag
entries=tree.findall('host')
i=0
while i < len(entries):
    sub=entries[i].find('address')
    adr=sub.attrib['addr']
    adrtype=sub.attrib['addrtype']
    sub=entries[i].find('ports')
    sub1=sub.findall('port')
    j=0
    while j < len(sub1):
        prot=sub1[j].attrib['protocol']
        prtid=sub1[j].attrib['portid']
        sub2=sub1[j].find('state')
        stat=sub2.attrib['state']
        sub2=sub1[j].find('service')
        servname=sub2.attrib['name']
        try:
           sub2.attrib['product']
        except KeyError:
           prod='unknown'
        else:
           prod=sub2.attrib['product']
        try:
           sub2.attrib['devicetype']
        except KeyError:
           devtype='unknown'
        else:
           devtype=sub2.attrib['devicetype']
        try:
           sub2.attrib['ostype']
        except KeyError:
           os='unknown'
        else:
           os=sub2.attrib['ostype']
        j=j+1
        comm= "http://api.hostip.info/get_json.php?ip="+adr+"&position=true"
        response = urllib.urlopen(comm).read()
        data=json.loads(response)
        country=data['country_name']
        city=data['city']


        with con:

                cur = con.cursor()
                cur.execute("""INSERT INTO nmap_table (Ip_Addr, Addr_Type, Protocol, Port_Id, State, Service_Name, Product, Device, OS, Country, City) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", (adr, adrtype, prot, prtid, stat, servname, prod, devtype, os, country, city))
        i=i+1
print("Kudos....I'm done....Please check the database...")

But the problem is that when i am calling the python script from php, it is not working properly...only the first print statement is coming in the browser....whereas the script is working all fine when executed from terminal...PLEASE HELP.... 但是问题是,当我从php调用python脚本时,它无法正常工作...只有第一个打印语句会在浏览器中出现....而从终端执行时,脚本可以正常工作.. 。请帮忙....

function banner($name) { 功能banner($ name){

$output = exec('python nmapdb.py $name', $full_output); $ output = exec('python nmapdb.py $ name',$ full_output); echo $output; echo $ output; // Here just last line of script print_r($full_output); //这只是脚本的最后一行print_r($ full_output); // Here will be all console output of python script } //这是python脚本的所有控制台输出}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM