Shell脚本:读取输出运行下一个命令 [英] Shell Script : read the output to run the next command
问题描述
我试图运行此,但仍没有得到输出。不知道是我错了。
首先,脚本应该登录到服务器结果
第二个命令是 netstat的-tulpun | grep按端口
(端口和放大器;服务器列表是在同一个文件)结果
第三个它可能会得到3至4输出这里,但我只需要25 PID输出不是别人
I am trying to run this but still not getting the output. Don't know were I am wrong.
First the script should login to the server
2nd command is "netstat -tulpun | grep -i port
(port & server list is in the same file)
3rd It may get 3 to 4 output here but I need only 25 PID output not the others
#!/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
更新所需的输出
Server1的结果
端口1311结果
根8063 8062 0 2014年? 0时08分06秒的/ opt / dm_cd经营;
Updated required output
Server1
Port 1311
root 8063 8062 0 2014 ? 00:08:06 /opt/dm_cd -run
=====结果
Server2的结果
端口1311结果
根6844 6843 0 2014年?零点20分22秒的/ etc /斌/ Linux的/ DS经营;
=====
Server2
Port 1311
root 6844 6843 0 2014 ? 00:20:22 /etc/bin/linux/ds -run
=====结果
Server3的结果
端口8000结果
applmgr 1505 4215 0 2014年? 0时05分44秒/应用/阿帕奇/斌/ httpd的-d
=====
Server3
Port 8000
applmgr 1505 4215 0 2014 ? 00:05:44 /app/Apache/bin/httpd -d
推荐答案
不需要的输出线可能是由于需要的PID出现在其他列中的 PS -ef
输出(PPID,CMD)或其他PID的一部分。替换 PS -ef
按 PS -fp%
来纠正它。
The unwanted output lines are probably due to the needed PID appearing in other columns of the ps -ef
output (PPID, CMD) or as part of another PID. Replace ps -ef
by ps -fp%
to rectify it.
这篇关于Shell脚本:读取输出运行下一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!