将脚本运行到ssh
并返回iostat
错误时,
solaris_command = "iostat -en | awk 'NR>=3 && $4 > 0' | while read a b c d e; do echo $e,$a,$b,$c,$d; done"
for line in server_list:
line_arr = line.split(',')
if line_arr[0] == 'SunOS':
ie = subprocess.Popen(["ssh", "%s" % (line_arr[2]), "hostname"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = ie.communicate()
服务器列表的值为SunOS,4,xxx.xxx.xxx.xxx,sunhost5...
返回错误,
Traceback (most recent call last):
File "./iostat_err_check.py", line 59, in <module>
output = ie.communicate()
NameError: name 'ie' is not defined
我不确定为什么变量没有被初始化,所以我可以使用。
奇怪的是它适用于我正在做的另一项检查
linux_command = """df -Ph | awk 'NR>=2' | while read a b C d e f; do echo $f, $e, $d; done"""
Solaris_command = """df -h | awk 'NR>=2' | while read a b C d e f; do echo $f, $e, $d; done"""
for line in server_list:
line_arr = line.split(',')
# No ssh for Daily Checks host
if line_arr[2] == 'xxx.xxx.xxx.xxx':
df = subprocess.Popen(linux_command, stdout=subprocess.PIPE, stderr=None, shell=True)
elif line_arr[0] == 'Linux':
df = subprocess.Popen(["ssh", "%s" % (line_arr[2]), linux_command], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
elif line_arr[0] == 'SunOS':
df = subprocess.Popen(["ssh", "%s" % (line_arr[2]), solaris_command], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = df.communicate()
output = (output[0].splitlines())
for mount in output:
data = ("%s,%s,%s,%s\n") % (date_code, line_arr[3], line_arr[2], mount)
data_file.write(data)
servers_processed += 1
正在初始化Df并且可以使用。
答案 0 :(得分:1)
您正在关注subprocess.Popen()
来电:
if line_arr[0] == 'SunOS':
ie = subprocess.Popen(["ssh", "%s" % (line_arr[2]), "hostname"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
但 next 行始终执行:
output = ie.communicate()
ie
仅在if
测试匹配时定义,因此您需要缩进 ie.communicate()
行才能在您实际创建{Popen()
时执行1}}对象。