Hi,
Is this the most appropriate way of finding the listen port number given the pid is "16659" ?
lsof -Pan -i tcp -i udp | grep 16659 | grep -i "listen"
If so, how can I extract "7001" and assign it to a variable say myport=7001 from the below output which happens to be actual port number?
java 16659 bea 134u IPv4 0xe00000041ea28100 0t0 TCP 250.133.129.70:7001 (LISTEN)
Peasant
2
To get the desired output (only port num) by piping the output to awk
lsof -Pan -i tcp -i udp | grep 16659 | grep -i "listen" | awk 'match($0,/:[0-9]+/) { print substr($0,RSTART+1,RLENGTH) } '
If you want to put it into variable use variable=$(command)
I have not tested this code on HPUX, if you have any issues i can get back to you when i come to work.
Regards
Peasant.
---------- Post updated at 10:53 AM ---------- Previous update was at 10:30 AM ----------
Be warned mate, your command could return multiple results due to how grep works.
If you grep a lower number pid, you will most probably get multiple results and unwanted script behavior.
1 Like