1
我正在嘗試編寫一個簡單的Python shell腳本,用於輸入服務器名稱和端口號,並將其路由到顯示SSL證書過期信息的OpenSSL命令中。Python Shell腳本。 Chain Unix OpenSSL命令
我正在使用子流程模塊,但我不清楚用用戶輸入的信息鏈接命令的正確方法。
完整的命令是:
echo | openssl s_client -servername www.google.com -connect www.google.com:443 2>/dev/null | openssl x509 -noout -dates
輸出命令(這是我希望腳本輸出):
notBefore=May 31 16:57:23 2017 GMT
notAfter=Aug 23 16:32:00 2017 GMT
我的代碼:
#!/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)
任何幫助感謝!