0

For example tomcat(PID is 4739) is initiated connection to postgress(Which is another server) to retrieve data from DB because, tomcat got the request from client(curl 192.168.1.12:8088/get). In this, tomcat uses a random tcp source port to connection postgress. Now, is there any way to get these source ports that was by created tomcat PID.

I used netstat -tp, but I'm not getting PID of the connection. Getting PID only I use l - Listening like netstat -tlp. I tried to read PID sockets from /proc/PID/fd and then correlated to /proc/net/tcp. But I'm not seeing anything in /proc/net/tcp.

NOTE: The topology like below.

My requests(server1)<------->tomcat(server2)<-------->postgress(server2)

UPDATE1 netstat -tepn output

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode       PID/Program name
tcp6       0      1 172.25.30.21:36598      xx.xx.xx.91:4343     SYN_SENT    0          42955126    9661/java
tcp6       0      0 172.25.30.21:38340      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38064      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38282      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37520      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38286      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37654      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37906      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38152      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38124      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37412      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37374      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38126      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38252      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37224      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37800      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37780      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38274      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37244      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37880      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38230      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38118      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:37496      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:1099       172.25.30.21:47496      ESTABLISHED 0          42954824    4738/java
tcp6       0      0 172.25.30.21:37474      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38200      172.25.30.12:5432       TIME_WAIT   0          0           -
tcp6       0      0 172.25.30.21:38102      172.25.30.12:5432       TIME_WAIT   0          0           -

PID greped output

veeru@localhost:~/veeru# netstat -tepn | grep 4738
tcp6       0      0 127.0.1.1:41095         127.0.0.1:45180         ESTABLISHED 0          42960213    4738/java       
tcp6       0      0 172.25.30.21:1099       172.25.30.21:49984      ESTABLISHED 0          42963153    4738/java 

migrated from stackoverflow.com Feb 27 '17 at 11:30

This question came from our site for professional and enthusiast programmers.

0

netstat -tep | grep <pid of server2>

This should give you the source port used by the server2 in connecting to postgress.

  • humm! Interesting! But for my requirement I need pids for TIME_WAIT state also. I know TIME_WAIT means connection completed. I need these client ports and its PID even the connection is in TIME_WAIT. Is that possible?. (For TIME_WAIT state, it not showing any pid) – Veerendra Feb 27 '17 at 10:45
  • @Veerendra, PIDs are process identifiers. There is no PID for each connections. So, if you grep for PID with netstat, you should be able to see all the connection states – Prabhu Feb 27 '17 at 10:51
  • @Pradhu Please check my updated question. As you can see for TIME_WAIT states, there is no PID. – Veerendra Feb 27 '17 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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