Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm searching for something like:

tcpdump -p PID        # But tcpdump does not know the PID

or

lsof -i --continuous  # But lsof just runs and exits, no «live logging»

to log which connections an application opens.

In my case, I want to find out to which port git connects when committing. This happens in a fraction of a second, so I cannot use lsof. If there is a lot of traffic, filtering by PID or process name would be useful.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

You can strace the process.

$ strace -e trace=connect git ...

connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("<your local DNS resolver>")}, 16) = 0
connect(4, {sa_family=AF_INET, sin_port=htons(<PORT>), sin_addr=inet_addr("<GIT SERVER>")}, 16) = 0
share|improve this answer
    
This works for wget here and (only as root) ping. But for git the only output is «--- SIGCHLD (Child exited) @ 0 (0) ---» (also without grep), any idea why this might be? –  Simon A. Eugster Sep 23 '12 at 9:21
    
Also, strace COMMAND|grep something does not work since it will only grep on the COMMAND's output, but it works with an additional filter: strace -e trace=network -e connect COMMAND. –  Simon A. Eugster Sep 23 '12 at 9:22
2  
If git forks an additional process to do the job, add a -f flag to strace that. Yes, if you don't have the pipe redirection 2>&1 then it will only grep on stdout, which is the output of COMMAND. –  Jay Sep 23 '12 at 12:18
    
That's it! -f did the job. Thanks a lot. –  Simon A. Eugster Sep 23 '12 at 14:18
add comment

In my case, I want to find out to which port git connects when committing.

Why not:

$ git config --get remote.origin.url

The default port is:

  • git:// - 9418
  • git+ssh:// - 22
  • http:// - 80
  • ...

Otherwise, git will let you know which port it connects to when committing:

$ git config --get remote.origin.url
git+ssh://git@git.domain.com:1234/project/repo.git
share|improve this answer
    
What if the program happens to use something other than documented or what it claims to use? (Unlikely to be true with git, but could happen in other cases.) –  mattdm Sep 23 '12 at 13:35
add comment

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.