gpt4 book ai didi

python 将\n as\x0a 传递给 bash 命令行

转载 作者:太空宇宙 更新时间:2023-11-04 03:57:37 25 4
gpt4 key购买 nike

所以我试图将命令从 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 传递到命令行。

最佳答案

澄清您的 check_nrpe 调用

假设您安装了 Nagios(我安装了 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_users 的尝试被破坏,因为 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']

并从中学习。

结论:

  • Python 允许通过列表调用命令并传递参数,绕过 shell 解析规则。
  • 就您而言,真正的问题似乎是使用 check_nrpe 而不是传递换行符。

关于python 将\n as\x0a 传递给 bash 命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24157462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com