用python实现抽象shell代码

2024-05-19 10:09:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有点搞不懂怎么用欧斯波本如果要在shell中执行此操作:

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

$1$2$3必须被dictionary值替换,$现在需要是现有日期(在我的代码中还没有)。你知道吗

也许吧欧斯波本不是去这里的路

谢谢你的帮助和建议

---开始---

#!/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()

---结束---


Tags: orcsvtobinisossysargument

热门问题