能够直接在主机上运行脚本,但不能通过Jenkins运行脚本

时间:2017-07-12 21:33:42

标签: python jenkins

我有一个脚本,它基本上可以插入多个主机以获取某些信息,并检查文件以查看信息是否匹配。直接从Jenkins节点运行时,脚本运行正常。但是当我尝试通过Jenkins运行它时,它会挂起。它确实创建了输出文件,我添加了调试语句,并且看到来自file.txt的主机正在Jenkins上打印,但是它只是挂起。 我正在传递文件名作为arg之一。在Jenkins上,我用字符串参数来传递文件。我究竟做错了什么? 提前谢谢。

import sys
import os
import subprocess
import re
import datetime

try:
  hosts_file = sys.argv[1]
  with open(hosts_file, 'r') as f:
    hosts = []
    for line in f:
      hosts.append(line.split()[0])
except IOError:
  print 'Error opening hosts_file'
  sys.exit(1)

for host in hosts:
    cmd = ('sshpass -f password pssh -A -O UserKnownHostsFile=/dev/null -O StrictHostKeyChecking=no -H ' + host + ' -l root  -i "<cmd>"')
    with open('file.txt', 'w') as fw:
      subprocess.call(cmd, shell=True, stdout=fw)
    with open('file.txt', 'r') as fr:

  for line in fr:
        match = re.search(r'some_word', line, re.I)
        if match:
          id_list.append(line.split()[-1])
          model_dict[id_list[-1]] = line.strip()

    if len(id_list) == 0:
      print "something not found for hostname =>  %s" % host

    with open('file.txt', 'r') as fr, open('output', 'a') as fw:
      fw.write("something ==> %s" % host + "\n")

fw.write("*****************************************************************************************************************************************************************" + "\n")
      for line in fr:
        for item in id_list:
          match = re.search(r'%s' % item, line, re.I)
          if match:
            my_line = model_dict[item] + '  => ' +  line
            fw.write(my_line)
            id_list.remove(item)

    if len(id_list) > 0:
      with open('output', 'a') as fw:
        for item in id_list:
          my_line = model_dict[item] + ' => ' + 'No match found'
          fw.write(my_line + "\n")

    with open('models.txt', 'r') as fr, open('output', 'a') as fw:
      fw.write("#### Printing Controller information ####" + "\n")
      for line in fr:
        match = re.search(r'vmhba', line, re.I)
        if match:
          fw.write(line.strip() + "\n")

    os.remove('models.txt')
#I'm assuming this will print on the Jenkins console
with open('output', 'r') as fr:
  for line in fr:
    print line

0 个答案:

没有答案
相关问题