Create an alias in Windows

May 20th, 2011 | by | technology

May
20

Recently, I wanted to create shortcuts for git commands. I can have written aliases inside the git config file, but then also, I had to type git explicitly. So, I just searched the internet and came across this link: http://superuser.com/questions/49170/create-an-alias-in-windows-xp.

And I just wrote few quick aliases using the doskey in a file called ‘a.cmd‘. Now I just type ‘a‘ and press TAB and ENTER in the command prompt and set aliases quickly. I don’t want it to be automatic, so I just manually run the a.cmd whenever needed. Here is how my a.cmd looks like initially:

doskey st=git status
doskey log=git log
doskey +=git add $1
doskey co=git commit
doskey ..=cd ..
doskey bin=cd "D:\Project\Release\"
doskey src=cd "D:\Project\"

No Comments »

Passwordless SSH Using Key Based Authentication with Putty

May 13th, 2010 | by | technology

May
13

SSH (Secure Shell) is a protocol for connecting two computers securely. It provides secure authentication as well as encryption. If you use Linux, most distros have ssh installed by default.

You can log to a Linux machine using SSH protocol from a putty client by entering the username and password. But if, both the machines are yours only, and no one else uses those machines, it makes a hindrance to enter the username and password, every time you do SSH to that server.

Like most users enable the auto login feature on their machines, we want to auto login using putty also. Though, you shouldn’t do this on your office computers or Laptops. Instead of authenticating using a username and password, we will authenticate using the RSA keys.

Generating the RSA and DSA Keys for SSH

To enable Passwordless SSH Using Key Based Authentication, go to your Linux server, and open a terminal and type:

ssh-keygen -t rsa

or

ssh-keygen -t dsa

After entering the above command you will be prompted for the location to save the file. By default this will be either ~/.ssh/id_rsa or ~/.ssh/id_dsa depending on the type of key generated. Just hit the enter key to save it to the default location, or specify a different name. You will then be prompted for a passphrase. Type this in and hit the enter key; you will then be prompted to re-enter to confirm. After doing so, two files will be created: the private keyfile is the name specified (by default id_rsa or id_dsa) and the public one the same but with a .pub extension.

Now go to the ~/.ssh/ directory and do the following:

cd ~/.ssh/
cat id_dsa.pub >> ~/.ssh/authorized_keys
rm id_dsa.pub
chmod 0600 authorized_keys
cd ../
chmod 0700 .ssh

Finally, we will need to tell ssh to allow the use of keys. So, we need to edit the sshd_config file. So, use your favorite editor and edit the /etc/ssh/sshd_config file.

sudo vi /etc/ssh/sshd_config

Find and edit these lines as follows:

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys

Save your changes and reload the ssh daemon:

/etc/init.d/ssh reload
exit

Generating the Private Keys for Putty

Now, ftp the ~/.ssh/id_dsa file to your Windows box and delete that file from the Linux server.

Run the puttygen.exe application by double-clicking the file you downloaded (it does not need to be installed) and select “Import Key” from the “Conversions” menu as shown in the example screenshot below. This will open a standard Windows open dialog; locate the RSA or DSA private key file and click the “Open” button. These files are usually named something like id_rsa and id_dsa.

Import Key pair in PuttyGen

If the private key file is protected by a passphrase (highly recommended) then you will be prompted for this before the key is loaded.

The private key file is now loaded into PuttyGen as shown in the screenshot below.

PuttyGen save private key

Note the “Key Comment” by default will be something like “imported-openssh-key”. You should change this to something a little more meaningful, such as your name. To save the private key click the “Save Private Key” button and then choose a place to save it using the Windows save dialog. The saved private key will be named with a .ppk extension.

Connecting to an SSH server with the private key file

Now that the key has been generated we can run PuTTY to connect to the SSH server.

Select the “SSH -> Auth” section in the left navigation of the PuTTY configuration page as shown in the screenshot below. Then browse for the file you created above in the circled region.

PuttyGen SSH Authentication using key

Now click the “Data” option under the “Connection”. This is the highlighted blue option in the screenshot below. Then enter your auto-login name in the box which I’ve highlighted in red. In this example the login name is “pbolia”.

Putty auto login name

Now, just click the open button, and instead of asking for password, it will automatically log in using the RSA keys.

Using username "pbolia".
Authenticating with public key "pbolia"

Key is of wrong type (PuTTY SSH2 private key) Error

If you get an error message like so when you try to log in:

Trying public key authentication.
Key is of wrong type (PuTTY SSH2 private key)

Then you are attempting to connect to an SSH1 server with an SSH2 generated key. Go to the Connection/SSH options page and make sure the “Preferred SSH protocol version” option is set to either “2″ or “2 only”.

PuttyGen SSH protocol version for authentication

References:

No Comments »

How can I make bash my login shell?

May 13th, 2010 | by | technology

May
13

Every time, I do SSH to my Linux box, the default shell comes as: csh, but I want to, use the bash shell automatically from PuTTY. For making bash as the default login shell, first we need to find out, the path of the bash shell, by using:

which bash

If your login shell is csh or tcsh, and you have installed bash in /bin/bash then create a ~/.login file and add the following line:

if ( -f /bin/bash ) exec /bin/bash --login

The `–login‘ tells bash that it is a login shell.

More Information:

No Comments »

Show line numbers in vi or vim

May 11th, 2010 | by | technology

May
11

Displaying line numbers while editing a file can be handy. Using line numbers you can directly jump to a particular line. You can set the line numbers in vi using the following commands:

:set number

If you want to turn off the line numbers in vi, use the following command:

:set nonumber

You can also insert the following text into the .vimrc file, and can switch on or off the line numbers, by pressing the F6 and F7 keys respectively.

vi ~/.vimrc
nnoremap <silent> <F6> :set number<CR>
nnoremap <silent> <F7> :set nonumber<CR>

You can also find lots of vimrc recipes at: http://stackoverflow.com/questions/164847

No Comments »

Move cursor by display lines when wrapping in vi

May 11th, 2010 | by | technology

May
11

You can move your cursor by display lines when wrapping in vi, instead of jumping to the next line. This can be really helpful, when you have long wrapped lines, and you want to move in between them.

Just edit the .vimrc file.

vi ~/.vimrc

and insert the following text into that:

nnoremap <up> gk
nnoremap <down> gj
inoremap <up> <C-o>gk
inoremap <down> <C-o>gj

No Comments »

Using FTP Batch Scripts on Windows

May 8th, 2010 | by | technology

May
08

The Microsoft KB article 96269 shows you how to use the ftp program that comes with all versions of windows to automate file transfer. You can use the -s option for passing FTP script to the program.

ftp -s:script.txt

The contents of script.txt might look like this:

open ftp.myserver.com
yourUserName
yourPassword
bin
cd /files
dele file.zip
put file.zip
bye

However, if the FTP host implements automatic login, this command will not work. To turn off automatic login, use the -n switch in the command line.

No Comments »

Shell script to download file using ftp

April 13th, 2010 | by | technology

Apr
13

#!/bin/sh

# ftpget - given an ftp: style URL, unwrap it, and try to obtain the file using ftp.

if [ $# -ne 2 ] && [ $# -ne 3 ]; then
  echo "Usage: $0 ftp://... username [password]" >&2
  exit 1
fi

# Typical URL: ftp://ftp.ncftp.com/2.7.1/ncftpd-2.7.1.tar.gz

if [ "$(echo $1 | cut -c1-6)" != "ftp://" ] ; then
  echo "$0: Malformed url. I need it to start with ftp://" >&2;
  exit 1
fi

server="$(echo $1 | cut -d/ -f3)"
filename="/$(echo $1 | cut -d/ -f4-)"
basefile="$(basename $filename)"
username="$(echo $2)"
password=""
if [ $# -eq 3 ] ; then
  password="$(echo $3)"
fi

#rm $basefile
echo ${0}: Downloading $basefile from server $server

ftp -n << EOF
open $server
user $username $password
bin
get $filename $basefile
quit
EOF

if [ $? -eq 0 ] ; then
  #chmod +x $basefile
  ls -l $basefile
fi

exit 0

No Comments »

VLC: How to play videos at a faster rate?

November 9th, 2009 | by | technology

Nov
09

In this video, we will see how to play video files at a faster rate using VLC, yet maintaining the audio clarity. This could help you to play video lectures downloaded from YouTube or other video sharing websites, at a faster rate, while maintaining the audio clarity for understanding and synchronize audio with the video. This would enable you to go through a one hour lecture in about half an hour.

No Comments »

How to add “Command Prompt Here” to Nautilus/Gnome?

November 6th, 2009 | by | technology

Nov
06

Command Prompt Here: Windows developers are familiar with the tiny utility “Command Prompt Here” which allows them to open the command prompt from the windows explorer, with the working directory set to the current directory selected in the windows explorer. This PowerToy adds an “Open Command Window Here” context menu option on file system folders, giving you a quick way to open a command window (cmd.exe) pointing at the selected folder.

Open Gnome-Terminal Here

Linux and Gnome are open source software which allows great deal of options for users, to control and customize it as per their needs. Nautilus is the default file manager for Gnome, and it allows easy customization of the user interface.

Open Gnome-Terminal Here will adds an “Open Gnome-Terminal Here” context menu option on file system folders, giving you a quick way to open a gnome-terminal pointing at the selected folder. It will open gnome-terminal with the working directory set to the current directory selected in nautilus.

Installation

You need to install the Nautilus actions. Nautilus actions is an extension for Nautilus, the gnome file manager. It allow to configure program to be launch on files selected into Nautilus interface. Each time you right-click on one or several selected files in nautilus, nautilus-actions will look at its configured actions to see if a program has been setup for this selection. If it is the case, it will add an item in the menu that allow you to execute the program on the selected files.

sudo apt-get install nautilus-actions

You can also install the Nautilus actions using the Synaptic Package Manager.

Adding a new Item

Run the Nautilus actions from the System » Preferences » Nautilus Actions Configuration and add a new action item to the Nautilus context menu.

Nautilus Actions

Set the Label as: “Open Gnome-Terminal Here” for the Nautilus menu item, also give the Path as: “gnome-terminal” and Parameters as: “–working-directory=%d/%f“. The parameters for the gnome-terminal can be check in the man pages for the gnome-terminal. %d is the base directory and %f is the folder name of the selected folder in the Nautilus file manager.

Adding a new Nautilus Actions Item

Nautilus actions has a lot of parameter condition legends, so that you have fine control over the Nautilus context menu customization.

Nautilus Actions Parameters Legend

In the Conditions tab, for the “Appear if selection contains“, select the “Only folders” checkbox.

Nautilus Actions Appearance Conditions

The result

Now navigate to some folder in the Nautilus file manager and Right click on any folder, or in the free space in the Nautilus file manager, and you will see the new menu item: “Open Gnome-Terminal Here“. When you choose that menu item, the gnome-terminal will be launched with the working directory set to the current selected folder in the Nautilus file manager.

Nautilus Actions Context Menu Item

Alternatives

The same functionality can be achieved using nautilus-open-terminal package:

sudo apt-get install nautilus-open-terminal

References

No Comments »

Run Commands Effectively in Linux

November 6th, 2009 | by | technology

Nov
06

There are various ways to launch applications and run commands in Linux. Which method is best is a matter of personal taste and individual productivity needs. Here I will discuss some of the best methods available in Linux to do the common quotidian tasks easier and faster.

Command Prompt

The basic method of running commands is through the gnome-terminal, i.e. the command prompt in the Windows language. To run gnome-terminal, just press ALT+F2, type “gnome-terminal” and press the enter key.

gnome-terminal

Similarly, you can run other applications, like “gnome-system-monitor” the task manager equivalent of windows, “gnome-control-center” the control panel equivalent of windows, by using the Run Application dialog box. In the terminal window, you can run any command, redirect the output to a file, run commands from command history, etc.

n>&m – Swap Standard Output and Standard Error

You can redirect the output of a console application to any file descriptor like:

command > output.txt

This will create a new file and send the output to that file.
If you want to append the output to an existing file, like your logs file:

command >> logs.txt

By default the output (cout) goes to the file and the error (cerr) goes to the screen, by using this way.
But there may be situation where you want to run your program automatically and logs all standard error to a file and discard the standard output, means send the error to a file and normal output to the screen. In the Bourne shell you can do that using redirection.

command 2> errorlogs.txt

File descriptors 0 means standard input, 1 means standard output and 2 means standard error. There are other file descriptors from 3 to 9 used as temporary descriptors.
When using pipes, you need different way, as pipes doesn’t have names. You can say point 2 to where 1 is pointing, like:

command 2>&1 |

This means send the standard error to where the standard output is going.
To swap the standard output and the standard error you need to say:

command 3>&2 2>&1 1>&3 |

Think this as swapping a and b using temporary temp:

temp = b;
b = a;
a = temp;

Command History

Command Description
history List the last 16 commands
history -c Clears the commands history
fc -l 20 30 List commands 20 through 30
fc -l -5 List the last five commands
fc -l cat List the last command beginning with cat
fc -ln 5 > doit Save command 5 to file doit
fc -e vi 5 20 Edit commands 5 through 20 using vi
fc -e emacs Edit previous command using Emacs
!! Reexecute previous command
!cat Reexecute last cat command
!cat foo-file Reexecute last command, adding foo-file to the end of the argument list
!N Reexecute Command number N in history list.
!-N Reexecute Nth command back from current command.
!$ Last argument of previous command.

Command Alias

You can also define command short aliases by adding the alias in the ~/.bashrc file.

gedit ~/.bashrc

and add a line as:

alias ll='ls -l'
alias sysmon='gnome-system-monitor'
alias ins='sudo apt-get install'

You can also run the alias command in the terminal window to create an alias for that session only:

alias h='history'
unalias h

You can also use variables in the aliases like:

alias cdo="cd \"\$OLDPWD\""

Note: it is important, that there are ONLY double quotes in the expression above, no single quotes like in the other examples!

Global Hotkeys

You can also create hotkeys for the most commonly used programs by following the steps:

  • Launch gconf-editor by typing in a terminal: gconf-editor.
  • In gconf-editor choose apps » metacity » global_keybindings.
  • In global_keybindings assign keyboard shortcuts.

    Assign keys for global keybindings

  • Select keybinding_commands (right below global_keybindings in the metacity menu).
  • In keybinding_commands assign the custom commands you would like to launch with the keyboard shortcuts you created in step three.

    Assign application to global keybindings

Gnome-do

You can use gnome-do, this is similar to QuickSilver (Mac) or Launcy/wxQuickRun (Windows). Install the program and the plugins using the Synaptic Package Manager.

gnome-do

Just launch the program, and press “Win/Command key + Space Bar” and type your commands. Use the arrows keys for selecting various items, and the tab key to move to a different pane. You can also download additional plugins from the gnome-do repository and copy them to “~/.local/share/gnome-do/plugins“.

Dock Window

Ever like the cool Mac GUI effects and the docking window to launch your favorite application without searching through the maze of menus. Well now we have lots of docking managers for Linux also namely: Avant Window Navigator, Cairo Dock, wbar, etc.

Avant Window Navigator

wbar

You can also imitate OSX GUI in your Linux desktop, by changing the Gnome themes.

Gnome Desktop

SUSE Linux Favorite Application

SUSE Linux Enterprise Desktop 10 uses a desktop menu to help you launch applications. This menu has three sections: Favorite Applications, Recently Used Applications, and Recent Documents. You can add or remove any application to your favorite application section. This allows easy access to the commonly used application.

SUSE Linux Favorite Application

References

No Comments »