6

On a Gentoo Linux machine, a process is communicating with another computer via serial port (/dev/ttyS1). I want to listen to this communication (i.e. what is being sent/received by the process) without further interfering.

How can I achieve this with just basic Linux tools? I even can change the serial port the process is communicating with, so maybe I can 'shortcut' two devices (/dev/ttyS1 and /dev/xxx) listen to all communication, when all output to /dev/ttyS1 is forwarded to /dev/xxx and vice versa. But how do I do it? Something with socat?

3
  • ethtool ? yes I know it will not work but you never know
    – Kiwy
    Commented Feb 24, 2014 at 13:31
  • I am looking for basic linux tools, nothing exotic.
    – Alex
    Commented Feb 24, 2014 at 13:49
  • Nothing exotic about ethtool.
    – terdon
    Commented Feb 24, 2014 at 14:19

2 Answers 2

11

interceptty looks like what you want.

I found that from this Ubuntu page

interceptty - Intercept traffic to and from a serial port.

Example

If you want to use interceptty as an external serial monitor [connected to two serial ports on your machine and relaying between them, while recording the output]

you can use one device as the backend, and use the -p option to tell the frontend not to create it's own tty, but just use the one you tell it:

interceptty -s 'ispeed 19200 ospeed 19200' /dev/ttyS0 -p /dev/ttyS1 -

Output

interceptty prints its output in a fairly unattractive, painful to look at format. However, it is very easy for other programs to parse. For an example of how to post-process this output into something appropriate to whatever you are intercepting, see the included Perl script interceptty-nicedump.

Output lines are in this general format:

< 0x54 (T)

  0x4b (K)  ^ Direction    ^^^^ Hex code (to real device)
    ^^^ ASCII character (to real device)
     ^^^^ Hex code (from real device)
          ^^^ ASCII character (from real device) The direction marker is a '<' if this character was sent to the backend device, and

'>' if it was received from the backend device. It is always followed by a single space. If the character was received from the real device, a tab will appear next (this makes the output easier to follow). After that is the hex code for the character, and the ASCII representation of the character if it is an ASCII character.

1
  • it look nice though if ethtool is exotic I can't even think about a tool nobody ever heard of :D
    – Kiwy
    Commented Feb 24, 2014 at 14:09
4

One approach (not necessarily the best...) would be to attach strace to the process (or, in order to handle the race condition, to a wrapper script which execs to this process), set strace to maximum string length and then catch all read()s and write()s (or whatever your process uses). After that you grep the lines with the right file descriptor (which should always be the same; if not then you have to catch the open()s, too).

3
  • Nice idea, but what are the file descriptors for /dev/ttyS1?
    – Alex
    Commented Feb 24, 2014 at 13:49
  • @Alex That depends on what the process is doing. You can see them in /proc/$PID/fd and in strace as the return value of the open() calls. Commented Feb 24, 2014 at 13:53
  • See also the -e read=x -e write=y options to get a hex dump of what is read/written on fd x. Commented Feb 24, 2014 at 14:34

You must log in to answer this question.

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