Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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    
  1. login to the server
  2. run the first command netstat which gives the output of process id
  3. ps -ef | grep -i 1505 "processid" runs it will take that process id output & run the command ps

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

share|improve this question
    
Unsure of what you're asking. Are you asking how to parse the output of netstat such that you can then run the ps command on the associated PID? –  Bratchley Jan 13 at 19:14
    
To the best that I know, netstat 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 skip netstat command. Otherwise I am unable to understand your question, like the first comment author, Bratchley. As a side note, you can use lsof command, grep for the port number and parse the output for the PID if this is your intention. –  MelBurslan Jan 13 at 20:40
    
@MelBurslan The -p option makes netstat show the PID and program name. 1505/httpd means PID = 1505, program = httpd. –  Barmar Jan 14 at 20:50
    
So far i have created this, but still the output is not upto the mark. I am not getting the output as required. please check the Question again. –  Ang Red Jan 15 at 2:53
    
Drop netstat - it's deprecated. And kinda broken. Use ss. –  mikeserv Jan 15 at 6:04

1 Answer 1

You can start with

for port in $(cat demo); do
   mypid=$(netstat xxxx |
      grep ":${port} " | grep LISTEN | sed -e 's/.*LISTEN//'| cut -d/ -f1)
   ps -fp ${mypid}
done

Possiple changes (what you are familiar with):

  • Use xargs for calling ps
    ... -f1) | xargs ps -fp
  • Use sed for cutting the /
  • Use while loop
    cat demo | while read port; do
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.