Tuesday, November 16, 2010

Linux magical commands

Linux command like a magic stick in the hand of wizard, more you practice more you find scope of learning. I am playing with it from few years and still I find myself n00b in this context, although today I planed to share my n00b experience with you regards to few basic Linux commands . This POST is totally belongs to the beginners.

cat :
concatenate files and print on the standard output
$> cat file.txt gt; cat file.txt ##print the content of the file on terminal
$> cat -n file.txt gt; cat -n file.txt ##print the content of file with line number


grep :
grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
$>grep apple fruitlist.txt ##grep prints all lines containing apple
$>
grep -w apple fruitlist.txt ##grep prints all lines containing apple as a word
You can also use grep with regular expressions

sed
is a stream editor. A stream editor is used to perform basic text transformations on an input stream. While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

$>sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName$>
##s Substitute command
##g global
##/../../ Delimiterday
##(oldstuff)Regular Expression Pattern Search Pattern
##(newstuff)night Replacement string


awk :
An AWK program is a sequence of pattern {action} pairs and function definitions. Short programs are entered on the command line usually enclosed in ' ' to avoid shell interpretation. Longer programs can be read in from a file with the -f option. Data input is read from the list of files on the command line or from standard input when the list is empty.
$>awk [ -F ] {pgm} | { -f } [ ] [ - | ]
##ch: Field-separator character.
##pgm: Awk command-line program
##pgm file: File containing an Awk program.
##vars: Awk variable initializations.
##data file: Input data file.


tr :
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
$>
tr 'abcd' 'jkmn' ##maps 'a' to 'j', 'b' to 'k', 'c' to 'm', and 'd' to 'n'.
$>cat file.txt | tr -d '\n' ##delete new line character from file

curl :
Curl is a computer software project providing a library and command-line tool for transferring data using various Protocols.
$>curl -o example.html www.example.com ##cURL can write the output it retrieves to a file with the -o flag
$>curl www.example.com

Wednesday, October 27, 2010

A small grep Tool

Yesterday, I was reading about the regular expressions design that how first developer wrote the complex code with minimal complexity, Since now I know the design of the minimal version of regular expression code I decided to share it. code in itself is quite simple and self explanatory..although I marked the complex part with appropriate comments.


 
int grep (char *regexp, char *text)
{
if (*regexp == '$' ){ /*End of regular expression*/
if ( *(regexp+1) == '\0' ){
return 1;
} else {
return 0;
}
} else if ( *regexp == '.' ) { /*Single character presence*/
return grep ( regexp + 1, text + 1 );
} else if ( *regexp == '*') { /*multi character presence*/
return matchstar(regexp+1, text);
} else if ( *regexp == *text ) {
return grep ( regexp + 1, text + 1 );
} else {
return 0;
}
}


The above code has the four important regions and will detect the expressions for *,.,$ code will return 1 on success.

 
int matchstar(char *regexp, char *text)
{
if ( grep (regexp, text ) ){ /*end of multi character match*/
return 1;
} else {
matchstar(regexp, text+1);
}
}


The code is matching multi character presence and checking it's end recursively.

 
int main(int argc, char **argv)
{
FILE *file;
char text[200];
char c;
size_t size = 0;

if (argc >= 3) {
file = fopen(argv[1], "r");

while(EOF != (c = getc(file))){
if (c=='\n'){
text[size] = '\0';
int i = 0;
for ( i = 0; i < size; i++){
if (grep(argv[2], text + i)) {
printf("%s\n", text);
break;
}
}
size = 0;
} else {
text[size] = c;
size++;
}
}
}
return 0;
}



Compilation Instruction

[~/CODE]
13:04:24 $ gcc -o grep grep.c

[~/CODE]
13:04:36 $ gedit grep.c


./grep file_name regular_expression$

[~/CODE]
13:37:32 $ cat test
my
myname is
my name is khan
dog
cat is my pet
peterson is an awesome player

[~/CODE]
13:37:35 $ ./grep test m*k$
my
myname is
my name is khan
cat is my pet
peterson is an awesome player

[~/CODE]
13:37:39 $ ./grep test dog$
dog

[~/CODE]
13:37:43 $ ./grep test awsome$

[~/CODE]
13:37:49 $ ./grep test aw.some$
peterson is an awesome player



:-fat0ss

Wednesday, October 20, 2010

nmap scans : some basics over LAN

nmap : nmap(network mapper) is a network scanning tool written by Gordon Lyon and used to scan port, IP, operating systems,services on computer network

Here, I List some useful set of nmap scans over LAN :

1)ping a range of IP addresses
nmap -sP 192.168.1.100-254

-sP: Ping Scan

2)list all opened ports on host
nmap -p 1-65535 --open IP

-p : port range
--open: Only show open
Where IP may be a single IP, a hostname or a subnet

3)Get info about remote host ports and OS detection
nmap -sS -P0 -sV -O IP

Where IP may be a single IP, a hostname or a subnet
-sS TCP SYN scanning (also known as half-open, or stealth scanning)
-P0 option allows you to switch off ICMP pings.
-sV option enables version detection
-O flag attempt to identify the remote operating system
Other option:
-A option enables both OS fingerprinting and version detection
-v use -v twice for more verbosity.

Useful Links:

* Installation Guide
* Document of Nmap
* Books on Nmap


:-fat0ss

freopen() : change Mode back

Yesterday, I found a particular problem with the C function freopen(), so I decide to share it with you

Problem is with the function freopen(); the function change the role of the usual stdin, stdout for the file operation.

Problem : if I use freopen, how can I get the original stdout (or stdin) back?
FOF, this is not a good way to doing the file handling and my suggestion is always use your own explicit output (or input) stream variable, for the file operation.

Solution : solution of this particular problem is given by using dup, dup2 , dup and dup2 are the system call for duplicating underling file descriptor, which are present in UNIX/LINUX but not necessary any where else.

Reference:
http://c-faq.com/stdio/undofreopen.html
http://c-faq.com/stdio/rd.kirby.c

:-fat0ss