Tag Archives: Linux - Page 4

Repeat a command every x seconds

Say you wanted to see whenever the content of a folder changes – what to do? Easy! Use the watch command:

watch 'ls -l'

This will clear the screen and output the contents of the command and update it every 2 seconds (clearing screen before each update). The watch command works for most scripts and commands

Tip: Using Tmux and the watch command together can help you create a really cool window with a bunch of “live” updated small windows

Tested on RHEL 3

Create a SSH tunnel inside a chain of SSH tunnels example

Here is an example of SSH tunnel setup inside a couple of SSH tunnels to create an all encrypted connection.

The setup is shown in the picture below:
System setup
The following rules apply:

  • Server 1 is public
  • Server 2 can only be accessed through Server 1
  • Server 3 can only be accessed through Server 2
  • All Servers have a SSH server running
  • My computer has only an SSH client

To set this up we start with the “small” tunnels:

Step 1

Set up Tunnel 3 with the following command:

ssh -N -L 2000:localhost:22 user@Server3

This creates a persitent (-N option) port forwarding from port 22 on Server 3 and to port 2000 on Server 2

Step 2

Set up Tunnel 2 between Server 1 and Server 2 with this command:

ssh -N -L 3000:localhost:2000 user@Server2

This sets upp a persistent tunnel between port 2000 on Server 2 and to port 3000 on Server 1

Step 3

Set up my connection (Tunnel 1) to the tunnel on Server 1 (the tunnel which is going all the way to Server 3)

ssh -N -L 4000:localhost:3000 user@Server1

This sets up a persistent tunnel between port 3000 on Server 1 and to port 4000 on My Computer

All the “small” tunnels are done!

Now we create a tunnel inside these tunnels to keep the connection secure:

ssh -p 4000 -N -L 8080:localhost:80 -o HostKeyAlias="Server3" user@localhost

This creates a tunnel from port 80 on Server 3 to port 8080 on My Computer. Lets take a closer look:

  • -p 4000 – we start a new tunnel by connecting to port 4000 on My Computer (see port 4000 in last step above). This means that we are connecting through the tunnel we setup to Server 1 (that is connected to Server 2 – that is connected to Server 3)
  • 8080:localhost:80 – we are forwarding port 80 on Server 3 to port 8080 on My Computer
  • HostKeyAlias=”Server3″ – this is often needed since the new tunnel should authenticate with Server3 and not My Computer. Failing to provide this may lead to “Man-in-the-Middle” attack warnings and failure to log on Server 3
  • user@localhost – localhost in this case is actually Server 3 so use your Server 3 credentials here

To add more tunnels just use the last command and define new ports to forward. As soon as the “small” tunnels are setup you can reach any open port on Server 3 for a tunnel

Not easy but now at least I have something to start with 🙂

How to create and use the patch command

This very simple HOWTO will show how to use the *nix patch command

Create a patch from two files

diff -u myfile mynewfile > myfile.patch

This will give you a patch (myfile.patch) that, when applied to myfile, will make it look exactly like mynewfile. I use the -u option because I like the so called unified diff format

Apply a patch to one file

patch myfile myfile.patch

This applies the myfile.patch on the file myfile (which makes it look exactly like mynewfile from above)

Create a patch from two directories

diff -u -r dir1/ dir2/ > myfiles.patch

Creating a patch file for multiple files in different directories requires the -r option which stands för recursive i.e. diff will search through all subdirectories

Apply a patch to a directory

 patch -p0 -i myfiles.patch 

When patching multiple files in different directories we need to tell the patch command to not strip paths from patch file (which is the default behavior). This is done through the -p0 option. We also need to specify the input file with the -i option <patchfile>
NOTE! when applying a patch you should always stand one level above the topmost directory of the structure to be patched