1

我试图在 OpenWrt 盒子上运行 python 脚本:

#!/root/system/usr/bin/python
import subprocess

p = subprocess.Popen([r"snmpget","-v","1","-c","public","-Oqv","-Ln", "192.168.1.1","1.3.6.1.2.1.2.2.1.10.7"], stdout=subprocess.PIPE).communicate()[0]
data = [r"curl","-d","iface_id=1&content="+ str(p).rstrip() ,"http://192.168.1.5:8080/stat/add_istat/"]
a = subprocess.Popen(data, stdout=subprocess.PIPE).communicate()[0]

它通过 snmp 获取数据,然后通过 curl 将数据发布到本地服务器。从外壳可以正常工作:

root@OpenWrt:~/python# ./w.py 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    34    0     6    0    28     31    146 --:--:-- --:--:-- --:--:--     0

我可以在数据库中看到数据。但来自 cron:

0-55/5 * * * * /root/python/w.py

我在 logread 中看到:

Dec 20 23:30:01 OpenWrt cron.err crond[1039]: USER root pid 16141 cmd /root/python/w.py

但是 DB 中没有数据 :( 并且 httpd access.log 中没有任何数据 :( 为什么?

4

2 回答 2

1

难道是snmpget或curl不在cron的路径中?

于 2010-12-20T22:45:12.720 回答
0

我用 python urllib 请求替换 curl,现在它可以工作了!:)

url = 'http://192.168.1.5:8080/stat/add_istat/"' # write ur URL here

values = {'iface_id' : '1', #write ur specific key/value pair
          'content' : str(p).rstrip(),
         }
try:
   data = urllib.urlencode(values)
   req = urllib2.Request(url, data)
   response = urllib2.urlopen(req)
   the_page = response.read()
   print the_page
except Exception, detail:
   print "Err ", detail
于 2010-12-21T08:48:33.470 回答