Category Archives: Linux

All posts around Linux.

vim – Goodbye to :set paste

I’ve been using vim as my editor of choice ever since I started learning Linux, and something that has been bothering me for a while is how vim handles pasting.

Say I want to paste a large bit of code into a terminal running vim. Before I do this I have to type:

When everything is pasted, I turn it off with:

or:

The command :set paste prevents vim from auto-indenting the code I’ve just pasted.

Luckily, as it most often goes, there is a solution. Why I haven’t bothered to actually find the answer till recently is a whole other matter.

As it turns out, my terminal of choice (which currently is rxvt-unicode) supports something called bracketed paste mode.

In short, when bracketed paste mode is set, pasted text is bracketed with control sequences so that the program can differentiate between pasted text and typed-in text.

Let’s stay that I copied the text:

from another program. When I paste it into my terminal, if it supports bracketed paste mode, it actually sends the text:

Now the thing is to let vim know how to watch out for these control sequences, and tell it what to do. Paste the following code into your .vimrc:

And that should reduce your use of :set paste quite a bit!

SimpleHTTPServer with SSL

I’ve often used Python’s SimpleHTTPServer to simply share a directory with someone over a network, it being either local or the Internet. In case you don’t know how it works, it’s simple. To start a HTTP server, at your current location, type:

and the result:

It listens on all IPv4 interfaces, and binds to the port you specify, which in my case is 8080. The person on the other side will then be able to access the files in the directory from the outside by going to http://server1.example.com:8080, provided that your machine has the hostname server1.example.com, and that you have the port 8080 forwarded to the IP of server1.

But what if you want to provide a secure connection, say over SSL? SimpleHTTPServer has no built in way of doing this.

But behold ssl, Python’s built in SSL-module!

To create a secure connection for your SimpleHTTPServer, first create a self signed certificate by running the following command (if you don’t have a proper SSL-certificate, that is):

Now create a script named shttps.py that contains the following code:

The only thing that needs further explanation is the variable bind_to_address. Fill this in with the text localhost if you want it to only listen to 127.0.0.1. Leave it blank to have it listen to all IPv4 interfaces (0.0.0.0).

Now that the certificate and key is all in place, and the script has been created, make it executable with:

Go to the folder you’d like to share the contents of, and run the script:

The result when you visit https://server1.example.com:8080?Because there is no third party verification it’s listed as insecure, but it should do the trick well enough for sharing files with others.

If you however do want a free SSL certificate for a more permanent setup, I suggest LetsEncrypt! Check out https://letsencrypt.org/getting-started/ for more information.

VMware, i3 and multiple monitors

For a while now I’ve been trying to set up VMware to work with multiple monitors, in a Linux guest. With some windowmanagers it works out of the box without any issue, such as with Unity. I never figured out how to do it with xmonad, and recently I switched to i3 just to try something new. The damn “Cycle multiple monitors” button didn’t work here either. When I tried it, a message popped up saying:

The virtual machine must have up-to-date VMware Tools installed and running.

..which it had! At this point I had installed vmware-tools, which is described as:

“A suite of utilities that enhances the performance of the virtual machine’s guest operating system and improves management of the virtual machine”

However, I found a solution! Place the following line in your i3 configuration file, whether it be ~/.i3/config or ~/.config/i3/config:

..and that’s it! Reload your i3 configuration, and now you should be able to press the “Cycle multiple monitors” button and have dual monitors in your VMware guest!

Now, if you’re using open-vm-tools instead of the native vmware-tools, do what Fabian suggested – use the following line in your config-file instead:

It should work!

ncat, a modern implementation of netcat

ncat is a utility that is like the UNIX cat command but for network connections. It’s based on the original netcat and comes with a couple of more modern features.

In this short post, we’ll go through a couple of examples to see exactly what uses this tool has. I’m currently using ncat version 7.01, in Ubuntu 16.04. ncat is a part of the nmap package in Ubuntu.

Shiny new things

A couple of the features of ncat, some of which are new, are:

  • IPv6 Support
  • Chain multiple ncat together
  • Support for SSL
  • Ability to specify specific hosts to allow or deny access to in listen mode

While the new features are great, it’s important to note that ncat is not 100% reverse compatible with the original netcat.

Examples

Let’s continue with a couple of examples to get you started.

IPv4 or IPv6?

To force ncat to only use either IPv4 og IPv6, use:

  • -4
  • -6

..as in:


..to connect to a server only through IPv6.


..and type in:


..and press enter twice. The result will be something along the lines of:


The option -C is used because it requires CRLF line endings.

Chaining

An example from nmap’s website; sending a log file from host1 to host3, by way of host2:

host3

host2

host1

Cloning partitions over the network

One of the more useful tricks is the ability to clone partitions over the network.

On the system you’d like to clone the partition from, do:


..and on the receiving machine:


To speed up the process of transfer you can always throw in gzip for compression:


..and:

Web server

Setting up a simple webserver is also easy:


The option -k makes ncat keep listening and accepting more connections after the first one is finished.

File transfer with SSL

On the machine you want to send the file from:


..and on the receiving end:


The option –send-only does what it says – it only sends data and ignores received.

Ports

Need to check if a port is open? Try:

This example checks if port 53 is open, with a timeout of 5 seconds. When a port is open:

..and when it’s closed:

Chat server

As far I know, the are two main ways to do this.

First way

Start listening on a port of your choice:


..and connect to it from another machine:


Type in some text and the line will appear on the other machine when you press enter. You won’t be able to see who wrote what, but hey, it’s good enough if you want to communicate with someone.

Second way

The new fancier way of starting a chat-server is by using –chat:


Users who then want to connect to the chat:


The output will be something along the lines of:


The user IDs generated by ncat are based on the file descriptor for each connection and must be considered arbitrary. Also, you won’t see <userX> in front of the text you type, but others will see it. The main difference when using –chat is that you and every user connected to the server will get a <userX> tag, making it easier to see who wrote what.

Mail client

ncat also works as a mail client. Expect to type a lot:


..followed up by typing:

TCP/UDP daytime server

The daytime service, defined in RFC 867, sends a human-readable date and time string over TCP or UDP port 13. It ignores any input. So, we can use:


Add –udp to create an UDP daytime server instead.

Access control

Allow one host, deny others

Deny one host, allow others

Allow or deny hosts from file


Replace –allowfile with –denyfile to deny and trusted-hosts.txt with a file that contains the hosts to be denied.

These are just a few of the things that you can do with ncat. Have fun exploring the rest!

Split a file into a number of equal parts

As an example, we have a file named primary_data_file.txt that contains 616 lines of data. We want to split this into 4 files, with the equal amount of lines in each.

The following command should do the trick:

The option -da generates the suffixes of length 1, as well as using numeric suffixes instead of alphabetical.

The results after running the command are the following files: