Mar 8, 2013

SSH: using specific keys with specific hosts

First of all, I have never thought I'd post something Unix related (not that I have anything against it; it just because I am more a Windows user :)

It just happens that for personal reasons I have some VMs running Ubuntu and from time to time I need to transfer files from my host computer to the guest, i.e, from my Windows machine to Ubuntu running on the VM. Since the virtualization solution I am using has some issues regarding file/folder sharing between host/guest OS, I am using scp to do the job.

Until today I used to have a single RSA key pair to authenticate me when connecting to my Ubuntu VM and also to github and bitbucket but this has proven to no be an optimal solution. You see, when I am copying files to Ubuntu I'd like to not be forced to type any password, after all this VM has no sensitive data and is off limits since it is not connected to the internet, but since I am using a single RSA key pair for most of my authentication  needs I don't want do leave it unprotected so every time I want to copy something to the guest OS (my Ubuntu running on the VM) I find myself typing a huge, complicated password - actually very often mistyping it :(.


Today I took the time and decided I'd find out how to setup multiple RSA key pairs and use one with no password at all to authenticate me with my VM and one with a strong password for each online service I use.


Actually it was easier than I thought ;). Basically the utilities I rely on (ssh and scp) allows one to specify which identity file (the RSA file containing your private key) (-i file_path) is to be used.


if you don't want to specify which file should be used every time or if the utility you use don't allow you to specify this file (but uses ssh behind the scenes) you can use ~/.ssh/config file!

Happy codding.

Mar 6, 2013

New types from .NET 4.5

Hi.

While experimenting with WPF programing I've just stumbled on a nice, new class in .Net 4.5 (thanks to Resharper) named CallerMemberNameAttribute.

The first thing you can figure out based on its name is that it is an attribute. It's only applicable to parameters and is meant to make developers live easier when it comes to find out the name of the caller method / property / event / etc :)

Basically you add this attribute to any parameter of type string with a default value and the compiler will do its magic.

Bellow you can see an example:

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
		
		button2.Click += delegate
		{
			Foo();
		};
	}

	private void Foo([CallerMemberName] string caller = null)
	{
		MessageBox.Show("Called from : " + caller);
	}
}

This is really useful for implementing INotifyPropertyChanged interface.

Note that this gives you only the name of the member calling the method, not the type in which this method is defined.

As any other feature developers should not abuse it. For instance, IMHO, using this feature with indexers should be avoided:

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
		
		button2.Click += delegate
		{
			MessageBox.Show( this[0] );
			MessageBox.Show( this[1] );
		};
	}

	private string this[int n, [CallerMemberName] string caller = null]
	{
		return n > 0 ? this[n-1] : caller + "(" + n + ")";
	}
}

For more information, look into the documentation.

Happy codding!