Tuesday, June 5, 2012

Messages between computers (Linux)

In the era of internet, sometime small needs are solved using complicate applications, when "old" but more functional solutions are available.
If, like me, somebody has the need to exchange quick messages between users inside a LAN, here an easy and functional solution.


To better define the problem, the need is to be able to send to a user on a computer in the LAN, a brief message.
It is assumed the computer running Linux (Ubuntu).
The user doesn't have to start any application nor do something.
Ideally a "popup" window with the message appears on the screen.


Two programs are needed and "usually" they are installed by default in a standard Ubuntu and Kubuntu distribution.
It is possible to use other programs, but these two are quite easy to use and the "popup" effect is nice.
In order to receive messages, EACH computer we want be able to do so, need to have a "daemon" running.

Here some pro and cons


  • easy to implement
  • no special programs to install
  • transparent to the user
  • it is necessary to know the IP address of the computer where to send the message
    If the computer has a fixed IP is not a big deal. In a DHCP environment it could be a problem
  • it address the machine, not the user so it is implicit only ONE user is using the machine
  • ending the process that sends messages, ends also the receiver
  • in order to send more than one message, the receiver needs to acknowledge the messages hitting OK


I'm assuming :
  • you are capable to open a terminal
  • you are capable to open an editor 
  • you are capable to change file properties
  • you are capable to install programs
  • you don't run in the church if you hear the word "daemon"
  • you have admin permission
If you don't understand one of more of the above prerequisites, then better to ask somebody that understand that to set up the system.

Creating the daemon

The daemon is a program that is running in background. In this case the daemon is a bash script.
Here a step by step guide to create the daemon. Remember, this must be done on each computer we want to be able to receive messages.
Of course is possible to prepare the script somewhere else and copy it on the computer.
Again, it is assumed we are using Ubuntu or Kubuntu with zenity and netcat installed.
  1. open a terminal
    You should end up in your  default home directory
  2. open an editor, for example
    $ vim
  3. insert in the file these lines :
    nc -l $msgport | while read message; do zenity --info --text "$message"; done
  4. save the file
  5. make the file executable
    $ chmod +x
At this point we have the daemon script ready.
To test it, simply run it in background from the terminal : $ ./ &
Then open another terminal and digit : nc localhost 3564
When you hit Enter, everything you type until the next Enter, will be sent as message and a popup window will appear.
To stop to send messages, simply hit Ctrl-C.

It is important to understand that even if the script is running in background, closing the terminal where the daemon was started, will end the daemon.

Installing the daemon

We need to have the daemon installed and run automatically.
To do so, we need to put our script in the startup list of application.
Be aware that ending the process that sends messages, ends also the daemon !

Sending messages

In order to send a message to the machine, it is necessary to know :

  1. the IP address of the machine where to send the message
  2. the port used to send message (the one in the script, in the example above it would be 3564
The is enough to open a terminal and digit :

$ nc ip_address_where_to_send_the_msg port

For example if the machine has the IP address and the port the one of the example :

$ nc 3564

After that every thing written will be sent to the remote machine after hitting the key Enter.
To close the process, hit Ctrl-D or Ctrl-C.
Be careful that doing so, will close also the daemon on the remote machine !

No comments:

Post a Comment