python将\n作为\x0a传递给bash命令行
因此,我尝试将一个命令作为十六进制换行符从python传递到命令行:\x0a 在python中也称为“\n” 我试图通过命令行打印的内容是: 检查nrpe-H 127.0.0.1-c检查用户-a“python将\n作为\x0a传递给bash命令行,python,linux,bash,python-2.7,escaping,Python,Linux,Bash,Python 2.7,Escaping,因此,我尝试将一个命令作为十六进制换行符从python传递到命令行:\x0a 在python中也称为“\n” 我试图通过命令行打印的内容是: 检查nrpe-H 127.0.0.1-c检查用户-a“echo-e”\x0a ls“”4 我试过了 import subprocess as sb sb.check_call(["check_nrpe", \ # first argument "-H", host, # host "-c", "c
echo-e”\x0a ls“
”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"]])
问题是我如何告诉python将参数\x0a传递到命令行。澄清您的
检查\u nrpe
调用
假设您安装了Nagios(我安装了它并运行Ubuntu)
请参阅,check\nrpe
help
$ ./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\u users
命令并向其传递一些参数。因此,对远程(NRPE驱动)机器的最终调用如下所示:
$ check_users "`echo -e "\x0a ls "` #" 4 4
$ check_nrpe -H 127.0.0.1 -c check_users -a -c 4 -w 4
将其与帮助屏幕上的用户建议进行比较:
$ ./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\u nrpe
可能如下所示:
$ check_users "`echo -e "\x0a ls "` #" 4 4
$ 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
从Python代码调用命令的测试
叫它:
$ python callit.py
['bcmd.py', 'alfa', 'be\nta', 'gama\n hama', 'omega\nOMEGA', 'double-omega\ndouble-OMEGA', 'literaly\\x0aliteraly']
并从中学习
结论:
- Python允许通过列表绕过shell解析规则调用命令和传递参数
- 在您的案例中,真正的问题似乎在于使用
而不是换行符check\nrpe
'\x0a'
或多个反斜杠“\\\\\x0a”
或两者都使用?我不知道你的情况,但当我得到操作系统(“echo'\\x0a')
时,它会为我打印\x0a
。我想你只需要将“改为”它确实发送了\x0a
:-H-c check\u用户-a“`echo-e”\x0a”`4
check\u nrpe
可能是有问题的。
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']