SSH port forwarding

Isn’t SSH great? It’s secure and it can do lots of cool things, as providing access to services to local machines that are only available to the remote machines (that you can connect through SSH). This is called port forwarding.

Windows with Putty

So, you are on your local windows box and got ssh access to a remote machine, let’s call it “Remote” and from there you can access a service in another machine, let’s call it “Far”. The problem is that from your local windows box you can’t directly access “Far” (most times because the good people of network, and their strong sense of security…, vpn’s, etc).

So:
Localbox -> Remote (ok)
Localbox -> Far (not ok)
Localbox -> Remote -> Far (ok)

and it would be nice to test the service (lets say HTTP to exemplify) running on Far with your nice Localbox browser, instead of the console based Lynx browser that you have on Remote.

Enter the black magic of ssh port forwarding. With Putty (the SSH client for Windows) it’s pretty easy. Just open your connection normally, but before pressing the Open button, go to Connection -> SSH -> Tunnels:

The source port will be the port on your Localbox, i usually put there the localhost ip:port combination (127.0.0.1:80).  You should check with “netstat -an” if you have this free, if there is some service (IIS, Apache) already running on this ip:port stop it. The destination is the Far ip:port that you want to get access (far_ip:80). Click “Add”.  And open the connection normally and login to the Remote console. On the Localbox check again with “netstat -an” and you should have an entry like this

TCP    127.0.0.1:80           0.0.0.0:0              LISTENING

And there you go! You have an open tunnel from Localbox to Far. Now just open the browser on localbox and point it to 127.0.0.1, your request is being sent to Far. If you need an hostname to access the service correctly just put it on the hosts file:

127.0.0.1 hostname

Linux

Pretty easy… just with the ssh -L switch.
-L localport:foreig_ip:foreign_port

To make this clear, an example. On my production server i run a MySQL server instance, but it only listens to localhost (127.0.0.1) but i want to use a GUI to manage it. I have the GUI in my linux box, so it would be impossible to connect the GUI to the MySQL server… not with ssh around…

ssh user@mysqlhost -L 3306:127.0.0.1:3306

after the ssh connection is made i can access the MySQL server as if it was running on my Linux localhost. We can even check with netstat.

netstat -an | grep 3306 | grep LISTEN

it should get something like:
tcp    0    0    127.0.0.1:3306    0.0.0.0:*    LISTEN
tcp6    0    0    ::1:3306    :::*    LISTEN

There, a no-brainer sometimes very useful.

(My) Unix useful CLI commands

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Video

Remove the rotating bit in video metadata with ffmpeg

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Video

Remove rotation metadata from video with FFMPEG

ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=0 output.mp4

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

Mud, Sweat and Tears by Bear Grylls

Just finished reading “Mud, Sweat and Tears” by Bear Grylls. Everything comes with a cost, and he payed with lots of mud and sweat. I believe most of the tears were dropped by happiness. I liked it.

It starts really slow and dull, about his roots, from the ancestors (great grandfathers and so) to parents and sister. All the people that were influential through childhood to manhood… i understand that he wants to give a good background about himself, but it’s rather too much of it, as one actions are much more important that one background.

Anyway down the line it starts to pay off, from the SAS training (the failed first attempt to the successful second go), the parachute accident, his faith and recovery, and of course it climaxes with the climb to the summit of Everest.

It’s an inspirational book, lots and lots of details (the summit ascend alone is around 200 pages) and funny stories. It has a simple life pattern: do what you want/love, work hard, give that extra to the ordinary, take risks, and with a bit of luck it all comes together.