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

Python 能够直接在主机上运行脚本,但不能通过Jenkins运行,python,jenkins,Python,Jenkins,我有一个脚本,它基本上可以在多个主机中搜索一些信息,并检查一个文件以查看信息是否匹配。直接从Jenkins节点运行时,脚本运行良好。但当我试图通过詹金斯运行它时,它挂起了。它确实创建了输出文件,我添加了调试语句,并看到file.txt中的主机被打印到Jenkins上,但除此之外它只是挂起。 我将文件名作为参数之一传递。在Jenkins上,我使用字符串参数来传递文件。我做错了什么? 提前谢谢 import sys import os import subprocess import re impo

我有一个脚本,它基本上可以在多个主机中搜索一些信息,并检查一个文件以查看信息是否匹配。直接从Jenkins节点运行时,脚本运行良好。但当我试图通过詹金斯运行它时,它挂起了。它确实创建了输出文件,我添加了调试语句,并看到file.txt中的主机被打印到Jenkins上,但除此之外它只是挂起。 我将文件名作为参数之一传递。在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
导入系统 导入操作系统 导入子流程 进口稀土 导入日期时间 尝试: hosts\u file=sys.argv[1] 将打开的(hosts_文件,'r')作为f: 主机=[] 对于f中的行: hosts.append(line.split()[0]) 除IOError外: 打印“打开主机\u文件时出错” 系统出口(1) 对于主机中的主机: cmd=('sshpass-f password pssh-A-O UserKnownHostsFile=/dev/null-O strichhostkeychecking=no-H'+host+'-l root-i”“) 打开('file.txt','w')作为fw: subprocess.call(cmd,shell=True,stdout=fw) 打开('file.txt','r')作为fr: 对于fr中的行: 匹配=重新搜索(r'some_word',line,re.I) 如果匹配: id_list.append(line.split()[-1]) model_dict[id_list[-1]]=line.strip() 如果len(id_list)==0: 打印“找不到主机名=>%s”%host的内容 打开('file.txt','r')作为fr,打开('output','a')作为fw: fw.write(“某物==>%s”%host+“\n”) fw.写(“****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** 对于fr中的行: 对于id_列表中的项目: match=re.search(r“%s”%item,line,re.I) 如果匹配: my_line=型号目录[项目]+'=>'+行 fw.write(我的_行) id\u列表。删除(项) 如果len(id_列表)>0: 将open('output','a')作为fw: 对于id_列表中的项目: my_line=model_dict[item]+'=>'+'未找到匹配项' fw.write(我的行+“\n”) 打开('models.txt','r')作为fr,打开('output','a')作为fw: fw.写入(“###########”+“\n”) 对于fr中的行: 匹配=重新搜索(r'vmhba',行,重新搜索I) 如果匹配: fw.write(line.strip()+“\n”) 删除('models.txt') #我想这会在詹金斯控制台上打印出来 以open('output','r')作为fr: 对于fr中的行: 打印行
files.txt正在创建,但为空。看来pssh不起作用了。但是当它直接在主机上运行时,它是工作的。这是因为pssh模块需要导入,而我的主机有这个模块,所以它直接在主机上工作很好,但是jenkins使用的是其他地方的Python,它没有这个模块,因此它失败了。如何设置Jenkins使用主机的Python?files.txt正在创建中,但为空。看来pssh不起作用了。但是当它直接在主机上运行时,它是工作的。这是因为pssh模块需要导入,而我的主机有这个模块,所以它直接在主机上工作很好,但是jenkins使用的是其他地方的Python,它没有这个模块,因此它失败了。如何设置Jenkins使用主机的Python?