首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python将\n作为\x0a传递给bash命令行

python将\n作为\x0a传递给bash命令行
EN

Stack Overflow用户
提问于 2014-06-11 07:53:34
回答 1查看 889关注 0票数 0

因此,我试图将命令从python传递到命令行,作为十六进制换行符:\x0a,在python中也称为"\n“。

我试图通过命令行打印的是:

check_nrpe -H 127.0.0.1 -c check_users -a "echo -e "\x0a ls " #“4 4

我试过了

代码语言:javascript
运行
复制
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

我还试着编码

代码语言:javascript
运行
复制
  "\n".encode("hex")
  which prints "0a"

问题是我如何告诉python将参数\x0a传递给命令行。

EN

回答 1

Stack Overflow用户

发布于 2014-06-11 08:55:58

澄清您的check_nrpe调用

假设安装了Nagios (我已经安装并运行了Ubuntu)

代码语言:javascript
运行
复制
cd /urs/lib/nagios/plugins

看,check_nrpe帮助

代码语言:javascript
运行
复制
$ ./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.

回顾您的示例调用(我已经更正了在您最初的文章中丢失的格式,它隐藏了反引号):

代码语言:javascript
运行
复制
$ check_nrpe -H 127.0.0.1 -c check_users -a "`echo -e "\x0a ls "` #" 4 4

似乎您试图调用check_users命令并传递一些参数。因此,对远程(NRPE驱动的)机器的最后调用如下所示:

代码语言:javascript
运行
复制
$ check_users "`echo -e "\x0a ls "` #" 4 4

将其与check_users在帮助屏幕上的建议进行比较:

代码语言:javascript
运行
复制
$ ./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个用户既是关键级别又是警告级别):

代码语言:javascript
运行
复制
$ ./check_users -c 4 -w 4

因此,您对check_nrpe的最后调用可能如下所示:

代码语言:javascript
运行
复制
$ check_nrpe -H 127.0.0.1 -c check_users -a -c 4 -w 4

请注意,如果您试图将动态值传递给关键和警告,则应该对Nagios变量执行此操作,而不要假设它将由命令行(发生在远程计算机上)形成。这样的技术是可行的,但相当棘手。

将换行符或其他字符传递给命令行调用

另一个主题是,如何将换行符或其他特殊字符从Python传递给命令。

这里没有那么困难,因为您有机会传递一个参数列表,这些参数不会被shell解释,而是直接传递给命令。

简单命令行脚本bcmd.py

下面的脚本允许测试从命令行传递给它的参数:

代码语言:javascript
运行
复制
import sys
print sys.argv

从Python代码调用命令的测试

代码语言:javascript
运行
复制
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)

称之为:

代码语言:javascript
运行
复制
$ python callit.py 
['bcmd.py', 'alfa', 'be\nta', 'gama\n        hama', 'omega\nOMEGA', 'double-omega\ndouble-OMEGA', 'literaly\\x0aliteraly']

并从中吸取教训。

结论:

  • Python允许调用命令并通过list传递参数,绕过shell解析规则。
  • 在您的例子中,真正的问题似乎是在使用check_nrpe而不是在换行符中传递。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24157462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档