Tuesday, January 24, 2012

15 Examples to Browse Google From Command Line Prompt


Goosh (the unofficial Google shell) provides command line interface on the web for several Google services to keep the command line junkies happy.
Linux users who are comfortable with command line prompt, will love to surf the net from command line using Goosh.
Goosh allows you to quickly search Google web, images, news and Wikipedia search. The output is presented in a very nice readable command line environment.
For the impatient, go to Goosh prompt, and type “web {keyword}” at the prompt to search Google from the command line.
Once you like the basic output come back to this article and figure out how you can use Goosh effectively using the 15 examples given below.

1. Google web search from Command Line Prompt

Following is the syntax for the search.
Syntax: [web|search|s|w] keywords
Start the search using s followed by the keyword as shown below. This will gives you the top most 4 URL as result.
guest@goosh.org:/web> s the geek stuff
To get more search results do one of the following.
guest@goosh.org:/web> more

(or)

guest@goosh.org:/web> {Enter-Key}
2. View Google Map from Command Line
Goosh command ‘place’ is used to do a google map search from the goosh prompt. Following is the syntax to do the search in google maps.
Syntax: place/places/map/p address
Fig: Locate Address in Google Map
Fig: Locate Address in Google Map
3. Search Wikipedia from Command Prompt
Use wiki command to search the Wikipedia website as shown below. Following example will search for geek in wikipedia and return the output.
syntax: [wiki|wikipedia] keyword

guest@goosh.org:/web> wiki geek

4. Open Website in Browser from Command Line

Following example will open http://www.thegeekstuff.com
syntax: go/g URL

guest@goosh.org:/web> go www.thegeekstuff.com

5. Perform Calculations From Command Prompt

Use Calc for calculations as shown below.
Syntax: calculate/calc

guest@goosh.org:/web> calculate 100+1
100+1 = 101

guest@goosh.org:/web> calculate 124/5
124/5 = 24.8

6. Search Google news from Command Prompt

Goosh command ‘news’ is to read news on your favourite topic from the goosh prompt. Following is the syntax for reading Google news.
The following example provides the latest news about Michael Jackson.
Syntax: news/n KEYWORD

guest@goosh.org:/web> news michael jackson

7. Translate a word in Google Translate from Goosh Prompt

Goosh provides a command to translate a word from one language to another language.
Following example translates the word “Good Morning” from English to Spanish.
Syntax: translate/trans/t [lang1] [lang2] STRING

guest@goosh.org:/web> translate en es good morning
translating "good morning" from english to spanish:

"buenos días"

guest@goosh.org:/web>

8. I’m Feeling Lucky – Get the 1st Search Output Always from Command Line

Simulate the “I’m Feeling Lucky” Google button behavior from command line as shown below. The example below will open the 1st results for Paris Hilton automatically.
Syntax: lucky/l searchkeywords

guest@goosh.org:/web> lucky paris hilton

9. More: Get more results, that is the next results set page.

Goosh command “more” gives next set of URLs for your last search which is very useful when you want to go beyond the next 4 results.
syntax: more/m

guest@goosh.org:/web> more

10. Search Google Images From Command Prompt

Use Goosh command images as shown below.
Syntax: images/image/i searchword
The following example shows the 1st output of the image for search “taj mahal”
Fig: Google Image Search
Fig: Google Image Search

11. Google blogs: Search in blogs using Google blog search.

Goosh command “blogs” in goosh is used to do search in blogs using google blogs search. This will display the list of blogs URL which matches the given keyword.
Syntax: blogs/blog/b blogsearchwords

12. Search YouTube Videos from Command Prompt

Goosh provides a way to do a google video search using command “video”. The following example shows how to search for Susan Boyle’s YouTube video
Syntax: video/v KEYWORDS

guest@goosh.org:/web> video susan boyle
Fig: Search Youtube from Command Prompt
Fig: Search Youtube from Command Prompt

13. All available Goosh commands

Goosh command “ls” command is used to list out all the available goosh commands.
Syntax: ls

guest@goosh.org:/web> ls
web         news      more      blogs      read      feeds
place       translate images    video      clear
wiki        help      cd        site       open
go          lucky     ls        addengine  load
calculate   settings  gmail     login      logout
Help command gives more details about a specific goosh command as shown below.
ramesh.thegeekstuff@goosh.org:/web> help settings
help: settings

command aliases parameters function
settings (set) [name] [value] edit settings

examples:
set lang de - sets language to german
set lang - displays value of lang-setting
settings - displays all settings
settings reset - reset all settings to default values

14. View and Edit Goosh Settings

View the current Goosh settings as shown below.
guest@goosh.org:/web> settings
name value default help
lang en en google default language
results 10 4 number of results for google-searches (1..100)
timeout 4 4 timeout for ajax requests in seconds (1..100)
style.bg #FFFFFF #FFFFFF goosh background color
style.fg #000000 #000000 goosh font color
style.hl #009900 #009900 goosh highlight color
style.sh #666666 #666666 goosh 'shaded' color
style.link #0000CC #0000CC goosh link color
style.vlink #551a8b #551a8b goosh visited link color
place.width 300 300 width of map image (20..600)
place.height 150 150 height of map image (20..500)
To change the default number of results shown from 4 to 10 use the following command.
guest@goosh.org:/web> settings results 10
results is set to "10".

15. Access Gmail from Command Prompt

When you enter login command as shown below, it will forward you to Google accounts page to enter your authentication to login to Gmail.
Syntax: login

guest@goosh.org:/web> login
Once you enter this command, it loads Gmail login page. You just enter your gmail account details (username and password) and hit enter which will log you into gmail.
If the provided credentials are correct, it provides you a Goosh prompt with your account name in it. This indicates that you’ve successfully logged in to gmail from command prompt.
ramesh.thegeekstuff@goosh.org:/web>
Goosh command “gmail” is for you to read and write in gmail. It provides you to access your gmail account in google shell itself. This is a mobile version of the gmail embedded for easy display.
Syntax: gmail

ramesh.thegeekstuff@goosh.org:/web> gmail

10 Awesome Examples for Viewing Huge Log Files in Unix


Viewing huge log files for trouble shooting is a mundane routine tasks for sysadmins and programmers.
In this article, let us review how to effectively view and manipulate huge log files using 10 awesome examples.

Example 1: Display specific lines (based on line number) of a file using sed command

View only the specific lines mentioned by line numbers.
Syntax: $ sed -n -e Xp -e Yp FILENAME
  • sed : sed command, which will print all the lines by default.
  • -n : Suppresses output.
  • -e CMD : Command to be executed
  • Xp: Print line number X
  • Yp: Print line number Y
  • FILENAME : name of the file to be processed.
The example mentioned below will print the lines 120, 145, 1050 from the syslog.
$ sed -n -e 120p -e 145p -e 1050p /var/log/syslog
In the following example,  you can view the content of var/log/cron from line number 101 to 110.
  • M – Starting line number
  • N – Ending line number
Syntax: sed -n M,Np FILENAME

$ sed -n 101,110p /var/log/cron

Example 2: Display first N lines of a file using head command

This example displays only first 15 lines of /var/log/maillog file. Change 15 to 10 to display the first 10 lines of a log file.
Syntax: head -n N FILENAME

$ head -n 15 /var/log/maillog

Example 3: Ignore last N lines of a file using head command

This example shows how to ignore the last N lines, and show only the remaining lines from the top of file.
The following example will display all the lines of the /var/log/secure except the last 250 lines.
Syntax: head -n -N FILENAME

$ head -n -250 /var/log/secure

Example 4: Display last N lines of the file using tail command

This example displays only last 50 lines of /var/log/messages file. Change 50 to 100 to display the last 100 lines of the log file.
Syntax: tail -n N FILENAME

$ tail -n 50 /var/log/messages

Example 5: Ignore first N-1 lines of the file using tail command

This example shows how to ignore the first N-1 lines and show only the remaining of the lines.
The following example ignores the 1st four lines of the /etc/xinetd.conf, which contains only the comments.
Syntax: tail -n +N FILENAME

$ tail -n +5 /etc/xinetd.conf
defaults
{
        instances               = 60
        log_type                = SYSLOG authpriv
        log_on_success          = HOST PID
        log_on_failure          = HOST
        cps                     = 25 30
}
includedir /etc/xinetd.d

Example 6: View growing log file in real time using tail command

This is probably one of the most used command by sysadmins.To view a growing log file and see only the newer contents use tail -f as shown below.
The following example shows the content of the /var/log/syslog command in real-time.
Syntax: tail -f FILENAME

$ tail -f /var/log/syslog

Example 7: Display specific lines (based on line number) of a file using head and tail command

The example below will display line numbers 101 – 110 of /var/log/anaconda.log file
  • M – Starting line number
  • N – Ending line number
Syntax: cat file | tail -n +N | head -n (M-N+1)

$ cat /var/log/anaconda.log | tail -n +101 | head -n 10
  • cat : prints the whole file to the stdout.
  • tail -n +101 : ignores lines upto the given line number, and then start printing lines after the given number.
  • head -n 10 : prints the first 10 line, that is 101 to 110 and ignores the remaining lines.

Example 8: Display lines matching a pattern, and few lines following the match.

The following example displays the line that matches “Initializing CPU” from the /var/log/dmesg and 5 lines immediately after this match.
# grep "Initializing CPU#1" /var/log/dmesg
Initializing CPU#1
[Note: The above shows only the line matching the pattern]

# grep -A 5 "Initializing CPU#1" dmesg
Initializing CPU#1
Calibrating delay using timer specific routine.. 3989.96 BogoMIPS (lpj=1994982)
CPU: After generic identify, caps: bfebfbff 20100000 00000000 00000000
CPU: After vendor identify, caps:  bfebfbff 20100000 00000000 00000000
monitor/mwait feature present.
CPU: L1 I cache: 32K, L1 D cache: 32K
[Note: The above shows the line and 5 lines after the pattern matching]
As explained in our previous grep command article, the following operations are possible.
  • Viewing specific lines identified by patterns, which is grep’s default functionality.
  • Viewing only the matched characters.
  • Viewing N lines after the match with -A option.
  • Viewing N lines before the match with -B option.
  • Viewing N lines around the match with -C option.

Example 9: Displaying specific bytes from a file.

The following example explains how to display either the top 40 or the last 30 bytes of a file.

Display first 40 bytes from syslog.

$ head -c40 /var/log/syslog

Display last 30 bytes from syslog.

$ tail -c30 /var/log/syslog

Example 10: Viewing compressed log files

After a specific time all the system log files are rotated, and compressed. You can uncompress it on the fly, and pipe the output to another unix command to view the file as explained below.
  • Display the first N lines of a compressed file.
    $ zcat file.gz | head -250
  • Display the last N lines of a compressed file.
    $ zcat file.gz | tail -250
  • Ignoring the last N lines of a compressed file.
    $ zcat file.gz | head -n -250
  • Ignoring the first N lines of a compressed file.
    $ zcat file.gz | tail -n +250
  • Viewing the lines matching the pattern
    $ zcat file.gz | grep -A2 'error'
  • Viewing particular range of lines identified by line number.
    $ zcat file.gz | sed -n -e 45p -e 52p

Monday, January 23, 2012

How to View, Modify and Recreate initrd.img


Question: How do I view, modify and recreate the new initrd.img on Ubuntu, Debian, CentOS, Fedora, Red-Hat, Arch Linux, or SUSE distributions?

1. How To View Content Of initrd.img file?

initrd.img is in gzip format.  So move initrd.img to initrd.gz as shown below.
# cp /tftpboot/el5/initrd.img  .

# ls
cdrom   initrd.img

# mv initrd.img initrd.gz
Unzip the initrd.gz file as shown below.
# gunzip initrd.gz

# ls
cdrom  initrd
After unziping the initrd.gz file, the initrd is further in cpio ‘newc’ format. So extract the files from initrd using cpio ‘newc’ format as shown below.
Note: info cpio will give more information about ‘newc’ format.
# mkdir tmp2

# cd tmp2/

# cpio -id < ../initrd
16524 blocks
Now you can view the content of initrd.img file
# ls
bin  dev  etc  init  modules proc  sbin  selinux  sys  tmp  var

2. How To Modify Content of Image and Recreate New Image?

After extracting the file as shown below, make appropriate modification to any of those files. Then pack the files back into the archive using the following commands. Pack the modified files back to cpio ‘newc’ format.
# find . | cpio --create --format='newc' > /tmp/newinitrd
16524 blocks

# ls /tmp/
cdrom  initrd  newinitrd  tmp2

# ls -l /tmp/newinitrd
-rw-r--r-- 1 root root 8460288 Jul  2 14:50 /tmp/newinitrd
Gzip the archive file.
# gzip newinitrd

# ls
cdrom  initrd  newinitrd.gz  tmp2

# ls -l newinitrd.gz
-rw-r--r--  1 root root 6649867 Jul  2 14:50 newinitrd.gz
Move file as an image file. You can use the newinitrd.img as your new boot image.
# mv newinitrd.gz newinitrd.img

# ls -l newinitrd.img
-rw-r--r-- 1 root root 6649867 Jul  2 14:50 newinitrd.img

Unix Stat Command: How To Identify File Attributes


Question: How do I find out all the available file attributes. i.e I would like to know more about a file or directory than what the ls -l command displays.
Answer: Everything in Unix is treated as files. This includes devices, directories and sockets — all of these are files. Stat command displays file or filesystem status as explained in this article.

File Stat – Display Information About File

For example, to find out more information about 101hacks.txt file, execute the stat commandas shown below.
$ stat 101hacks.txt
  File: `/home/sathiyamoorthy/101hacks.txt'
  Size: 854        Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d Inode: 1058122     Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/ sathiya)   Gid: ( 1000/ sathiya)
Access: 2009-06-28 19:29:57.000000000 +0530
Modify: 2009-06-28 19:29:57.000000000 +0530
Change: 2009-06-28 19:29:57.000000000 +0530

Details of Linux Stat Command Output

  • File: `/home/sathiyamoorthy/101hacks.txt’ – Absolute path name of the file.
  • Size: 854 – File size in bytes.
  • Blocks: 8 – Total number of blocks used by this file.
  • IO Block: 4096 – IO block size for this file.
  • regular file – Indicates the file type. This indicates that this is a regular file. Following are available file types.
    • regular file. ( ex: all normal files ).
    • directory. ( ex: directories ).
    • socket. ( ex: sockets ).
    • symbolic link. ( ex: symbolic links. )
    • block special file ( ex: hard disk ).
    • character special file. ( ex: terminal device file ).
  • Device: 801h/2049d  – Device number in hex and device number in decimal
  • Inode: 1058122 – Inode number is a unique number for each file which is used for the internal maintenance by the file system.
  • Links: 1 – Number of links to the file
  • Access: (0600/-rw——-): Access specifier displayed in both octal and character format. Let us see explanation about both the format.
  • Uid: ( 1000/ sathiya) – File owner’s user id and user name are displayed.
  • Gid: ( 1000/ sathiya) – File owner’s group id and group name are displayed.
  • Access: 2009-06-28 19:29:57.000000000 +0530 – Last access time of the file.
  • Modify: 2009-06-28 19:29:57.000000000 +0530 – Last modification time of the file.
  • Change: 2009-06-28 19:29:57.000000000 +0530 – Last change time of the inode data of that file.

Dir Stat – Display Information About Directory

You can use the same command to display the information about a directory as shown below.
$ stat /home/ramesh
File: `/home/ramesh'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h/2051d Inode: 5521409 Links: 7
Access: (0755/drwxr-xr-x) Uid: ( 401/ramesh) Gid: ( 401/ramesh)
Access: 2009-01-01 12:17:42.000000000 -0800
Modify: 2009-01-01 12:07:33.000000000 -0800
Change: 2009-01-09 12:07:33.000000000 -0800

Details of File Permission:

File Permission In Octal Format

This information about the file is displayed in the Access field when you execute stat command. Following are the values for read, write and execute permission in Unix.
  • Value Meaning
  • 4 Read Permission
  • 2 Write Permission
  • 1 Execute Permission

File Permission In Character Format

This information about the file is displayed in the Access field when you execute stat command.
  • File Type: First bit of the field mentions the type of the file.
  • User Permission: 2nd, 3rd and 4th character specifies the read, write and execute permission of the user.
  • Group Permission: 5th, 6th and 7th character specifies the read, write and execute permission of the group.
  • Others Permission: 8th, 9th and 10th character specifies the read, write and execute permission of the others.

Display Information About File System

You can also use stat command to display the file system information as shown below.
$ stat -f /
  File: "/"
    ID: 0        Namelen: 255     Type: ext2/ext3
Blocks: Total: 2579457    Free: 1991450    Available: 1860421    Size: 4096
Inodes: Total: 1310720    Free: 1215875

How To Generate SSL Key, CSR and Self Signed Certificate For Apache


If you want to convert your website from HTTP to HTTPS, you need to get a SSL certificate from a valid organization like Verisign or Thawte. You can also generate self signed SSL certificate for testing purpose.

In this article, let us review how to generate private key file (server.key), certificate signing request file (server.csr) and webserver certificate file (server.crt) that can be used on Apache server with mod_ssl.
Key, CSR and CRT File Naming Convention
I typically like to name the files with the domain name of the HTTPS URL that will be using this certificate. This makes it easier to identify and maintain.
  • Instead of server.key, I use www.sureshkumarpakalapati.in.key
  • Instead of server.csr, I use www.sureshkumarpakalapati.in.csr
  • Instead of server.crt, I use www.sureshkumarpakalapati.in.crt

1. Generate Private Key on the Server Running Apache + mod_ssl

First, generate a private key on the Linux server that runs Apache webserver using openssl command as shown below.
# openssl genrsa -des3 -out www.sureshkumarpakalapati.in.key 1024
Generating RSA private key, 1024 bit long modulus
.......................................++++++
...................................................++++++
e is 73547 (0x01001)
Enter pass phrase for www.sureshkumarpakalapati.in.key:
Verifying - Enter pass phrase for www.sureshkumarpakalapati.in.key:

# ls -ltr www.sureshkumarpakalapati.in.*
-rw-r--r-- 1 root root   963 Jun 13 20:26 www.sureshkumarpakalapati.in.key
The generated private key looks like the following.
# cat www.sureshkumarpakalapati.in.key
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,485B3C6371C9916E

ymehJu/RowzrclMcixAyxdbfzQphfUAk9oK9kK2
jadfoiyqthakLKNqw9z1MoaqkPyqeHevUm26no
AJKIETHKJADFS2BGb0n61/Ksk8isp7evLM4+QY
KAQETKjdiahteksMJOjXLq+vf5Ra299fZPON7yr
-----END RSA PRIVATE KEY-----

2. Generate a Certificate Signing Request (CSR)

Using the key generate above, you should generate a certificate request file (csr) using openssl as shown below.
# openssl req -new -key www.sureshkumarpakalapati.in.key -out www.sureshkumarpakalapati.in.csr
Enter pass phrase for www.sureshkumarpakalapati.in.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:US
State or Province Name (full name) [Berkshire]:California
Locality Name (eg, city) [Newbury]:Los Angeles
Organization Name (eg, company) [My Company Ltd]:The Suresg Stuff
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []: sureshkumarpakalapati
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

# ls -ltr www.sureshkumarpakalapati.*
-rw-r--r-- 1 root root   963 Jun 13 20:26 www.sureshkumarpakalapati.in.key
-rw-r--r-- 1 root root   664 Jun 13 20:35 www.sureshkumarpakalapati.in.csr

3. Generate a Self-Signed SSL Certificate

For testing purpose, you can generate a self-signed SSL certificate that is valid for 1 year using openssl command as shown below.
# openssl x509 -req -days 365 -in www.sureshkumarpakalapati.in.csr -signkey www.sureshkumarpakalapati.in.key -out www.sureshkumarpakalapati.in.crtSignature ok
subject=/C=US/ST=California/L=Los Angeles/O=sureshkumarpakalapati/OU=IT/CN=www.sureshkumarpakalapati.in
Getting Private key
Enter pass phrase for www.sureshkumarpakalapati.in.key:

# ls -l www.sureshkumarpakalapati*
-rw-r--r-- 1 root root   963 Jun 13 20:26 www.sureshkumarpakalapati.in.key
-rw-r--r-- 1 root root   664 Jun 13 20:35 www.sureshkumarpakalapati.in.csr
-rw-r--r-- 1 root root   879 Jun 13 20:43 www.sureshkumarpakalapati.in.crt

# cat www.sureshkumarpakalapati.in.crt
-----BEGIN CERTIFICATE-----
haidfshoaihsdfAKDJFAISHTEIHkjasdjadf9w0BAQUFADCB
kjadfijadfhWQIOUQERUNcMNasdkjfakljasdBgEFBQcDAQ
kjdghkjhfortoieriqqeurNZXCVMNCMN.MCNaGF3dGUuY29
-----END CERTIFICATE-----

You can use this method to generate Apache SSL Key, CSR and CRT file in most of the Linux, Unix systems including Ubuntu, Debian, CentOS, Fedora and Red Hat.

4. Get a Valid Trial SSL Certificate (Optional)

Instead of signing it youself, you can also generate a valid trial SSL certificate from thawte. i.e Before spending the money on purchasing a certificate, you can also get a valid fully functional 21 day trial SSL certificates from Thawte. Once this valid certificate works, you can either decide to purchase it from Thawte or any other SSL signing organization.

This step is optional and not really required. For testing purpose, you can always use the self-signed certificate that was generated from the above step.

Go to Thwate trial certificate request page and do the following:
  • Select “SSL Web Server Certificate (All servers)” under the “select your trial certificate”.
  • Do not check the PKCS #7 check-box under the “configure certificate”
  • Copy/Paste the *.csr file that you generate above in the textbox under “certificate signing request (CSR)”
  • Click on next at the bottom, which will give you a 21-day free trial certificate.

Copy/Paste the trial certificate to the www.sureshkumarpakalapati.in.crt file as shown below.
# cat www.sureshkumarpakalapati.in.crt
-----BEGIN CERTIFICATE-----
haidfshoaihsdfAKDJFAISHTEIHkjasdjadf9w0BAQUFADCB
kjadfijadfhWQIOUQERUNcMNasdkjfakljasdBgEFBQcDAQ
kjdghkjhfortoieriqqeurNZXCVMNCMN.MCNaGF3dGUuY29
-----END CERTIFICATE-----