How to set up a reverse Telnet on Mac OS X

I am trying to set up a reverse Telnet (Telnet to COM) on Mac OS X 10.9.5 (Mavericks). The Mac is connected to a serial device. I have no problem accessing the serial device from the Mac using screen:

screen /dev/cu.USBserial

However, I would like to access the device remotely, via Telnet.

I tried using Netcat (nc) and have limited success. The command I used is:

nc -l 9999 > /dev/cu.USBserial < /dev/cu.USBserial

And then I can Telnet to the device. However, it does not work for two reasons:

  1. For interactive user interface, the input is essentially line buffered until I hit return.
  2. All my input is echoed back to me. In real life, the device should the one echoing my input characters.

What is the right way of doing it?


Solution 1:

The simplest method:

  1. telnet (or rather ssh) to your Mac
  2. type your screen /dev/cu.USBserial command

Now, if you wan't to connect to your mac directly into the com port, or allow other to do that and nothing else, here is a way:

  1. Create a dedicated user on your mac. Let's call it "comport" and log in as this user.
  2. Create a connexion script for this user. Create a file named /Users/comport/log2com.sh and insert these lines into it:

    #!/bin/bash
    /usr/bin/screen -R /dev/cu.USBserial
    
  3. Allow the script to be executed by running this command in your terminal:

    chmod +x /Users/comport/log2com.sh
    
  4. Finally, set this script as the login shell of your user. For this, in System Preferences > Users and Groups , in the users list, right click on the user and select "Advanced Option". Then, in the "shell" field, type in /Users/comport/log2com.sh, and click OK.

That's it. You can now telnet (I'd rather recommend ssh!) to this login on your Mac to get directly into screen.

Note that to exit you need to do it the screen way (usually Ctrl+A, then :quit Return).

Tested this on OSX 10.10.5. Let me know if it worked for you.

Solution 2:

I found a solution. The Python PySerial package has an rfc2217 class. They have a sample app that works as a telnet server:

https://pyserial.readthedocs.org/en/latest/examples.html

$ python rfc2217_server.py /dev/tty.serial

opens up a port 2217 that allows telnet to connect. It is exactly what I wanted.