我知道这是一个冗长的问题。指导方针说要提出一个可以回答的问题,尽管我意识到如果我们不小心的话,这可能会演变成讨论。因此,一般方法和循环定义可能足以回答这个问题......尽管如果有代码我不会提示。
意识到 zpool status 的输出确实很复杂...我知道它在 PHP 和 VB 中是可行的,我该如何在 python 中真正实现它?有一个问题,foreach 循环似乎不适用于从 linux 命令行返回的字符串。
信息: 操作系统:CentOS Python:2.6.6..尝试升级到2.7 MySQL 数据库
数据库关系: 很复杂并且有些专有,但我可以这样说:池、Vdev、子 Vdev 和硬盘驱动器的 4 个表都是相关的。
当前的 Python,不太好
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
import sys
import os
import subprocess
try:
def get_cols(column_num):
col_return = [os.system("zpool list | awk '{print$%d}'" % column_num)]
return col_return
for line in get_cols(0):
pool_name = get_cols(1)
size = get_cols(2)
alloc = get_cols(3)
free = get_cols(4)
print(pool_name + size + alloc + free)
x = subprocess.Popen(['zpool list'])
print(x)
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
当前错误
Traceback (most recent call last): File "/var/www/html/pythonscripts/BH_startup.py", line 21, in x = subprocess.Popen(['zpool list']) File "/usr/lib64/python2.6/subprocess.py", line 642, in init errread, errwrite) File "/usr/lib64/python2.6/subprocess.py", line 1238, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory
最佳答案
x = subprocess.Popen(['zpool list'])
应该是
x = subprocess.Popen(['zpool', 'list'])
关于python - 如何使用 python 可靠地解析命令 zpool status 和 zpool list 的输出,然后将所有信息插入 mysql 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31956507/