所以我试图将命令从python传递到命令行作为hex换行符: \ 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"]])
&#34; \&#34; \ x0a&#34; #,问题是python在将参数传递给命令行时将其更改为 \ n
所以我要做的是打印 \ x0a 而不是 \ n
我也尝试编码
"\n".encode("hex")
which prints "0a"
问题是我如何告诉python将参数\ x0a传递给命令行。
答案 0 :(得分:0)
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_users
超过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
注意,如果您尝试将动态值传递给critical和warning,则应该通过Nagios变量执行此操作,并且不要假设它将由命令行(在远程计算机上发生)进行整形。这种技术可行,但相当棘手。
另一个主题是,如何将换行符或其他特殊字符传递给Python的命令。
这里并不是那么困难,因为你有机会传递一个参数列表,这些参数列表不会被shell解释,而是直接传递给命令。
bcmd.py
以下脚本允许测试从命令行传递的参数:
import sys
print sys.argv
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
而不是传递换行符。