how to connect 2 applications through a pseudo serial port with socat - linux

I am developing an embedded application. It will be running on a full-fledged embedded linux, but will be controlled through commands arriving from the serial port from a windows host.
during the debug phase, this windows host is running in a virtual machine and the embedded application is run locally. As such I would like for these 2 to communicate normally as if a normal usb (to serial) cable was connected between them.
How do I go about it? I have heard of the notion of pseudo ports (/dev/ptyXX) but haven't found any C tutorial to go about it.
I basically expect something like a device node (or 2), so I can assign one node to virtualbox "serial port" settings and assign the other node to my linux applicatio that is currently running on the linux host.
Thank you for your help
EDIT: it looks like socat might be the tool for the job , but I can't quite grab the way it works. I am trying the command:
socat -d -d PTY:link=/home/nass/acm0,raw,echo=0 UNIX-CONNECT:/home/nass/dm0
2013/07/18 17:53:56 socat[13271] N PTY is /dev/pts/10
2013/07/18 17:53:56 socat[13271] N opening connection to AF=1 "/home/nass/dm0"
2013/07/18 17:53:56 socat[13271] E connect(5, AF=1 "/home/nass/dm0", 16): Connection refused
2013/07/18 17:53:56 socat[13271] N exit(1)
Why is the connection refused?
If /home/nass/dm0 is missing, the virtual machine will fail to startup (both in host pipe and host device cases)
I pre-created the node /home/nass/dm0 as a file with touch /home/nass/dm0. how should I setup virtualbox? host device? host pipe?
should dm0 even exist before powering up the virtual machine? if yes, should it be a file created with touch /home/nass/dm0? If it should be a socket, how do I pre-create it?

I realize this is an old question, but I was recently working with socat and virtual serial ports and this cropped up in a google search.
If you want socat to create the unix socket, then use UNIX-LISTEN instead of UNIX-CONNECT:
socat pty,link=/home/nass/acm0,raw,echo=0 unix-listen:/home/nass/dm0
Running the above produces a pty device with a symlink at $HOME/acm0:
$ ls -l ~/acm0
lrwxrwxrwx 1 lars lars 10 Jan 12 23:12 /home/lars/acm0 -> /dev/pts/8
And a Unix socket at $HOME/dm0:
$ ls -l ~/dm0
srwxr-xr-x 1 lars lars 0 Jan 12 23:12 /home/lars/dm0
If you connect to the unix socket and send data, it will be sent out the pty device (and vice-versa). This works, but it may not be what you want. If your Linux application expects to interact with a serial port, then instead of a unix socket you would want a second pty device:
socat pty,link=$HOME/vmside,raw,echo=0 pty,link=$HOME/hostside,raw,echo=0
This creates a pair of connected pty devices (one named vmside and one named hostside). You would connect the vmside pty to your virtual machine, and then you would connect your application (or other serial communication program, like picocom or cu or screen) to hostside.

Related

virtual serial port from remote physical serial port

I have a local client host (linux), which runs an application (like minicom) that connects to a serial port.
However, the physical serial port (/dev/ttyUSB0) is present on a remote server host (linux), to which I have ssh access.
For various reasons, I do not want to run the application on the server host through ssh.
Therefore, I am connecting this physical serial port to a local virtual serial port (/dev/ttyNET0) using socat:
sudo socat "pty,link=/dev/ttyNET0,group-late=dialout,perm-late=666,sane,rawer" \
"exec:'ssh -tt ${remote-host} socat "stdio,sane,rawer" "file:/dev/ttyUSB0,sane,rawer,b115200"',pty,sane,rawer"
I am then able to connect to this serial port on my local machine as follows:
sudo minicom -D "/dev/ttyNET0"
Note that there were two calls of socat in the previous command: one on the client side, and one on the server side.
I am wondering if the equivalent of this command can be constructed with a single call of socat. I tried this:
sudo socat "pty,link=/dev/ttyNET0,group-late=dialout,perm-late=666,sane,rawer" \
"file:'ssh://${remote-host}:/dev/ttyUSB0',sane,rawer,b115200"
But it did not work. I have read through the manpage of socat, but I might be missing something.

Linux command to send data to a remote tcp-client

I have a Linux Server running Redhat Rhel 7 and a Device called "Compoint Lan System (Colas)" (german manufacturer). The Colas has its own firmware so I don't know if it's based on linux. The Colas is set as a TCP-Client. It receives messages from its serial 1 port. I get the messages coming from the serial port 1 of the colas on my server with rsyslog.
Now what I want is to send a string (2 letters) from my server (tcp-server) to my colas's serial port 1 (tcp-client) to get information of the device attached to serial port 1.
Is there a command in linux to accomplish that? Something like "command 'string message' destination port"? I am sorry if it isn't written well.
Install netcat
yum install nc
Make it to listen to a particular port number
nc –l portnumber &
Lets validate it using netstat from a different console:
netstat -anlp |grep yourportnumber
PS: Change the installation command based on your linux flavor.
Ranadip Dutta's answer meets your requirement. The listen there doesn't mean listen for incoming data, it rather means listen for connection request from client. Of course you can't use rsyslog and nc as the server at the same time, but with nc you get the messages coming from the Colas displayed as well as the characters you enter sent.

node-serialport equivalent of remserial

remserial is a program aimed at acting as a communication bridge between a TCP/IP network port and a linux serial port.
Is there a way to use the serialport node package (or another) to achieve the same behavior as this command?
./remserial -r 10.42.0.70 -p 5001 -l /dev/remserial1 /dev/ptmx

iperf between 2 interfaces on same host

I am looking for a way how I can run iperf benchmark between 2 interfaces on the same host as shown on the schema:
Both of the machines (pc and router) are linux.
I'm need a way to run some functional tests on my "router" machine using my only 1 PC with 2 ethernet ports.
I tried already few different ways to solve it but still no luck:
1) Adding static routes for destination hosts with metric
2) Binding iperf to specific ip address
3) Disabling local loopback interface
Basicly what I'm looking for is to run traffic 192.168.1.10<-->1.1.1.10 thru the router device (with ip forwarding enabled).
Don't forget to bind the server as well:
For the server run iperf -s -B 1.1.10 in terminal
and on another terminal instance for the client run iperf -c 1.1.10 -B 192.168.1.10
This will normally work if your OS doesn't use the loop-back interface by default.

how to replicate putty ssh tunnel settings with pure command line commands?

After a day of searching I am unable to replicate the feature with command line for ssh tunnel and reverse tunnel.
reverse ssh tunnel configuration
normal ssh tunnel configuration
with the UI settings above I am able to get it working with instruction on the web!
currently I am trying to do
device 1 connect to (ssh server) and passes its port 80 to port 9191 onto the server then the server relays the port 9191 to device #3.
(device #1 reverse tunnel port 9191 to server device #2) <--> (server device #2 receives port 80 and use it as port 9191 which will relay to device #3) <--> (device #3 with normal tunnel to get port 9191 from device #1 with device #2 being the middle man)
I am able to use the putty user interface to click do get it working, but I am unable to replicate a working situation with command line. ssh command line examples out there do not seem to work.
for example:
on reverse tunnel device
ssh -R 9191:localhost:80 root#localhost
on the normal tunnel device
ssh -L 9191:localhost:9191 root#localhost
please let me know if the question is lacking info or unclear
thanks!
To clarify my intent, I am trying to make a tunnel between (2 android devices) with a SSH server. I can't SSH directly into an android mobile device simply because of firewall issue, so I have to use a standalone SSH server to act as a middleman to help relay the connection. However if there is any other better options please let me know as well, for example VPN or anything which may not use a lot of battery power on our mobile devices through 3g/wifi.
any other options would be fine but please consider 3g/wifi/firewall/battery consumption/data overhead concerns as well. I am not sure how does the major mobile app handle this, so any new ideas or methods are welcomed.
thanks in advanced
As i understand from the comments, you're on Device 1 (Android A), trying to talk to Device 3 (Android B) via Device 2 (SSH host).
Try this on Android A:
ssh -L 9191:android_b:9191 root#ssh_host
On Android B:
ssh -L 9191:android_a:80 root#ssh_host
Or also from Android A:
ssh -R 9191:android_a:80 root#ssh_host
That should serve Android A's port 80 on Android B's port 9191, and that one on Android A's port 9191.
On Device A
ssh -R 9191:127.0.0.1:80 user#RemoteHost
On Device B
ssh -L 9191:127.0.0.1:9191 user#RemoteHost

Resources