Thursday, March 27, 2008

SED (Stream EDitor)

SED


Find and Replace:

Sed is often used as a find-and-replace tool.

     sed 's/Glenn/Harold/g' oldfile >newfile

will replace every occurrence of "Glenn" with the word "Harold", wherever it occurs in the file. The "find" portion is a regular expression ("RE"), which can be a simple word or may contain special characters to allow greater flexibility (for example, to prevent "Glenn" from also matching "Glennon").

SED Leaving Space:

My very first use of sed was to add 8 spaces to the left side of a file, so when I printed it, the printing wouldn't begin at the absolute left edge of a piece of paper.

     sed 's/^/        /' myfile >newfile   # my first sed script
sed 's/^/ /' myfile | lp # my next sed script

SED display one paragraph of a file:

sed could display only one paragraph of a file, beginning at the phrase "and where it came" and ending at the phrase "for all people". My script looked like this:

     sed -n '/and where it came/,/for all people/p' myfile

SED Display 12 to 18 lines:

found that sed could show me only (say) lines 12-18 of a file and not show me the rest. This was very handy when I needed to review only part of a long file and I didn't want to alter it.

     # the 'p' stands for print
sed -n 12,18p myfile

SED Display everything but of those 12, 18:

sed could show me everything else BUT those particular lines, without physically changing the file on the disk:

     # the 'd' stands for delete
sed 12,18d myfile

SED double-space my single-spaced file when it came time to print it:

double-space my single-spaced file when it came time to print it:

     sed G myfile >newfile


Iptables rules for Outlook Express

A Squid proxy can share a Internet Service.
But it will not download the mails in Outlook Express.
In this case IPtables can be used to retrieve mails.

A model Network setup:
Eth0 : Public N/W

Eth1 : Private N/W
IP : 192.168.10.40
IP Range : 192.168.10.0/24

So hence the Iptables rules for forwarding the packets ,

# iptables --table nat --append POSTROUTING -s 192.168.10.0/24 --out-interface eth0 -j MASQUERADE

# iptables --append FORWARD -s 192.168.10.0/24 --in-interface eth1 -j ACCEPT

# iptables -A INPUT -s 192.168.10.0/24 -j ACCEPT

Insert the above rules
Save and restart the service .

#service iptables save
# service iptables restart

Now you can download your mails in the Outlook express.

Client Side Configuration:
1. Set the Gateway address as Eth1 IP.
2. Open Outlook Express – Tools – Accounts – mail –add new
3. Configure your mail account.

Linux - Memory Leakage

In Linux the valgrind tool help us to detect the memory leakage in our coding.

For Example
if your application exec is named as myexec, then run the command as below
valgrind --tool=memcheck --leak-check=yes --num-callers=25 ./myexec

Note :- when your running the exec with valgrind tool run the exec with ./ (dot slash).

when your application needs to pass arguments with the exec run tool with argument

valgrind --tool=memcheck --leak-check=yes --num-callers=25 ./myexec -ORBInitRef
NameServic=corbaloc:iiop:localhost/NameService

Memcheck performs a range of memory-checking functions,including detecting accesses to uninitialized memory, misuse of allocated memory (double frees, access after free, etc.) and detecting memory leaks.

Linux Disk Copy

HardDisk Copy In Linux
(Disk to Disk Copy)

Connect two hard disk in the mother board.
Boot the system with the data hard disk.
Run the following command to view the hard disk name,
# fdisk -l (shows booted H/D)
# ls /dev/ (shows the other H/D)
Let the data containing hardisk be HDA and the data in which to be copied be HDB.
Now execute the below command to copy the hard disk.

# dd if=/dev/hda of=/dev/hdb bs=1024k

Now the data's with start to copy.

Internet Sharing in linux (Squid)

Squid’s main configuration file is in /etc/squid/squid.conf.
Edit the file
Open /etc/squid/squid.conf
You will need to either find and uncomment entries, or modify existing uncommented lines in the squid configuration file. Use text editor or a text find to locate these lines:

visible_hostname machine-name or IP
http_port 3128
cache_dir ufs /var/spool/squid 1000 16 256
cache_access_log /var/log/squid/access.log

In the acl section near the bottom add:
acl intranet 192.168.10.0/24 -> Allow all the machines to access.
http_access allow lcsintranet.com (lcsintranet.com – hostname)
visible_hostname - Create this entry and set this to the hostname of the machine. To find the hostname, use the command hostname. Not entering a value may cause squid to fail as it may not be able to automatically determine the fully qualified hostname of your machine.

http_port 3128 - Uncomment this line but there is no need to edit it unless you want to change the default port for http connections.
cache_dir ufs /var/spool/squid 1000 15 256 - Uncomment this line. You may want to append a zero to the value 100 which will make the cache size 1000MB instead of 100MB. The last two values stand for the default folder depth the cache will create on the top and subdirectories respectively. They do not need modification.
cache_access_log - Uncomment this line. This is where all requests to the proxy server will get logged.
acl intranet 192.168.10.0/24 - This entry needs to be added. It should correspond to whatever your local network range is.
http_access allow lcsintranet - This allows the acl named intranet to use the proxy server. Make sure to put allow directives above the last ‘http_access deny all’ entry, as it will overide any allow directives below it.

To allow a single address to access a specific URL
This example allows only the special_client to access the special_url. Any other client that tries to access the special_url is denied.

acl special_client src 192.168.10.3
acl special_url url_regex ^http://www.squid-cache.org/Doc/FAQ/$
http_access allow special_client special_url
http_access deny special_url

To allow a particular address to access a list of URL
acl special_client src 192.168.10.3
acl special_client src “/etc/squid/ip.txt”->(Make a file ip.txt and add IP to access the particular URL)
acl lcssite url_regex –i “/etc/squid/site.txt” -> (Make a file site.txt add the URL)

Turning on squid
Start the service:

service squid start
Verify that squid is running :

service squid status

Configuring the clients
If you are using Firefox or Mozilla you will need to add the proxy server as follows:
Go to Preferences>Network>Settings
Add the name of your new proxy server and port 3128 to the http proxy field.