Blog is moving

My blog is moving to http://victormendonca.com/blog/. If you are looking for a specific or older post you are in the right place Otherwise check out my new page for more up to date content.

Monday, November 26, 2012

How to view saved wifi passwords in Android

The main location for the password file is (note that it may vary depending on your device):

/data/misc/wifi/wpa_supplicant.conf

And the format is:

network={
ssid="[SSID Name]"
psk="[WPA passphrase]"
key_mgmt=[Encryption type]
priority=[XX]
}


I will provide two ways of viewing the password.

Method 1 - UI
1- Download a root browser if you don't have one. I used Root Browser Lite
2- Browse to the file location given above
3- Check how many bytes the file has... if it has 0 this method will not work
4- Click open the file with a text editor

Method 2 - Terminal
1- Open the terminal application
2- Change into root with 'su' and then cat the 'wpa_supplicant' file

su
cat /data/misc/wifi/wpa_supplicant.conf

Wednesday, November 21, 2012

Deleting iptables Rules by Line Number

Looking for a simple way of deleting a specific iptable rule? Well, here it is...

First display all rules by line numbers (this will show a list of the rules by line):
iptables -L INPUT -n --line-numbers
# or/and
iptables -L OUTPUT -n --line-numbers

Then use the line number to delete the rule:
tables -D INPUT 1  # where "1" is the line number or the rule

Command prompt exit face (color smiley face)

Here's a neat little hack that you can use to display a color exit code smiley face on your shell prompt (also refer to "Playing with terminal colors on your script" for more info on color and shell).

Here's what the end result looks like:
victor@Desktop:~ :) $ cd /tmp
victor@Desktop:/tmp 
:) $ ls 123
ls: cannot access 123: No such file or directory

victor@Desktop:~ :(

To achieve that, simply change your PS1 variable in your profile or you shell rc file to the code below:
PS1='\u@\h:\w `if [ $? = 0 ]; then echo \[\e[32m\]:\)\[\e[37m\]; else echo \[\e[31m\]:\(\[\e[37m\]; fi` $ '

Thursday, November 1, 2012

Playing with terminal colors on your script

Most Linux shells include support for color (font and background), which can be used in many different ways. Today I will show how to change font color based on the output of a script. This way you can have colorful outputs depending on the exit code.

Here are the two example outputs of the script (function) we will be using. It simply checks if Apache is running and returns the color coded status:

$ isApacheUp 
Apache is running

$ isApacheUp 
Apache is NOT running

Finding the mapping for the color codes is also quite easy. I will be providing some of the basic colors, but you can also try this really cool site to find additional color and background mapping - Google.

The basic usage for setting up the color is:

- Sets color (this case, white)
$ echo -e "\e[01;37m"

- Reverts color back to default
$ echo -e "\e[00m"

So if you want to change your terminal to other color, you can:

victor@Desktop:~$ echo -e "\e[00;34mPrompt to blue"
Prompt to blue
victor@Desktop:~$ echo -e "\e[00;31mPrompt to red"
Prompt to red
victor@Desktop:~$ echo -e "\e[00mPrompt to default"
Prompt to default
victor@Desktop:~$ 

Note that the color changes happens after you enter the code. So if you only want to change one word you will need to prefix the word with the new color code, and add the default color right after.

Here we can see the our Apache script using just that:

isApacheUp () {

# Sets the font color
GREEN='\e[00;32m'
DEFT='\e[00m'
RED='\e[00;31m'

# Checks if the Apache process is running
ps -ef | grep -i apache | grep -v grep > /dev/null ; STATUS=$?

# Changes the color depending on grep's exit code
if [[ $STATUS -eq 0 ]] ; then
  echo -e "Apache is ${GREEN}running${DEFT}"
else
  echo -e "Apache is ${RED}NOT running${DEFT}"
fi
}

Font color mapping:

Black = ”\e[00;30m”
Dark Gray = ”\e[01;30m”
Blue = ”\e[00;34m”
Light Blue = ”\e[01;34m”
Green = ”\e[00;32m”
Light Green = ”\e[01;32m”
Cyan = ”\e[00;36m”
Light Cyan = ”\e[01;36m”
Red = ”\e[00;31m”
Light Red = ”\e[01;31m”
Purple = ”\e[00;35m”
Light Purple = ”\e[01;35m”
Brown = ”\e[00;33m”
Yellow = ”\e[01;33m”
Light Gray = ”\e[00;37m”
White = ”\e[01;37m”