Categories
Linux

Configuring Amazon EC2 Instance with Ubuntu: Enabling remote root access

The other day, I was setting up an amazon EC2 instance for Leevio. It is a rare experience since we do not setup a new instance everyday! 😀 So, I am gonna share what I did to do what.

First, Hasin vai gave me a secret email address and a password which I used to login to the Amazon backend. There are quite a number of tabs for different services. I chose the EC2 tab. There was no instance created, so I went ahead and created a new instance. While creating it I was driven through a simple configuration wizard. It asked me to select the instance type, create some tags for the instance and choose a operating system image and finally I created network access permission. I chose Ubuntu 11.10 (the then latest one). The permission set dictates which IPs shall get access to which ports. I opened up port 80 for http connection and port 22 for SSH and SFTP. There are preset settings to open all TCP ports, UDP ports and All ports etc. You can choose the access level manually or pick one of this presets. I did manually since I wanted to learn how it worked.

Then I saved the ssh keys and secured them in a secret place on my HDD 😀 I also noted the public DNS of the instance. I typed in the following command to login to the ubuntu machine running on the instance:

The first problem I got is the “too open permission” for the key file. In fact if your key file has such a permission that other users on the same machine can access it, you can not use the key. Keys are meant to be private and make them private by issuing the following command:

Now I issued the previous command and I got in! Yay! 😀

My default username is “ubuntu”. To configure stuff, I need to be “root”. So I hit:

By default the root username doesn’t have a password. When prompted for a password, just press enter. I logged in as root. But wait, I am using a Linux machine and the root user has no password? I can’t stand that! So I decided to change the password of root account first. The process is simple:

I was asked twice the password and upon confirmation, the root account now had a super secret password which is known to only me in the whole universe 😀

The second issue we still have is that root user still can’t login via ssh. To do so, we have to enable PasswordAuthentication on the server. With my mighty root power, I installed vim on the fly and edited the heart of ssh daemon:

I navigated to the “PasswordAuthentication” section and changed it’s value from “no” to “yes”. Then I reloaded the daemon by typing:

Then I typed in these commands to exit the root shell and logout from the remote shell:

I then assigned an IP address to the instance from the IP management section and reloaded the instance. Now I can login to the server from my laptop using the command:

PS: Using the key pair to login is a more secured and recommended way of doing the stuff. But it’s easier to do stuff using remote root login.

Categories
Uncategorized

Doing magic with C# Method Extension

Say, we have a class defined in one of the assemblies shipped with a third party solution. It would be a great help if you could add a helper method to one of the objects. Unfortunately, we do not have the source code. 🙁 What do we do?

In such scenarios, .NET hasn’t left us alone in the desert 😀 The method extension technique will allow us to add our own methods to classes of our choice. Want to see an example? Take the List generics, every list of a generic type has some handy methods. Now we want to add one of ours. Printing each of the element in a list requires us to setup a foreach() loop every time. Why don’t we add an extension method named “Print()” which would print all the elements in a list – one by one? Let’s get our hands dirty!

Facts to remember:
— To extend a class and add method, we must declare a public static class with some name. The name doesn’t matter at all.
— We should define a method with our desired name in that class.
— The first parameter to the method will be “this < Object_we_want_to_extend > parameterName”. Do not forget the “this” keyword. It is the magic wand that does the magic.
— We can pass additional parameters.
— When calling the extended method from the original object, the first parameter is not required to pass. You can ignore it as if it were never there.
— For safety and security the extended method can not access private areas (fields, properties or methods).

So what do all these mean? Show me some codes! Okay, here you go:

The output shall be:

Cool, no? 😀 The code is commented as much as I thought necessary. Feel free to ask a question or submit any feedback you might come up with 🙂

Have fun implementing some extended methods on your own 🙂

Categories
Uncategorized

Implementing the Observer Pattern in C#: Code Samples

I have been reading about data binding in windows phone 7 development and got curious about how .net implements the observer pattern. I spent some time understanding the flow and decided to write a blog post about it.

.NET platform offers IObserver and IObservable interfaces to quickly get up and running with the observer pattern. We know the theory – there is an independent subject, a observable aimed towards monitoring that subject and some observers which are very keen to receive updates whenever something interesting happens to the subject. The observers know that the observable monitors the subject, so they subscribe to the observable for notifications. The observable accepts all these subscriptions, keeps them in a safe place. Then he keeps watching and watching. When something interesting happens to the subject, he quickly gets to the list of subscribers and then notifies them one by one.

Compare this scenario to the day before Eid Ul Fitr. Tomorrow is Eid if we see the moon. Now, you have decided to go to the roof and keep waiting there until the moon pops up. Knowing this, some of your friends (who are so computer freak that they don’t even want to leave their PCs to watch the moon of Eid Ul Fitr) called you and asked you to notify them if you see the moon.

Here:
The Moon == Subject
You == Observable
Your nerd friends == Observers

Here the observable keeps watching for the subject, when the subject pops up in the sky, the observable calls the observers – that’s it, simply that’s the observer pattern.

To implement this pattern, we define a Subject object. It has a counter. Every time you view the counter, it gets increased by one. We are doing this little magic to track any changes in the subject.

Then we define the SubjectObservable object which implements the IObservable. It accepts a subject in it’s constructor. This is not mandatory but we did this to track the subject in a better fashion. It provides a Subscribe() method which is called by the observers to subscribe to the observers. The method must return an IDisposable object. We do a trick and create a custom object implementing the IDisposable interface. In the constructor we accept the list of observers recorded by the observable and the instance of the observer passed for subscription. In the Dispose() method we remove the observer from the observer list. So when the Dispose() method of the disposer is called, the observer is removed from the observer list. The observable will no longer send any notifications to the removed observer.

The SubjectObserver is pretty simple. It has OnNext(), OnCompleted() and OnError() methods. The OnNext() is called by the observable to send the current state of the subject. Besides the abstract methods, we also define a constructor to accept a unique name instance and an instance of an observable to follow. When the OnNext() is called, we print out the current counter value of the subject. We also define a Dispose() method which shall invoke the Dispose() method of the disposer, removing itself from the observer list of the observable.

Well, if all these theories seem too complex to you. Do read the codes below. I have tried to add relevant comments. If you have any feedback, please do make a comment.

Okay, so every time we read the value of the counter data, the value increases. The subject is inspected by the observable and the observable passes the subject to the observers. Each observer again prints the counter and increases the value by one. This continues. 🙂