Jump to content
43oh

Debian / Linux tip: use netcat.


Recommended Posts

Recently I was faced with a problem where I had to create a logfile with lots of output, but did not want to touch flash media in order to do so. Normally I run NFS root, but for this I was testing a prebuilt image, and I did not want to setup an NFS share just for a one-off logfile dump. To be sure there are a few way to go about this, and I've already written off a one-ff share so. . . .

 

sshfs is really nifty. In a pinch it it great for mounting a file system over an ssh tunnel. The only real problem with this is that I only had regular user permissions. Quite honestly though, even if I have root, or am in the sudoers file, I try to stay off root as much as possible.

 

Enter netcat. WIthout getting too deep into the "guts" of netcat. netcat can be used ( preferably ) on a local network to "stream data" over ethernet  a network from one system to another. For transfers over a WAN ( e.g. the internet ) one would probably be best off using socat. Anyway . . . The gist is as follows . . .

 

remote host to store the output:

william@eee-pc:~$ nc -l -p 5000 > /home/william/test/test-kernel-4.1.x.log

 

on the "local" system from which to get data from:
debian@beaglebone:~$ candump -L -n 2000 can0,0:0,#FFFFFFFF | nc 192.168.XXX.XXX 5000

 

So what we're doing here is - On the host we're listening on port 5000 for data, and redirecting it to a file we have permissions to. Then on the beagelbone we're "logging" 2000 "packets" from candump, and piping this "log file" to netcat. giving netcat the IP address, and port of the host. Although, you could replace candump with just about anything ( within reason of course ). Such as . . .

 

cat /some/dir/file | nc $IP_ADDR $PORT

 

And have it saved on the remote system . . . Anyway, I though this might come in handy for a few of you out there in the field. It's definately worked well for me.

Link to post
Share on other sites

netcat is the swiss army knife of networking tools.  Several books have been written about netcat alone.

 The more I learn about Linux ( and I've been using it since the 90's ), I find Linux in general to be the "swiss army knife" of general purpose computing. Like anything else, it can be far from perfect, sometimes. But as far as flexability goes it really can not be matched.

 

There are so many useful command line tools in Linux it boggles the mind. dd, mount, strace, gdb nc, ps . . . just to name a very small few. Granted some of these tools are also availible on Windows even, but it is the flexibility in use on Linux( or even just *NIX in general ) that sets these tools apart.

 

Using MS Windows - How easy do you all think it would be to make a 1:1 disk copy - saved as an image file, mounting this image file( or even just one partition in it- assuming multiples ) as if it was it;s own partition(s) / file system, and then sharing this same file system over a network so a remote system could use it ? Let us make it even more interresting. Only using standard command line tools. Neigh impossible ? So assuming it is not impossible, which it probably isn't, how much do you think that would cost ? I'm betting more than the average hobbyist would want to invest. Much more.

 

[EDIT]

 

I only mention Windows as a comparrison as Windows is my preffered desktop. Having been using MS products also since the 90's.

Link to post
Share on other sites

Granted some of these tools are also availible on Windows even, but it is the flexibility in use on Linux( or even just *NIX in general ) that sets these tools apart.

Many of them are available with cygwin. Windows goes far beyond these simple utils with powershell.

 

Using MS Windows - How easy do you all think it would be to make a 1:1 disk copy - saved as an image file, mounting this image file( or even just one partition in it- assuming multiples ) as if it was it;s own partition(s) / file system, and then sharing this same file system over a network so a remote system could use it ? Let us make it even more interresting. Only using standard command line tools. Neigh impossible ? So assuming it is not impossible, which it probably isn't, how much do you think that would cost ? I'm betting more than the average hobbyist would want to invest. Much more.

Trivial.

 

You can use command line or GUI to make images, mount them, and even boot them.

 

https://technet.microsoft.com/en-us/library/gg318052(v=ws.10).aspx

 

 

Sharing them over the network can also be done with GUI or command line.

 

 

Same for OS-X using Disk Utility as a GUI or command line tool.

Link to post
Share on other sites

@@oPossum Heya, how you been ?

 

I had forgotten about VHD, but it is not quite the same.  WIth Linux, no need to install 3rd party tools such as cygwin. All tools are built into the standard toolset that Linux comes with. dd, mount, etc. In the case of NFS, well one does need to download / install the package from their distro repo.

 

Some of this no doubt would be a matter of taste. In this context, and in this situation, for me. I feel that Windows is far too complex. Also the documentation might be there on the subject matter, but a lot of it is obscure by comparrison. Some of it, far more obscure.

 

Disk image:

 

dd if=/dev/sdx(x) of=/some/path/some_file bs=n

 

where . . .

if  -> in file

of -> out file

bs-> block size.

 

mount is slightly more complex, but not much. I always have to peek at a cheatsheet I made . . . still a "one-liner" command to mount a file as disk / file system though. Setting up an NFS share for network booting however, is a bit more involved. Again, I made a text guide to follow when the need arises.

 

Anyway, much of this between the two OSes can be considered " just different ". You may prefer the Windows way of doing this, where I prefer to use Linux. I'm fine with that.

 

OSX ? yuck . . . But Linux and OSX use many of the same tools . . .

Link to post
Share on other sites

@@oPossum Heya, how you been ?

I have recovered from alcoholism, but my eyesight has become worse.

 

You may prefer the Windows way of doing this

I have never said this, and never will. I use Linux, OS-X, and Windows. They are all very frustrating at times and all lack features I would like them to have.

 

I was just pointing out that Windows and OS-X both support disk images without any additional software. They both allow command line or GUI tools to be used. You are correct that it is not exactly the same as using dd. The Windows Virtual Hard Drive (vhd) and OS-X Disk Image (dmg) go beyond simple byte-for-byte copies of a chunk of media. The GUI tools will often allow using these capabilities without consulting any documentation. It is difficult for me to see how Linux is easier to use in this specific case.

Link to post
Share on other sites
I have recovered from alcoholism, but my eyesight has become worse.

 

I can see, no longer a dead-looking possum, road-side, hugging a bottle of Gin, or Vodka( Whatever it was ).

 

I was just pointing out that Windows and OS-X both support disk images without any additional software. They both allow command line or GUI tools to be used. You are correct that it is not exactly the same as using dd. The Windows Virtual Hard Drive (vhd) and OS-X Disk Image (dmg) go beyond simple byte-for-byte copies of a chunk of media. The GUI tools will often allow using these capabilities without consulting any documentation. It is difficult for me to see how Linux is easier to use in this specific case.

 

 

OSX should be able to duplicate whatever Linux can do using standard command line tools. There was little doubt in my mind about that.  However, I would not know from hands on experience.

 

As far as Windows goes . . . each step I mentioned above I'm sure Windows can do. Have I looked into this ? Yes, several times in the past, and just now as a refresher. The process of doing what I described above is very complex. So complex in fact you would need to scour technet, or MSDN for information for a long time. Before spending several weekends attempting to implement netbooting Windows, in a pure Windows envoirnment. When knowing nothing about the subject initially. With this, I called no joy after a couple weekends for lack of information, and wound up using Debian on both client, and server side. As PXE booting Windows XP was too much of a pain in the backside.

 

Would it be easier now days ? No idea, don't care enough to bother with Windows in a situation like this any more. Windows is not so important that it can not be replaced . . . Works fine as a Desktop OS though, right where I like it to be.

Link to post
Share on other sites

For the record, going through all the same steps but replacing Windows with Debian initially took me a few hours to get working. From creating disk images, mounting, sharing, and finally netbooting. Perhaps hard to believe when I spend most of my time using Windows, that *somehow* I know Linux / Debian better. . . . But I think it is more likely that Windows is just more diffcult to work with in this, and in similar cases. Hence, Why I say these things on Linux are easier.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...