我正在尝试运行它,但仍然没有得到输出。不知道我是不是错了。
首先脚本应该登录到服务器
第二条命令是“netstat -tulpun | grep -i port
(端口和服务器列表在同一个文件中)
3rd 它可能在这里得到 3 到 4 个输出,但我只需要 25 个 PID 输出而不是其他的
#!/bin/bash
while read -r -u10 server port line
do
echo ========== server: "$server" port: "$port" ==========
ssh $line "netstat -tulpunt | grep -E \"\b$port\b\"" | awk '{print $7}' | grep '/' | awk -F '/' '{print $1}' | xargs -I % bash -c 'echo Port % && ps -ef | grep % && echo ' | grep -v grep
done 10< demo
更新所需的输出:
=====
服务器1
端口 1311
root 8063 8062 0 2014 ? 00:08:06 /opt/dm_cd -run
=====
服务器2
端口 1311
root 6844 6843 0 2014 ? 00:20:22 /etc/bin/linux/ds -run
=====
服务器3
端口 8000
applmgr 1505 4215 0 2014 ? 00:05:44 /app/Apache/bin/httpd -d
最佳答案
不需要的输出行可能是由于所需的 PID 出现在 ps -ef
输出(PPID、CMD)的其他列中或作为另一个 PID 的一部分。将 ps -ef
替换为 ps -fp%
以纠正它。
关于linux - shell 脚本 : read the output to run the next command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959343/