Skip navigation

Tag Archives: netcat

(If you just want the good stuff and don’t care about the story, visit the snd2remote web page. Bottom line: run a listener on the LAN, then on a Linux box type “snd2remote C:/WINDOWS/Media/ding.wav”)

Problem: You’re running applications on a remote Linux system across a LAN, possibly using an X server such as Xming or x11vnc to view them on a Windows machine.  The programs run fine, displaying on the Windows machine, but there is no way to hear notifications on events such as receiving an email or IM. Every program has an option that lets you “run a custom command” on notification events, or has an available add-on that enables this functionality. There is no custom command that makes sounds play on the Windows machine you’re actually using.

Solution: Roll your own solution using free tools that are already available!

I use Mozilla Thunderbird for email and Pidgin for all instant messaging services. For two years, I have sat directly in front of the server at the shop and used these applications. Now, however, I use an old Windows laptop that we can’t sell for technical reasons along with Xming to run these Linux apps on my sever and show them as apps on my Windows laptop.  Unfortunately, as I have been getting busier and busier, I have been forgetting to periodically check my mail window.  Thunderbird in Xming has no tray mail notification and no sound on the Windows machine at all; likewise with Pidgin, which (at best) can put a star in the chat window title.  However, Pidgin on Linux comes with an option to “use a custom command” for playing sounds.  Thunderbird doesn’t, but an add-on easily adds “during new mail notification, run this command” capabilities to it.

So, I know that I CAN do this if I can run a command which plays the sound on the Windows machine, but a brief Google hunt turns up nothing of interest.  Oh, I’m sure somehow I can use JACK or some other complicated audio system, but I could care less about setting that up and dealing with the extra admin overhead if there’s a simpler way!

It takes some real Linux geek thought processes to do, but I figured out a way that, regardless of which machine in my shop I sit at, I can get audio notifications for my Xming apps (or any app that can run a command to play a sound for that matter).  The answer: UDP broadcast the sound you want to play to all the machines on the network.

I created a bash shell script and a Windows NT/2000/XP command prompt  batch file (it uses SET /P which I don’t think DOS/Win9x/Me have) that pair together to trigger sounds on my Windows machine from my Linux machine. An additional constraint which I artificially imposed on myself to minimize the number of required downloaded files was to only use Windows batch commands and not “cheat” by pulling a UNIX-style shell, sed, grep, and other UNIX commands.  This also forced me to learn some ways to do things in a batch file that I had never done before, meaning that not only do I make this easier to duplicate for others, but I’ll be a better Windows admin too.

I had to download the following free tools and Linux packages to make it work (links provided):

Here’s how we do it. The computer which is generating the notification event needs to order the computer at which we’re actually sitting to play some sort of sound. Ideally, we need to be able to play lots of different sounds, too. So, the quick and easy solution was to use socat (and netcat for Windows since there’s no native socat port) to broadcast the names of sound files to play and receive those broadcasted names, then try to play them.  Most of the time I spent was actually on file scanning and filtering code rather than making the *cat tools behave nicely.

“snd2remote” on Linux can both listen for events AND broadcast them, and does some fairly clever search tricks to try very hard to play a WAV file that is not valid for the Linux machine as-is.  The Windows version is restricted to batch file commands, which means it’s much “dumber” and therefore needs more hand-holding; most of the clever logic is in the Linux side so the Windows side can afford to be dumbed down.

snd2remote taught me the following lessons and/or has the following nice features:

  1. You can make netcat exit immediately on receipt of a single UDP packet in listen mode by piping an empty “echo” into it. This drastically decreases latency because the 1-second timeout is unnecessary.
  2. The way to remove timeout latency in socat is to specify a “udp4-recvfrom” endpoint instead of “udp4-listen” and also specify the “unidirectional” switch.
  3. Translating to backslashes for dumb Windows batch listeners and letting Linux listeners translate them back to normal forward slashes makes life a thousand times easier.
  4. Making the Linux version assume UNC paths such as \\server\share\path\file.wav equals /home/share/path/file.wav on the Linux side allows the use of a default Samba home directory share as a source for both machines to fetch the same WAV files from.
  5. Letting the Linux script perform quick scans in a couple of obvious locations for alternate copies of the specified file makes it extremely easy to feed Windows-specific paths and have them still play on the Linux listeners without replicating the path to the same file on Windows.
  6. snd2remote in listen mode checks the ROOT of the executing user’s home directory, does full subdirectory scanning of a custom directory (defaults to $HOME/media), and performs the previously mentioned UNC path to home directory translation, in an exhaustive effort to find a playable copy of the requested WAV file.
  7. It’s better to use the shell construct $(commands) than to use (backticked) `commands` because the latter interprets special characters in a way that makes life very painful.

By running “snd2remote -l” on a Linux box or “snd2remote-listener.bat” on a Windows box, any other Linux box on the LAN can broadcast a sound event to all listening computers with a simple command:

snd2remote [-q] /path/to/file.wav

For example, to play the classic Windows “ding” on all listeners:

snd2remote C:/WINDOWS/Media/ding.wav

If you are interested in the internal workings of these scripts, they are heavily commented to explain in detail what is going on.  Lots of shell/command interpreter constructs and command combinations are used in them which I had not needed, or had rarely used, before; therefore, these scripts would be a good starting point to nudge your way into some interesting scripting.

The code has grown too long and complex to document everything here, but it is freely available at the c02ware snd2remote page.

One of the greatest things in the world about Linux is the amount of flexibility it gives a person in creating a solution to a problem, and I found myself faced with yet another situation where this became relevant lately.  Some background is in order before I get to the point of the story, however, so grab your popcorn and soda, and prepare to be astonished!

As many readers will no doubt know, I am hard at work on Tritech Service System 3.0, the next incarnation of c02ware’s Linux distribution which we use regularly in our shop.  Since TSS is primarily developed with the needs of a computer repair service business in mind, we obviously look for ways to help TSS help us do our jobs, which is where some of the strange and unique features I’ve come up with originate.  One of the most novel ideas has been that of having a TSS CD which doesn’t require upgrading, because every upgrade cycle I’m forced to distribute new burned CDs to all of the technicians and rewrite all of our bootable USB flash drives for the new system.  TSS 3.0 will have the ability to upgrade over a network automatically during early startup.

Being able to self-upgrade before running anything very important is difficult, however, primarily due to the fact that it’s easy to make a messy solution that works in a specific case, but very hard to make a clean solution that works 99% of the time.  I’ve been writing custom scripts for the private TSS CDs that specifically refer to our core server by its internal IP address, meaning if it’s not on our network or the server’s down for any reason, there’s no way to run that script.  Making them generic, where they would automatically locate a suitable server clone such as a mirror on an external hard drive, is much harder, because you have to scan for things and, if using a network resource but not having its IP pre-programmed, you have to discover that resource somehow, without any prior knowledge of its existence.

Yes, this is all sort of straying from the point of the post, but I’m building up to it, so bear with me for a moment longer.

THE QUESTION:  “How can I find a network resource containing files I need, even if I have no idea what the server’s IP address is…AND get 100% of the information required to access those files without any manual user intervention?” Nothing that I could think of immediately seemed to be a viable solution, other than perhaps somehow pulling a particular network name via Samba and using that, but then I considered that perhaps the server itself might want to be running TSS with the default hostname of…well, “tss” (which obviously presents a problem, one which will also be fixed at some point!)  Then it started to hit me: what if we could somehow set up a “beacon” that would magically tell every “beacon-capable” TSS on the network that the beacon sender is a server?

Eureka!  …but not so fast…

The first thing that came to mind was, of course, good old fashioned netcat.  Netcat  (or just nc) is notoriously useful software that does a very simple thing: provide a way to pipe commands and information over TCP and UDP connections.  Well, netcat definitely would have done the job, so I tried something similar to this, to pipe a “configuration string” via UDP to the broadcast IP address for the LAN I was on at the time:

echo “12.34.56.78 share path/to/file username password” | nc -u 192.168.0.255 9999

Unfortunately, all  kinds of really freakin’ weird socket errors were spit out, and it totally choked on me.  Apparently you can’t use netcat to do UDP broadcast on Linux for some reason (at least, not GNU netcat).  I was extremely disappointed, and I started to research netcat to see if there was some sort of solution.  I hit a forum post that mentioned socat and looked it up.  Holy cow, I hit jackpot!

socat is referred to by many as “netcat on steroids” and now I can see why.  Where netcat is a simple TCP or UDP pipeline tool, socat is a much more generic socket-to-socket version cat (thus “socat,” or “socket cat”).  This bad boy can hook up UNIX sockets, files on disk, standard input/output/error, TCP, UDP, raw IP with no protocol, and more, bidirectionally, and without restrictions on which can be input or output.  The help text for the usable socket modes alone is over 50 lines long, and it doesn’t complain at all if I try to use it to do a UDP broadcast.

What happened when I tried it out?  Check it out:

# socat UDP4-LISTEN:9999 GOPEN:foo &
[1] 14130
# echo "192.168.0.100 share path/to/files username password" | \
> socat - UDP4-DATAGRAM:192.168.0.255:9999,broadcast
# cat foo
192.168.0.100 share path/to/files username password
[1]+  Done                    socat UDP4-LISTEN:9999 GOPEN:foo

Of course, this was all on the same machine, so I tried something on an old Linux router box on the network which I can’t use socat on for technical reasons.  With GNU netcat listening, the exact same configuration beacon broadcast came in perfectly:

$ nc -u -l -p 9999
192.168.0.100 share path/to/files username password

What does all of this silly stuff mean in the end?

With some clever scripting, Tritech Service System 3.0 will be able to automatically update itself from any server on the LAN. That means that when TSS 3.0.1 or 3.1 or even 4.0 appear, one machine running the newer version in its default configuration will be able to give its updated packages to all other machines on the network without any user intervention required. That means I can set up a TSS package repository on my main server and set up a TSS package beacon, and all of the TSS 3.0 CDs and flash drives I give to technicians won’t ever need to be replaced or rewritten just to get updated packages.  Bug fixes from one running new version will automatically propagate to older ones.  If I make a custom package that isn’t part of mainstream TSS, such as our custom scripts, they can be distributed only to private installations by the on-location server rather than being burned to the CD, eliminating the need for special private versions of TSS for in-shop use.

This, combined with an enhanced package acquisition system, dependency management, lower memory usage, persistent directories, and a host of other features will make Tritech Service System 3 one of the most flexible live Linux distributions in existence.

Follow

Get every new post delivered to your Inbox.

Join 42 other followers

%d bloggers like this: