script which runs netstat -tulpun | grep -i port_from_the_list
& takes the output for running the next command
I need to run 2 command on all the server mentioned in the list.
server: Jan port: 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 1505/httpd
server: feb port: 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 1505/httpd
- login to the server
- run the first command
netstat
which gives the output of process id ps -ef | grep -i 1505 "processid"
runs it will take that process id output & run the commandps
Accepted output
server: Jan port: 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 1505/httpd
root 1421 15033 0 13:38 pts/16 00:00:00 grep -i 1505
server: feb port: 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 1505/httpd
root 1421 15033 0 13:38 pts/16 00:00:00 grep -i 1505
I am unable to read the output of the netstat
command and run the next.
while read -r -u10 server port line
do
echo ========== server: "$server" port: "$port" ==========
ssh -qn "$server" "netstat -tulpun | 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
echo
done 10< demo
netstat
such that you can then run theps
command on the associated PID? – Bratchley Jan 13 at 19:14netstat
doesn't show you any process IDs (PIDs). On the other hand, your process may have the port number (1505 in your example) listed as an argument, in which case you should go with that and skipnetstat
command. Otherwise I am unable to understand your question, like the first comment author, Bratchley. As a side note, you can uselsof
command,grep
for the port number and parse the output for the PID if this is your intention. – MelBurslan Jan 13 at 20:40-p
option makesnetstat
show the PID and program name.1505/httpd
means PID = 1505, program = httpd. – Barmar Jan 14 at 20:50netstat
- it's deprecated. And kinda broken. Usess
. – mikeserv Jan 15 at 6:04