LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-27-2022, 12:20 PM   #1
urkelbundy
LQ Newbie
 
Registered: Mar 2015
Posts: 8

Rep: Reputation: Disabled
socat and tee


Hi all,

I usually manage to find what I need to do with a little help of google, but this time I can't seem to figure out why it is not working.

On my LAN there is a serial to ethernet bridge which serves a serial port on IP port 9000 at 192.168.100.18

I connect with that port from another machine on my LAN with the socat command below.
The program that uses the serial data connects to /dev/myserial.

socat TCP:192.168.100.18:9000 SYSTEM:'tee /var/log/in.log | socat - "PTY,link=/dev/myserial,raw,echo=0,waitslave" | tee /var/log/out.log'

This works fine. The progam that connects to /dev/myserial receives the data and I get 2 log files that show the data that comes in and goes out of the serial port.
I am very happy with this except for 1 thing. I would like to have date and time in front of every line in the log so that I know when the data went through the serial port.

I read a lot on internet and came up with the command sequence below.
The ts command adds a date and time string before every line.
The date and time string should only go to the log files and not to the serial port otherwise the program that is using /dev/myserial does not understand the data that it receives.

socat TCP:192.168.100.18:9000 SYSTEM:'tee >(ts > /var/log/in.log) | socat - "PTY,link=/dev/myserial,raw,echo=0,waitslave" | tee >(ts > /var/log/out.log)'

Unfortunatelly I receive this:
sh: 1: Syntax error: "(" unexpected
2022/01/27 19:04:59 socat[30494] E waitpid(): child 30495 exited with status 2

I am a bit surprised that the error says sh while I am using bash. Not sure if this has something to do with the issue.

I tried at various locations in the socat command sequence to use single or double quotes but it did not help.

As a test I tried this:
echo Hello | tee >(ts > /var/log/in.log)
This works as expected. In in.log I see "Jan 27 19:13:06 Hello" without the double quotes.

Why isn't my socat command sequence working and how do I get it to work as described?
 
Old 01-27-2022, 12:47 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
The documentation isn't clear on this, but I suspect that for a SYSTEM:<shell-command> type of address, socat forks /bin/sh which on Debian-based systems is dash.

What happens if you explicitly specify SHELL?
Code:
SHELL=/bin/bash socat ...

Last edited by shruggy; 01-27-2022 at 12:52 PM.
 
Old 01-27-2022, 12:51 PM   #3
urkelbundy
LQ Newbie
 
Registered: Mar 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Indeed /bin/sh is a link to dash. I changed it to be a link to bash, but the same error message remained.
 
Old 01-27-2022, 12:59 PM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
In POSIX shell, you can emulate process substitution to some extent with named pipes. It should work here.

Last edited by shruggy; 01-27-2022 at 01:05 PM.
 
Old 01-27-2022, 01:04 PM   #5
urkelbundy
LQ Newbie
 
Registered: Mar 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
How does the socat command look like with named pipes?
 
Old 01-27-2022, 01:16 PM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Something like (untested):
Code:
mkfifo fifo1 fifo2

ts <fifo1 >/var/log/in.log&
ts <fifo2 >/var/log/out.log&

socat TCP:192.168.100.18:9000 \
  SYSTEM:'tee fifo1 | socat - "PTY,link=/dev/myserial,raw,echo=0,waitslave" | tee fifo2'
BTW, did assigning the SHELL environment variable not work? Ah sorry, I've just noticed you answered while I was updating my post #2 with new info.

Last edited by shruggy; 01-27-2022 at 01:24 PM.
 
Old 01-27-2022, 02:30 PM   #7
urkelbundy
LQ Newbie
 
Registered: Mar 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Although I never used named pipes I understand the concept but unfortunately it is not yet working.

Data is coming from fifo1. This is data without the date and time and this data also goes to the application, which is good.
The file in.log is created but nothing is added to the file.

I have a question about these lines:
ts <fifo1 >/var/log/in.log&
ts <fifo2 >/var/log/out.log&

Because of the & at the end, the commands are running in the background.
When they exit they show the PID I believe, but if I look for this PID I do not see the ts command.
Am I missing something?
Because I tried it a couple of times I may now have several running in the background or are the removed by some mechanism?

I will have a look at it at a later date and report back.
Thanks anyway for this solution. I really appreciate it.
 
Old 01-27-2022, 02:45 PM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by urkelbundy View Post
Because I tried it a couple of times I may now have several running in the background or are the removed by some mechanism?
I usually remove created named pipes with trap, like this:
Code:
#!/bin/sh
dir=$(mktemp -d --tmpdir) ||
  { echo Cannot create temp dir;exit 1;}
mkfifo $dir/fifo1 $dir/fifo2 ||
  { echo Cannot create fifos;rm -r $dir;exit 1;}
trap "rm -fr -- '$dir'" EXIT

ts <"$dir/fifo1" >1.log&
ts <"$dir/fifo2" >2.log&

seq 10|tee "$dir/fifo1"|sed 's/.*/@&@/'|tee "$dir/fifo2"
 
Old 01-27-2022, 03:02 PM   #9
urkelbundy
LQ Newbie
 
Registered: Mar 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Clear. Very neat solution. Thanks.

Will report back later when I have some time to investigate.
 
Old 01-30-2022, 02:36 AM   #10
urkelbundy
LQ Newbie
 
Registered: Mar 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi Shruggy

I do not know what went wrong the first time I tried but it works now.
The socat and tee are running for more than a day without a hitch.
The logs are filling up nicely.

Besides that I learned something new. Although I heard about named pipes I never used them.
Now that I know what it is and how to use them, I will going to use them more often.
Many thanks for the example script you published. That really helped.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] what is 'socat' and what is it used for? Ubuntu-15.04 MrUmunhum Linux - Software 8 10-04-2015 10:47 AM
netcat or socat expert needed wgcampbell Linux - Networking 5 03-01-2010 12:39 PM
socat slow Lorian Linux - Networking 0 05-11-2009 10:30 AM
Debian for mips: installing socat (and related problems) SomeIdiot Linux - Software 3 03-25-2009 01:17 PM
LXer: CLI Magic: socat LXer Syndicated Linux News 0 03-26-2007 08:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration