我设计了一个简单的bash脚本,只是为了处理一个命令的输出,该命令根据文本文件中的条目数循环。我在下面附上了脚本:
`#!/bin/bash
# Check if at least one argument (server list file) is provided
if [ $# -lt 1 ]; then
echo "Usage: $0 server_list_file"
exit 1
fi
# Input file containing server names and port numbers (server:port)
server_list_file="$1"
# Check if the input file exists
if [ ! -f "$server_list_file" ]; then
echo "Error: Server list file '$server_list_file' not found."
exit 1
fi
# Loop through each line in the server list file
while IFS= read -r line; do
# Split the line into server_name and server_port
server_name=$(echo "$line" | cut -d ':' -f 1)
server_port=$(echo "$line" | cut -d ':' -f 2)
echo "Checking server: $server_name, port: $server_port"
# Run the openssl command with the server_name and server_port
openssl s_client -connect "$server_name:$server_port" -servername "$server_name" 2> /dev/null | openssl x509 -noout -dates
# Add a separator line between server checks
echo "------------------------"
done < "$server_list_file"`
该脚本接受一个参数文件,该文件在命令行上传递,如$checkopenssl.sh servers.txt,servers.txt文件如下所示:
example.local:443
test.example.org:8443
another.server.local:12345
我遇到的问题是,当我试图运行脚本时,它设法打破循环,重新运行命令,但是如果我注解掉openssl命令,它将循环多次,因为有条目。
有人能提出一个解决方案吗?
我希望输出看起来像下面这样:
Checking server: example.local, port: 443
notBefore=Mar 11 07:26:29 2023 GMT
notAfter=Apr 11 07:26:29 2024 GMT
------------------------
Checking server: test.example.org, port: 8443
notBefore=Mar 11 07:26:29 2023 GMT
notAfter=Apr 11 07:26:29 2024 GMT
------------------------
Checking server: another.server.local, port: 12345
notBefore=Mar 11 07:26:29 2023 GMT
notAfter=Apr 11 07:26:29 2024 GMT
------------------------
1条答案
按热度按时间wrrgggsh1#
这是因为第一个
openssl
命令从标准输入中读取,耗尽了输入文件。可能有一个命令行选项来避免这种情况,或者你可以直接重定向一个noop: