使用python执行抽象shell代码

使用python执行抽象shell代码,python,shell,csv,command,Python,Shell,Csv,Command,我分析了一些放在字典里的数据。某些dict值随每一读取行更新。 我想使用外壳代码输入这些字典元素(必须使用icinga cmd解析器) 如果我想在shell中执行此操作,我对如何使用os.popen感到有点困惑: /usr/bin/printf“[%lu]添加主机注释;$1;1;CompuWare就绪;$2,$3\n”$now>$commandfile $1$2$3必须由字典值替换,$now需要是现有日期(我的代码中还没有) 也许os.popen不是我们要去的地方 谢谢你的帮助和建议 ---开始

我分析了一些放在字典里的数据。某些dict值随每一读取行更新。 我想使用外壳代码输入这些字典元素(必须使用icinga cmd解析器)

如果我想在shell中执行此操作,我对如何使用os.popen感到有点困惑:

/usr/bin/printf“[%lu]添加主机注释;$1;1;CompuWare就绪;$2,$3\n”$now>$commandfile

$1$2$3必须由字典值替换,$now需要是现有日期(我的代码中还没有)

也许os.popen不是我们要去的地方

谢谢你的帮助和建议

---开始---

#/usr/bin/env python
#todo:测试所提供的参数或路径是否存在,否则退出
#使用外部命令的脚本
#添加主机评论,最多需要30秒!如果您不断添加注释,则可以添加多个注释!
#添加主机注释;,,;
#/bin/printf“[%lu]添加主机注释;nagios.yourict.net;1;CompuWare就绪;这是一个testcomment\n”$now>$commandfile
#其中$1是第一个参数:如icinga中所定义
#其中$2是第二个参数:服务或应用程序ie应用程序备份或应用程序新建基础设施
#其中$3是第3个参数:位置ie:loc\u datacenter1或loc\u datacenter2\n
#now=`date+%s`
#commandfile='/omd/sites/master/tmp/run/icinga.cmd'
#/usr/bin/printf“[%lu]添加主机注释;$1;1;CompuWare就绪;$2,$3\n”$now>$commandfile
从子流程导入调用
导入系统、操作系统、csv
omd_站点='master'
cmdfile='/omd/sites/%s/tmp/run/icinga.cmd'(omd_site)
如果len(sys.argv)==2:
f=open(sys.argv[1],'r')#open文件名作为唯一参数给出
csv_f=csv.reader(f,分隔符=“;”)#创建一个csv文件对象
对于csv\u f中的行:
#包含用作注释字段的元素的字典
dict={“主机名”:行[0],“组”:“grp_PDU”,“服务”:“svc_数据中心”,“位置”:行[1]}
#filestrip=lines.strip()
#打印行。strip()
#print os.popen(“printf”+filestrip.read())
#print os.popen(“printf”+dict[“hostname”]).read()+os.popen(“printf”+dict[“group”]).read()
os.popen(“printf”+dict[“group”]).read()
其他:
打印“您没有提供1个参数。正在退出…”
系统出口
f、 关闭()

---结束---

您真的需要用管道发布主机评论吗?
我认为,最好的选择是将其放入主机配置并重新加载icinga。

您真的需要用管道发布主机注释吗?
我认为,最好的选择是将其放入主机配置并重新加载icinga。

是这样做的,但似乎不起作用,但我发现了如何在调用函数中解析python变量,同时等待一些社区输入cmd=[“/usr/bin/printf”,“\”[%lu]“+”添加\u主机注释;“+stru主机+”;1;“+stru组+”;“+stru svc+”,loc“+stru loc+”\\\\n“+”\“”+str(unixtime)+“>”+“”]call(cmd)最后我不会执行这个shell命令,因为我需要写入fifo,我所做的一切毫无意义。。。将像这样解析数据并通过pythondid将其写入fifo,但似乎不起作用,但我发现了如何在调用函数中解析python变量,同时等待一些社区输入cmd=[“/usr/bin/printf”,“\”[%lu]“+”添加\u主机注释;“+stru主机+”;“+stru组+”;“+stru svc+”,loc\\\\n+str(unixtime)+“>”+”]call(cmd)最后我不会执行这个shell命令,因为我需要写入fifo,我所做的一切毫无意义。。。将解析数据并通过python将其写入fifo
#!/usr/bin/env python

# todo : test to see if provided argument or path does exist, else exit

#Script to use external commands
#Add Host Comment , can take up to 30s ! You can add multiple comments if you keep adding them !

#ADD_HOST_COMMENT;<hostname>,<persistent>,<author>;<comment>
#/bin/printf "[%lu] ADD_HOST_COMMENT;nagios.yourict.net;1;CompuWare Ready;This is a testcomment\n" $now >$commandfile


# where $1 is 1st argument : <hostname> as defined in icinga
# where $2 is 2nd argument : Service or app ie app_backup or app_newinfrastructure
# where $3 is 3nd argument : Location ie : loc_datacenter1 or loc_datacenter2\n

# now=`date +%s`
# commandfile='/omd/sites/master/tmp/run/icinga.cmd'
# /usr/bin/printf "[%lu] ADD_HOST_COMMENT;$1;1;CompuWare Ready;$2,$3\n" $now > $commandfile



from subprocess import call
import sys,os,csv


omd_site = 'master'
cmdfile = '/omd/sites/%s/tmp/run/icinga.cmd' % (omd_site)

if len(sys.argv) == 2:
        f = open(sys.argv[1], 'r')              # open filename given as only argument
        csv_f = csv.reader(f, delimiter=';')    # create a csv file object
        for row in csv_f:
                #dictionary containing our elements to use as comments field
                dict = {"hostname" : row[0], "group" : "grp_PDU", "service" : "svc_Datacenters", "location" : row[1]} 
                #filestrip = lines.strip()
                #print lines.strip()
                #print os.popen("printf " + filestrip).read()
                #print os.popen("printf " + dict["hostname"]).read() + os.popen("printf " + dict["group"]).read()
                os.popen("printf " + dict["group"]).read()
else:
        print "You did not provide exactly 1 argument. Exiting..."
        sys.exit
f.close()