因此,我试图将命令从python传递到命令行,作为十六进制换行符:\x0a,在python中也称为"\n“。
我试图通过命令行打印的是:
check_nrpe -H 127.0.0.1 -c check_users -a "
echo -e "\x0a ls "#“4 4
我试过了
import subprocess as sb
sb.check_call(["check_nrpe", \ # first argument
"-H", host, # host
"-c", "check_users", # wanted remote command
"-a", # option
"\"`echo -e",
"\"\\x0a", # <new line>, problem is that python changes this to \n
parameter,
"\"` #\"", "4", "4"]])"\"\x0a" #,问题是在将参数传递给命令行时,python将其更改为\n
所以我想要做的是打印\x0a而不是\n
我还试着编码
"\n".encode("hex")
which prints "0a"问题是我如何告诉python将参数\x0a传递给命令行。
发布于 2014-06-11 08:55:58
澄清您的check_nrpe调用
假设安装了Nagios (我已经安装并运行了Ubuntu)
cd /urs/lib/nagios/plugins看,check_nrpe帮助
$ ./check_nrpe -h
NRPE Plugin for Nagios
Copyright (c) 1999-2008 Ethan Galstad (nagios@nagios.org)
Version: 2.12
Last Modified: 03-10-2008
License: GPL v2 with exemptions (-l for more info)
SSL/TLS Available: Anonymous DH Mode, OpenSSL 0.9.6 or higher required
Usage: check_nrpe -H <host> [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>] [-a <arglist...>]
Options:
-n = Do no use SSL
-u = Make socket timeouts return an UNKNOWN state instead of CRITICAL
<host> = The address of the host running the NRPE daemon
[port] = The port on which the daemon is running (default=5666)
[timeout] = Number of seconds before connection times out (default=10)
[command] = The name of the command that the remote daemon should run
[arglist] = Optional arguments that should be passed to the command. Multiple
arguments should be separated by a space. If provided, this must be
the last option supplied on the command line.
-h,--help Print this short help.
-l,--license Print licensing information.
-n,--no-ssl Do not initial an ssl handshake with the server, talk in plaintext.
Note:
This plugin requires that you have the NRPE daemon running on the remote host.
You must also have configured the daemon to associate a specific plugin command
with the [command] option you are specifying here. Upon receipt of the
[command] argument, the NRPE daemon will run the appropriate plugin command and
send the plugin output and return code back to *this* plugin. This allows you
to execute plugins on remote hosts and 'fake' the results to make Nagios think
the plugin is being run locally.回顾您的示例调用(我已经更正了在您最初的文章中丢失的格式,它隐藏了反引号):
$ check_nrpe -H 127.0.0.1 -c check_users -a "`echo -e "\x0a ls "` #" 4 4似乎您试图调用check_users命令并传递一些参数。因此,对远程(NRPE驱动的)机器的最后调用如下所示:
$ check_users "`echo -e "\x0a ls "` #" 4 4将其与check_users在帮助屏幕上的建议进行比较:
$ ./check_users -h
check_users v1.4.15 (nagios-plugins 1.4.15)
Copyright (c) 1999 Ethan Galstad
Copyright (c) 2000-2007 Nagios Plugin Development Team
<nagiosplug-devel@lists.sourceforge.net>
This plugin checks the number of users currently logged in on the local
system and generates an error if the number exceeds the thresholds specified.
Usage:
check_users -w <users> -c <users>
Options:
-h, --help
Print detailed help screen
-V, --version
Print version information
-w, --warning=INTEGER
Set WARNING status if more than INTEGER users are logged in
-c, --critical=INTEGER
Set CRITICAL status if more than INTEGER users are logged in
Send email to nagios-users@lists.sourceforge.net if you have questions
regarding use of this software. To submit patches or suggest improvements,
send email to nagiosplug-devel@lists.sourceforge.net很明显,您在check_nrpe上调用check_nrpe的尝试已经失败,因为check_users需要精确的四个参数,并且调用应该类似于(假设您认为4个用户既是关键级别又是警告级别):
$ ./check_users -c 4 -w 4因此,您对check_nrpe的最后调用可能如下所示:
$ check_nrpe -H 127.0.0.1 -c check_users -a -c 4 -w 4请注意,如果您试图将动态值传递给关键和警告,则应该对Nagios变量执行此操作,而不要假设它将由命令行(发生在远程计算机上)形成。这样的技术是可行的,但相当棘手。
将换行符或其他字符传递给命令行调用
另一个主题是,如何将换行符或其他特殊字符从Python传递给命令。
这里没有那么困难,因为您有机会传递一个参数列表,这些参数不会被shell解释,而是直接传递给命令。
简单命令行脚本bcmd.py
下面的脚本允许测试从命令行传递给它的参数:
import sys
print sys.argv从Python代码调用命令的测试
from subprocess import call
args = ["python", "bcmd.py"]
args.append("alfa")
args.append("be\nta")
args.append("""gama
hama""")
args.append("omega\x0aOMEGA")
args.append("double-omega\x0adouble-OMEGA")
args.append("literaly\\x0aliteraly")
call(args)称之为:
$ python callit.py
['bcmd.py', 'alfa', 'be\nta', 'gama\n hama', 'omega\nOMEGA', 'double-omega\ndouble-OMEGA', 'literaly\\x0aliteraly']并从中吸取教训。
结论:
check_nrpe而不是在换行符中传递。https://stackoverflow.com/questions/24157462
复制相似问题