Linux Fu: Leaning Down With Exec

Shell scripting is handy and with a shell like bash it is very capable, too. However, shell scripting isn’t always very efficient. Think about it. If you run grep or tr or sort to do some operation in a shell script, you are spawning a whole new process. That takes time and resources. But there are some answers to reducing — but not eliminating — the problem.

Have you ever written a program like this (in any language, but I’ll use C):

int foo(void)
{
  ...
  bar();

}

You hope the compiler doesn’t write assembly code like this:

_foo: 
....

      call _bar
      ret

Most optimizers should pick up on the fact that you can convert a call like this to a jump and let the ret statement in _bar return to foo’s caller. However, shell scripts are not that smart. If you have a shell script called MungeData and it calls another program or shell script called PostProcess on its last line, then you will have at one time three processes in play: your original shell, the shell running MungeData, and either the PostProcess program or a shell running the script. Not to mention, the processes to do things inside post process. So what do you do?

Continue reading “Linux Fu: Leaning Down With Exec”

Think You Know CURL? Care To Prove It?

Do you happen to remember a browser-based game “You Can’t JavaScript Under Pressure”? It presented coding tasks of ever-increasing difficulty and challenged the player to complete them as quickly as possible. Inspired by that game, [Ben Cox] re-implemented it as You Can’t cURL Under Pressure!

In it, the user is challenged in their knowledge of how to use the ubiquitous curl in a variety of different ways. Perhaps this doesn’t sound terribly daunting, especially if your knowledge of curl is limited to knowing it is a command-line tool to fetch something from a web server. But curl has a staggering number of features. The man page is over 4500 lines in length. The software’s main site offers a (free) 250+ page guide on how to use curl and libcurl. Reflecting on this is exactly what led [Ben] to create his challenge.

It’s a wonderful piece of work, but things get really interesting once [Ben] starts talking about the infrastructure behind it all. At its core the game works by giving the user a problem and a virtual machine, and catching outgoing HTTP calls to see whether they look correct. If the outgoing HTTP call is the right solution for the problem, terminate the current VM and start up the next one with the next problem. He’s put a lot of work into getting suitable VMs up and running quickly, securely, and properly isolated. The code can be found on the project’s GitHub repository for those who want a closer look.

But that’s not all. [Ben] says that in the past he’s had a bad habit of presenting interactive features in his blog posts that can’t keep up with sudden demand. So to address that, the system auto-scales as needed with a small Linux cluster; small brick-sized PCs are started and shut down automatically to meet demand. Hey, the only thing cooler than a functioning cluster is a cluster doing an actual job, like this one that detects NSFW images.

Now Even Your Business Card Can Run Linux

It takes a lot of work to get a functional PCB business card that’s thin, cheap, and robust enough to be practical. If you can even blink a few LEDs on the thing and still hand them out with a straight face, you’ve done pretty well for yourself. So you can imagine our surprise when [George Hilliard] wrote in to tell us about his $3 business card computer that boots into a functioning Linux environment. If this were a bit closer to April, we might have figured it was just a joke…

Of course it helps that, as an embedded systems engineer, [George] literally does this kind of thing for a living. Which isn’t to say it was easy, but at least he keeps close enough tabs on the industry to find a suitable ARM solution at a price that makes sense, namely the Allwinner F1C100s. This diminutive chip offers both RAM and CPU in a single package, which greatly simplifies the overall design and construction of the card.

With a root filesystem that weighs in at just 2.4 MB, the environment on the card is minimal to say the least. There’s no networking, limited I/O, and forget about running any heavy software. But it does boot in about six seconds, and [George] managed to pack in a MicroPython interpreter and a copy of the classic Unix dungeon crawler rogue.

Oh yeah, and it also has his resume and some samples of his photography onboard. It is, after all, a business card. All the user has to do is plug it into the USB port of their computer and wait for the virtual serial port to pop up that will let them log into the system running on the card. It also shows up as a USB Mass Storage device for recipients who might not be quite as adept at the command line.

In addition to the high-level documentation for this project, [George] has also prepared a deeper write-up that goes into more technical detail for anyone who might be looking to follow in his footsteps. Thanks to all of the source code that he’s made available, it should be a lot easier for the next person to get their own disposable pocket computer up and running.

We’ve seen all manner of electronic business cards over the years, but never anything quite like this. Which, of course, is quite the point. If you’re ever given a business card that doubles as a computer running a full-fledged operating system on it, you aren’t likely to forget it anytime soon.

Linux Fu: WSL Tricks Blur The Windows/Linux Line

We have to admit, we have an odd fascination with WSL — the Windows subsystem for Linux. On the one hand, it gives us more options on Windows 10 for running the software we love. On the other hand, we wonder why we aren’t just running Linux. Sometimes it is because our cool laptop doesn’t work well on Linux. Other times we are using someone else’s computer that we aren’t allowed to reload or dual boot. Still, as long as we have to use Windows, we are glad to have WSL. A recent blog post by [Hanselman] shows some very cool tricks for using WSL that make it even better.

Exploring WSL

Did you know you can use WSL to run Linux commands in a Windows command shell? For example, you have a long directory and you want to run grep:

dir c:\archive\* | wsl grep -i hackaday

Of course, from bash you could access the same directory:

ls /mnt/c/archive | grep -i hackaday

Continue reading “Linux Fu: WSL Tricks Blur The Windows/Linux Line”

Linux Fu: Stupid SSH Tricks

If you connect to remote computers over the Internet, it is a pretty good chance you use some form of SSH or secure shell. On Linux or Unix you’ll use the ssh command. Same goes for Linux-like environments on Windows like Cygwin or WSL. For native Windows, you might be using Putty. In its simplest form, ssh is just a terminal program that talks to a server using an encrypted connection. We think it is very hard to eavesdrop on anyone communicating with a remote computer via ssh.

There are several tricks for using ssh — some are pretty straightforward and some are things you might not think of as being in the domain of a terminal program. You probably know that ssh can copy files securely, and there are easy and hard ways to set up logging in with no password.

However, you can also mount a remote filesystem via ssh (actually, there are several ways to do that). You can use ssh to securely browse the web in your favorite browser, or even use it to tunnel specific traffic by port or even use it as a makeshift VPN. In fact, there’s so much ground to cover that this won’t be the last Linux Fu to talk about ssh. But enough setup, let’s get to the tricks.

Continue reading “Linux Fu: Stupid SSH Tricks”

Nintendo Switch Doubles As Network Switch

Coming straight to you from the “Department of Redundant Redundancies” comes this clever hack that turns a Switch into a switch. More specifically, a network switch. Not even a half bad one either, judging by the speed tests [Cynthia Revström] performed after setting it all up. We wouldn’t advise you dump your existing network gear in favor of a repurposed game system, but perhaps in a pinch…

Despite what you might be thinking, there’s no hardware modifications at work here. This is a fully functional Nintendo Switch that’s just had two USB to Ethernet adapters plugged into it. The secret ingredient is the addition of some Penguin Power, up and running on Nintendo’s latest and greatest thanks to a project called switchroot.

With Linux running on the system, all [Cynthia] had to do was make sure that the USB to Ethernet adapters were supported, and fiddle around with the brctl and ip commands to configure a bridge between the interfaces to get the packets moving. Putting the Switch between the main network and a test computer showed it had a throughput of just over 90 Mbps, which is about all that could be expected from the USB-connected network interfaces.

From here it wouldn’t have taken much more effort to get the system working as a wireless router and providing services like DHCP and NAT to clients. But since Nintendo didn’t see fit to call it the Router, that would’ve offered minimal meme value. There’s always next generation.

Seeing the Nintendo Switch do a surprisingly good job running as an Ethernet switch is even more surprising given the fact that it struggles to function with accessories that are actually intended for it. Though to be fair, the migration to USB-C has been a little rockier than most of us would have hoped.

Line Printer Does Its Best Teletype Impression

Back in the early days of computing, user terminals utilized line printers for output. Naturally this took an incredible amount of paper, but it came with the advantage of creating a hard copy of everything you did. Plus it was easy to annotate the terminal output with nothing more exotic than a ballpoint pen. But once CRT displays became more common, these paper terminals (also known as teleprinters, or teletypes) quickly fell out of style.

A fan of nostalgic hacks, [Drew DeVault] recently tried to recreate the old-school teletype experience with (somewhat) more modern hardware. He picked up an Epson LX-350 line printer, and with a relatively small amount of custom code, he was able to create a fairly close approximation of what it would have been like to use one of these terminals. He’s published all the source code, so if you’ve got an old line printer and a Linux box, you too can learn what it was like to measure your work day in reams of paper.

This is made possible by the fact that the modern Linux virtual terminal is simply a userspace emulation of those physical terminals of yore. [Drew] just need to write some code that would essentially spawn a shell on the Linux USB line printer device, plus sprinkle in some quality of life improvements such as using Epson’s proprietary ANSI escape sequences to feed the paper out far enough so the user can see what it says before pulling it back in to write the next interactive line.

Of course, the experience isn’t perfect as the printer naturally doesn’t have a keyboard attached to it. If you’re looking for something a bit more authentic, you could always convert an old electric typewriter into a modern-ish teletype.