Monday 13 December 2010

using a remote machine as a proxy with SSH

This is such a useful feature of SSH that I wish I had know about before now.

When working remotely from an insecure location, or when only the remote location is trusted by another host, e.g for a firewall login or viewing intranet pages you only have access to from a certain machine; you can use a remote machine as a SOCKS proxy straight out of the box.

e.g. on the localmachine, run:

ssh -fND 1234 username@yourremotemachine

This will create a SOCKS proxy on localhost port 1234
-f flag tells the shell to fork and run in the background
-N Leaves no login shell
-D 1234 sets up a SOCKS proxy via port 1234 on the localhost

On the local machine you can then set the browser to use the SOCKS 5 proxy on localhost port 1234

Tuesday 2 November 2010

diff files across servers

Here's another really handy one liner for diff-ing files across servers using process substitution. This seems to work in zsh and bash.

diff /a/file/ <(ssh anotherserver 'cat /a/file/')

Tuesday 19 October 2010

Finding the most recently modified files with find

I recently needed to check that no files had been modified since I last made an archive of a certain directory. Here's a good one for geting a list of files by last modified date, newest at the top.

find . -type f -printf "%TY-%Tm-%Td %TT %pn\n" | sort -r | less

Tuesday 24 August 2010

ctrl-a and ctrl-b not working in tmux on RHEL 5.5

Rather than moving to the beginning of a line and the end of a line, ctrl-a and ctrl-b were just print ^A and ^B to the screen. Not sure why, I don't really have time to investigate, but this seems to fix the issue when run within the tmux session.

set -o emacs

Wednesday 18 August 2010

MySQL Grants grant and revoke

I'm not really a fan of MySQL but here is a simple problem I always forget the syntax for which is really a SQL issue.

An installation of MediaWiki has an update script which requires the DROP privilege on the database, so I wanted to add the DROP privilege, run the update script and revoke the DROP privilege again.

Here's how:

mysql> show grants for 'username'@'host';
mysql> grant drop on adatabase.* to 'username'@'host';
mysql> revoke drop on adatabase.* from 'username'@'host';

Job's a good-un!

Tuesday 20 July 2010

Simple SSH Tunneling

Today I had a problem where the office firewall would not let out traffic to the fibs.com server on port 4321 (Shame on them!)

Luckerly a remote machine comes in handy. I then just tell my Fibs client to connect on localhost port 4444

ssh -f -L 4444:fibs.com:4321 aremotemachine -N

Wednesday 17 March 2010

In-place file editing with sed

This took me a while to work out why this would not work. When using the -i option with sed you need to specify a backup extension to be used (at least on my 10.5 mac you do!)

e.g.

sed -i '' -e 's/something/something else/g' -e 's/again/again again/g' *.py

Monday 4 January 2010

Nested List Comprehension

I never really feel like i'm any good at using Python unless I am doing cool things with List Comprehension.

Here's a quick example of nested list comprehension I used today to create a new list from a list containing lists of three items

Visually:-

[['mostly cloudy (day)', 'cloudy3.png', 'cloudy3.png'],
['partly cloudy (night)', 'cloudy2_night.png', 'cloudy2_night.png'],
['partly cloudy (day)', 'cloudy2.png', 'cloudy2.png'],
....................................
['Sunny', 'sunny.png', 'sunny.png']]

To this:-

[['mostly cloudy (night)', 'cloudy3_night.png'],
['mostly cloudy (day)', 'cloudy3.png'],
['partly cloudy (night)', 'cloudy2_night.png'],
....................................
['Sunny', 'sunny.png']]

The code then:-

[[a[0],a[1]] for a in theList]