Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythonshell脚本。链接Unix OpenSSL命令_Python_Shell_Unix_Subprocess_Pyopenssl - Fatal编程技术网

pythonshell脚本。链接Unix OpenSSL命令

pythonshell脚本。链接Unix OpenSSL命令,python,shell,unix,subprocess,pyopenssl,Python,Shell,Unix,Subprocess,Pyopenssl,我正在尝试编写一个简单的pythonshell脚本,它将接收用户输入的服务器名称和端口号,并将其路由到一个显示SSL证书过期信息的OpenSSL命令中 我正在使用子流程模块,但是我不清楚将命令与用户输入的信息链接起来的正确方法 完整命令是: echo | openssl s_client -servername www.google.com -connect www.google.com:443 2>/dev/null | openssl x509 -noout -dates 命令的输出

我正在尝试编写一个简单的pythonshell脚本,它将接收用户输入的服务器名称和端口号,并将其路由到一个显示SSL证书过期信息的OpenSSL命令中

我正在使用子流程模块,但是我不清楚将命令与用户输入的信息链接起来的正确方法

完整命令是:

echo | openssl s_client -servername www.google.com -connect www.google.com:443 2>/dev/null | openssl x509 -noout -dates
命令的输出(这是我希望脚本输出的):

我的代码:

#!/usr/bin/env python
import subprocess 

server_name = raw_input("Enter server name: ")
port_number = raw_input("Enter port number: ")

def display_cert_info(servername, portnum):
    pn = str(server_name + ":" + port_number)
    cmd = ["echo", "|", "openssl", "s_client", "-servername", str(servername), "-connect", pn, "2>/dev/null", "|", "openssl", "x509", "-noout", "-dates"]

    info = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = info.communicate()[0]
    print(output)

display_cert_info(server_name, port_number)

感谢您的帮助

与shell不同,标准输入、标准输出和标准错误都由的
stdin
stdout
stderr
参数处理。因此,您可以放弃前两个命令元素(
echo |
)。然后,您需要使用管道中的最后一个命令运行一个单独的进程,将第一个命令的输出放入其
stdin
。一般来说,在使用另一种语言运行时,您不使用shell管道,而是使用该语言自己的管道(或流式)机制。

@Han我意识到我可能会来不及帮助您,对此表示抱歉,但这是为其他正在寻找此解决方案的人准备的。我必须将Popen()和check_output()函数串在一起,如下所示:

#!/usr/bin/python3
from subprocess import Popen, PIPE, check_output
from os import open, O_WRONLY

servers = [
    "server1.domain.com",
    "server2.domain.com",
    "server3.domain.com"
    ]

for s in servers:
    print("querying {}".format(s))
    dn = open("/dev/null", O_WRONLY)
    q = Popen(["/usr/bin/openssl", "s_client", "-servername", s, "-connect","{}:443".format(s)], stdout=PIPE, stdin=PIPE, stderr=dn, shell=False)
    y = check_output(["/usr/bin/openssl", "x509", "-noout", "-dates"], stdin=q.stdout)
    print(y.decode("utf-8"))
这使我能够对添加到列表中的所有服务器进行快速而肮脏的审核。我的下一次迭代是在输出中添加监视/警报,再也不会对过期的证书感到惊讶

#!/usr/bin/python3
from subprocess import Popen, PIPE, check_output
from os import open, O_WRONLY

servers = [
    "server1.domain.com",
    "server2.domain.com",
    "server3.domain.com"
    ]

for s in servers:
    print("querying {}".format(s))
    dn = open("/dev/null", O_WRONLY)
    q = Popen(["/usr/bin/openssl", "s_client", "-servername", s, "-connect","{}:443".format(s)], stdout=PIPE, stdin=PIPE, stderr=dn, shell=False)
    y = check_output(["/usr/bin/openssl", "x509", "-noout", "-dates"], stdin=q.stdout)
    print(y.decode("utf-8"))