Thursday May 21, 2009

The SSH daemon i.e. sshd didn't come pre-installed in my Ubuntu Linux 8.10 distribution. As a result, I could not SSH into my machine from another terminal. Therefore, I had to install and configure it as per the following steps:

Install SSH with apt-get

sudo apt-get install ssh

The service can be started using the init scipts as below:

sudo /etc/init.d/vsftpd <start | stop >
and the
sudo /etc/init.d/ssh <start | stop >

However, it is more convenient to use the Run-level configuration tool  for init scripts (sysv-rc-conf). You can control the background services and enable/disable the start of various applications at different run levels by clicking a bunch of check-boxes on a single page. This could bring down your OS boot up time as well by disabling unnecessary services that aren't used frequently (printers, rsync, bluetooth, etc).

Install the sysv-rc-conf tool

sudo apt-get install sysv-rc-conf

Run the application

sudo sysv-rc-conf

The SSH service can be configured by modifying the check boxes associated with it accordingly. Similarly, you can identify and configure additional unneeded services. Please note the initial default values for any service that you may opt to change. Also, in order to completely disable a particular service, you will need to remove all the Xs appearing for that service across differnt run levels.




Bookmark and Share

Sunday Apr 26, 2009

Libssh2 is a php extension that allows ssh connections to a remote machine in order to execute shell commands or even access the remote shell. Subsequently, the output from these commands can be displayed in a browser, as required. Evidently, it can be convenient to use it in various scenarios, where such a need may arise.

It's official website defines Libssh2 as:

libssh2 is a library implementing the SSH2 protocol as defined by Internet Drafts: SECSH-TRANS, SECSH-USERAUTH, SECSH-CONNECTION, SECSH-ARCH, SECSH-FILEXFER, SECSH-DHGEX, SECSH-NUMBERS, and SECSH-PUBLICKEY.

Many a times, we are faced with situations where we do not have root privileges on a unix box, and hence we are unable to install tools in their default path (eg. /usr/local/bin, /usr/local/include). As a result, we have to customize the installation by providing a non-standard location (eg. ~/$HOME), and providing additional flags for procedures like configure, build and install.

In this article, we have listed the steps involved in installing the libssh2 library on Solaris Express Community Edition. However, the same set of steps will work on any other unix distro, with minor changes.

Install libssh2
cd /tmp
wget http://voxel.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.18.tar.gz
tar xvf libssh2-0.18.tar.gz
cd libssh2-0.18

Since, the /usr/local directory is "read-only", we'll provide a prefix in the configure step

-bash-3.2# ./configure --prefix=/opt/install

 -bash-3.2# make all install

Install PECL ssh2

-bash-3.2# wget http://pecl.php.net/get/ssh2-0.10.tgz
-bash-3.2# tar xvf ssh2-0.10.tar
x package.xml, 3091 bytes, 7 tape blocks
x ssh2-0.10/config.m4, 2149 bytes, 5 tape blocks
x ssh2-0.10/ssh2.c, 43225 bytes, 85 tape blocks
x ssh2-0.10/php_ssh2.h, 5397 bytes, 11 tape blocks
x ssh2-0.10/ssh2_fopen_wrappers.c, 36414 bytes, 72 tape blocks
x ssh2-0.10/ssh2_sftp.c, 22321 bytes, 44 tape blocks

Patch the ssh2.c file
-bash-3.2# cd ssh2-0.10
-bash-3.2# vi ssh2.c

Seach for
#if LIBSSH2_APINO < 200412301450
Change it to as followings
#if LIBSSH2_VERSION_NUM < 0x001000
Save the file and run

From the ssh2-0.10 directory, run

-bash-3.2# /usr/php/5.2/bin/phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519


-bash-3.2# env CPPFLAGS="-I/opt/install/include" LDFLAGS="-L/opt/install/lib" ./configure --with-ssh2=/opt/install/lib --with-php-config=/usr/php/5.2/bin/php-config --prefix=/opt/install

-bash-3.2# make

-bash-3.2# make install
Installing shared extensions:     /var/php/5.2/modules/

Add the entry for the ssh2 extension in php.ini
vi /etc/php/5.2/php.ini
under Dynamic Extension, add
extension=ssh2.sos

Restart the apache service
svcadm restart apache22

Check if the service started successfully
svcs apache22

Bookmark and Share

Thursday Apr 23, 2009

Amazon describes the functionality provided by EBS as follows:

“Amazon Elastic Block Store (EBS) provides block level storage volumes for use with
Amazon EC2 instances. Amazon EBS volumes are off-instance storage that persists
independently from the life of an instance. Amazon Elastic Block Store provides highly
available, highly reliable storage volumes that can be attached to a running Amazon EC2
instance and exposed as a device within the instance. Amazon EBS is particularly suited for
applications that require a database, file system, or access to raw block level storage.”

 In this article, we  will list all the necessary steps involved in creating new EBS volumes and attaching them to an existing instance. The following commands were run on an OpenSolaris 2008.11 instance. The disk device name may change in other AMIs.

Set the Environment Variables on local machine
export EC2_HOME=/opt/ec2
export PATH=$PATH:$EC2_HOME/bin
export EC2_PRIVATE_KEY=$EC2_HOME/keys/pk-************.pem
export EC2_CERT=$EC2_HOME/keys/cert-************.pem
export JAVA_HOME=/usr/jdk/latest
export EC2_URL=https://ec2.amazonaws.com

Note: Replace the values of the environment variables accordingly.


Start an OpenSolaris 2008 Instance 
ec2-run-instances ami-7db75014 -k prateek-keypair

Login to the Instance
ssh -i /PATH/TO/keypair-file root@ec2-XX-XXX-XXX-XXX.compute-1.amazonaws.com
Note: Replace the value of the public host name of the above instance accordingly.


Create a keys directory
mkdir /opt/ec2/keys

Transfer your Private key and certificate to the Instance from local machine
scp -i /PATH/TO/keypair-file /PATH/TO/KEYS/*pem root@root@ec2-XX-XXX-XXX-XXX.compute-1.amazonaws.com:/opt/ec2/keys

Set the Environment Variables on the EC2 instance

export EC2_HOME=/home/EC2/ec2-api-tools-1.3-19403
export PATH=$PATH:$EC2_HOME/bin
export EC2_PRIVATE_KEY=/home/EC2/ec2-api-tools-1.3-19403/keys/pk-************.pem
export EC2_CERT=/home//EC2/ec2-api-tools-1.3-19403/keys/cert-************.pem
export JAVA_HOME=/usr/jdk/latest
export EC2_URL=https://ec2.amazonaws.com

Create a new EBS Volume
The following command can be repeated as needed to create additional volumes.
ec2-create-volume --size 128 --availability-zone us-east-1a
VOLUME  vol-b1dc3fd8    128             us-east-1a      creating        2009-04-22T01:23:00+0000

ec2-describe-volumes vol-b1dc3fd8
VOLUME  vol-b1dc3fd8    128             us-east-1a      available       2009-04-22T01:23:00+0000

You can list all the volumes along with their status as:
ec2-describe-volumes | cut -f2,3,5,6

vol-7208ed1b    2       us-east-1c      available
vol-99dc3ff0    128     us-east-1a      available
vol-0f2ecb66    1       us-east-1a      available
vol-b1dc3fd8    128     us-east-1a      available

Attaching Volumes to an OpenSolaris EC2 Instance
The command for attaching volumes to an instance is:
ec2-attach-volume -i <instance-id> -d <device-id> VOLUMENAME

In Solaris, devices are named from 0 to 23. Device number 0 and 1 are used for ephemeral storage. Therefore, we can use the devices starting from 2 until 23.
I have attached 2 volumes to the current instances below:

ec2-attach-volume -i i-ed573f84 -d 3 vol-6123c008
ec2-attach-volume -i i-ed573f84 -d 4 vol-b1dc3fd8

The list of the attached volumes can be displayed as:
ec2-describe-volumes | grep attached

ATTACHMENT      vol-99dc3ff0    i-ed573f84      2       attached        2009-04-22T02:46:24+0000
ATTACHMENT      vol-6123c008    i-ed573f84      3       attached        2009-04-22T02:47:22+0000
ATTACHMENT      vol-b1dc3fd8    i-ed573f84      4       attached        2009-04-22T02:47:57+0000
ATTACHMENT      vol-6e23c007    i-ed573f84      5       attached        2009-04-22T02:48:16+0000

Using EBS volumes from the Solaris Instance
You can view the mapping of the Solaris devices to the EBS volumes that we created earlier.

NOTE: Use c4d*p0 for OpenSolaris 2008.05 (ami-41e70328 )and c0d*p0 for SXCE Build 79 32 bit image (ami-eb7a9f82)

ls -l /dev/dsk/c3d*p0
lrwxrwxrwx 1 root root 26 2008-12-14 23:32 c3d0p0 -> ../../devices/xpvd/xdf@0:q
lrwxrwxrwx 1 root root 26 2008-12-22 18:54 c3d1p0 -> ../../devices/xpvd/xdf@1:q
lrwxrwxrwx 1 root root 26 2009-04-22 02:46 c3d2p0 -> ../../devices/xpvd/xdf@2:q
lrwxrwxrwx 1 root root 26 2009-04-22 02:47 c3d3p0 -> ../../devices/xpvd/xdf@3:q
lrwxrwxrwx 1 root root 26 2009-04-22 02:47 c3d4p0 -> ../../devices/xpvd/xdf@4:q
lrwxrwxrwx 1 root root 26 2009-04-22 02:48 c3d5p0 -> ../../devices/xpvd/xdf@5:q
lrwxrwxrwx 1 root root 26 2008-12-14 23:32 c3d6p0 -> ../../devices/xpvd/xdf@6:q

Now, you can use these EBS volumes just like you would use any other attached storage device. 

Bookmark and Share

Wednesday Apr 15, 2009

I am sure every software developer has come across the vi editor a few times. Although, the initial learning curve can be a little steep, the flexibility and power offered by vi makes it a worthy programming tool. Moreover, you are guaranteed to find it on any unix box that you log into. Following are some of the commands that I have found to be extremely useful during my development work. 

Find the match of current parenthesis, brace, or bracket

%

Working with Multiple Windows
Open another file in VI in the existing window

:sp filename
Tile widows horizontally or vertically
:windo wincmd H or K
Move cursor between the files
Ctrl + W + W
Alternatively, you can move cursor to right or left window
Ctrl W followed -> or <-


Search forward for the next identifer under the cursor

*

Search backward for the previous identifer under the cursor

#

Search and Replace

:%s/word1/word2/g
replaces each occurrence of word1 with word2 in the current file

You can also perform this operation on a part of the file (like a method) by selecting the specific text portion of the file ( with v in command mode) and
:s/word1/word2
Alternatively, use markers to perform the same operation as shown below:
Move to the first line of the method
ma
will mark the current line as a

Move to the end of method
mb
will mark the last line of method as b

Finally,
:'a,'bs/word1/word2/g
will only make the changes in the lines between the markers a and b

Execute any shell command by prepending it with a :! in command mode

:!<command>
This is particularly useful if you want to compile a program without exiting the editor. For java, use
:!javac %
% denotes the name of the current file (eg. Sample.java)


Access the shell without exiting the editor

:sh
Use Ctrl+D to return to the same line from where you entered the shell.

Move to a specific line in the file

:line_number
For example,
:32
will take you to line number 32
Use :$ to move to the last line and
:0 to move to the first line

The same result can be achieved by entering
linenumber or $ or 0 followed by (not simultaneously) G (capital G)

Use Ctrl+G to find out the current line and total number of lines in the current file

Repeat the last command
Also, in Vi, typing in numbers before a command repeats that command. Therefore, 100. will repeat the last command 100 times. For instance, 5dd will delete the next 5 lines.

Indentation
Use >> and << to indent lines to the right or left respectively. Multiple lines can also be indented by selecting the portion of text with v in command mode followed by << or >>


Undo/Redo

u
will undo the last change while
U

will undo all changes in the current line
Ctrl + R performs the redo operation. You can complement this with the undo last command operation to arrive at the oldest change.

Plugins

Additionally, I'd like to point out a couple of plugins that can be extremely beneficial, especially if you are familiar with IDEs like NetBeans and Eclipse.

SuperTab
Allows you to do all your insert-mode completion with Tab. More information about this plugin can be found at:
http://www.vim.org/scripts/script.php?script_id=1643

Tag List
Provides a source code browser for the popular languages lik C, C++, Java, PHP, Perl, Python, etc). Fore detailed information, visit:
http://vim-taglist.sourceforge.net/
Note that this plugin requires the exuberant ctags utility.

Resources:

Interview with Bill Joy

VIM Reference Card

VIM Graphical Cheat Sheet and Tutorial

vi on Uncyclopedia, the content free encyclopedia

Bookmark and Share

Wednesday Apr 08, 2009

I came across this program in Peter Van Der Linden's excellent book "Expert C Programming: Deep C Secrets". And, it's solution is quite amusing. 

foo(const char **p) { } 
 
main(int argc, char **argv) 
{
foo(argv); 
}

Interestingly, the above program gives the following warning message on compilation:

line 5: warning: argument is incompatible with prototype

Moreover, one would expect it to work knowing that char *s matches with const char *p as evident throughout the library string functions.  So, why wouldn't

char **argv match const char **p ?

The answer to the above question lies in certain parts of 3 sections in ANSI C Standard.

The Constraints portion of Section 6.3.2.2 of the ANSI C Standard includes the phrase:

Each argument shall have a type such that its value may be assigned to an object with the unqualified
version of the type of its corresponding parameter.

 
  
 
  

and the Constraints portion of Section 6.3.16.1 of the ANSI C Standard includes the phrase:

Both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right.

implies that the passing of arguments should behave as an assignment and allows the following code to compile without any warnings

char *s;
const char *p;
p = s;

Observe, that the left operand p is a pointer to a const qualified character, the right operand is a pointer to an unqualified character, and it has all the qualifiers of the type pointed to by s (zero), and an additional one i.e. const

For the same reason, something like

s = p;

will generate a compilation warning, as the left operand has less qualifiers than those on the right.

Subsequently, one can wrongly apply the above logic to char **argv and const char **p. The following example is included in Section 6.1.2.5

The type designated "const float *" is not a qualified type—its type is "pointer to const-qualified float" and is a pointer to a qualified type.

Therefore, const char **p is a pointer to an unqualified type, and p's type is a pointer to a pointer to a qualified type. And, p and argv are pointers to unqualified types that are of different types, and hence are incompatible. Therefore, char **argv does not match with const char **p and disregards the constraint in Section 6.3.2.2 resulting in the display of the "incompatible types " warning during compilation.



Bookmark and Share

Tuesday Apr 07, 2009

I recently decided to setup Ubuntu 8.10 (codename - Intrepid) on my Toshiba Tecra M3 laptop, and was pleasantly surprised to discover how easily Ubuntu detected the display and wireless drivers. Moreover, I had my favorite browser (Firefox) and mail client (Thunderbird) already bundled with the OS. However, I was frustrated to discover that the Firefox browser didn't display the websites containing java applets with the default settings, even if you have the updated JRE installed. After a few searches and referring half a dozen websites, I found out that Firefox was using Java Iced Tea (OpenJDK) to render applets. This seemed like the primary source of the problem.

 You can check the java version installed on your system as below:

java -version

Bear in mind, that the instructions mentioned here have only been tested on Ubuntu 8.10, and it may not work on other distributions.

You can check the specifics of your distribution as:

cat /etc/lsb-release

Therefore, I went ahead and installed the jdk 6 package and java 6 plugin from the Ubuntu repository

sudo apt-cache search java
sudo apt-get install jdk-6
sudo apt-get install java6-plugin

Finally, you need to create a soft link for the java plugin in the firefox plugin directory as shown below

sudo ln -s /usr/lib/jvm/java-1.5.0-sun/jre/plugin/i386/ns7/libjavaplugin_oji.so /usr/lib/firefox/plugins/libjavaplugin_oji.so

Now, since, you might have more than one java packages installed on your system, you will have to suggest Ubuntu to use the newly downloaded java 6 as shown below

 sudo update-alternatives --config java

And, Voila! It's done. This will, hopefully, get your firefox browser to recognize and display java applets correctly.

Bookmark and Share

Wednesday Nov 05, 2008


[Read More]
Bookmark and Share

This blog copyright 2009 by Prateek Parekh