0

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.

2 Answers 2

5

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
4
  • 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? Sep 23, 2012 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. Sep 23, 2012 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, 2012 at 12:18
  • That's it! -f did the job. Thanks a lot. Sep 23, 2012 at 14:18
1

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
1
  • 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, 2012 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .