Monday, February 22, 2010

How to start Python Simple Httpd Server for Simple File Sharing

Do you want to share a file and dont have any prior knowledge of setting up WebServer or FTP Server or even dont know about other file sharing protocols knowledge. This Issues can be resolved using Python Language.

How to Do that?

1. As Root go to the location which you want to Share to the External Users.

[root@localhost ~]# cd /tmp

2. Run the Python Command with a Defined port or default will be used (Port 8000).

[root@localhost tmp]# python -m SimpleHTTPServer 19000 &

Note :
a) To Run the Command in background use "&" at the end.
b) In the above case the Port number is set to be 19000.

3. From the Remote System In the browser Type in the IPaddress:Portno you have used. Now you can see the contents of the /tmp directory
http://ip.addr.ess:19000

4. Yeppie Thats it !!!! you are now sharing your files without setting up WebServer or any FTP Server or any knowledge of system administration.

How to sent Message through Terminal to a Login users In Fedora, Centos, RedHat, Ubuntu.

You can broadcast a message using WALL -- How to broadcast a Message through Terminal to Login users How To but what if you want to sent message to only a particular user logged into the system or different Messages to Different users. Yes its possible using write command.

Before using write command Check if it is avaliable in you system. From the terminal Run

[root@localhost ~]# which write

which should give
[root@localhost ~]# which write
/usr/bin/write


If write is not installed Install bsdmainutil package

Once Installed to sent message to a particular user we need to know which users are currently logged into the System

[root@localhost ~]# w

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
ashwin :0 - 15Feb10 ?xdm? 7:56m 0.21s /usr/bin/gnome-session
ashwin pts/1 :0.0 11:34 22.00s 1.08s 0.01s gnome-terminal
demo1 pts/2 :0.0 11:58 11.00s 1.07s 1.2s gnome-terminal
demo2 pts/3 :0.0 01:58 8.00s 0.16s 1.8s gnome-terminal
ashwin pts/4 :0.0 12:58 1:47 4.02s 0.02s bash


Now you have the list of users logged in.

METHOD 1
[root@localhost ~]# write ashwin pts/4
Your Message 1
Your Message 2
Your Message 3
Once Done

“Terminate the write by ctrl+D.”


The User "ashwin" would see the message into the console as

[ashwin@localhost ~]$
Message from ashwin@localhost (as root) on pts/2 at 12:49 ...
Your Message 1
Your Message 2
Your Message 3
EOF


METHOD 2
[ashwin@localhost ~]$ echo "Test Message Using Write from http://linuxmaza.com" > /dev/pts/3

The above command will display a message to User "demo2" on the console.

Write Command Runs on all linux distros and installed by default in Fedora, Centos. Please refer the documentation if it is not installed under you linux distribution.

How to broadcast a Message through Terminal to Login users In Fedora, Centos, RedHat, Ubuntu

Do you want to sent message to all logged in users in your system (This works great when you are a System Administrator and many users are logged into same System)?

If you are Running Any Linux Distribution like Fedora, Centos, RedHat, Ubuntu you can send or broadcast message to all user logged in using "WALL" command. To use it on the command line / Terminal.

METHOD 1
========

[root@localhost ~]# wall "Your message Goes here"


Now all users can see the Message Broadcasted by You

[ashwin@localhost tmp]$
Broadcast message from root (pts/2) (Mon Feb 22 12:10:35 2010):

Your message Goes here


METHOD 2
========
[shankar@localhost ~]$ wall [Press Enter]
Write You Message Here.
Testing Wall OutPut On All Terminals.
Finished


Press Ctrl + D for EOF (End of File)

[root@localhost ~]#
Broadcast message from shankar (pts/3) (Mon Feb 22 12:14:55 2010):

Write You Message Here.
Testing Wall OutPut On All Terminals.
Finished


All Users can view the Message on their Terminal as below

[root@localhost ~]#
Broadcast message from shankar (pts/3) (Mon Feb 22 12:14:55 2010):

Write You Message Here.
Testing Wall OutPut On All Terminals.
Finished


Limitations of Wall :
1. The total length of the message is limited to 22 lines.
2. Broadcasted Messages are limited to The System Itself.

Advantages of Wall
1. During an Activity / Crisis on the system all users can be notified immediately
2. No need to Send Email to the users who are Logged into the System.

Wednesday, February 17, 2010

Mounting SSH Server Using FUSE

FUSE which stands for Filesystem in Userspace has brought a host of features to mount a remote filesystem using various protocols like SSH, FTP. FUSE allows non privileged users to develop their own filesystem without even modifying the Kernel, basically fuse is module which play a role of bridge between user space and the kernel.

To Mount a remote partition locally. Run the Command (You Dont Need To be Root User to Run this Command).

root# sshfs user@hostname:path_of_directory_to_be_mounted mount_to_point

root# sshfs ashwin@192.168.1.10:/home/ashwin /mnt/

If You are using a specific port for connecting to your SSH Server use -p Option

root# sshfs -p 10234 ashwin@192.168.1.10:/home/ashwin /mnt/

where 10234 is the port running SSH Service
Important Note : To Mount other Partition like /var, /opt, /root you have to mount being a root user ofthe remote machine.

root# cd /mnt

root# ls

msrv2826d0.1.tmp
keyring-0sIxHo keyring-K0zJSJ msrv2832d0.1.tmp
keyring-16cIRa keyring-kJqkFX mysqlbin.000085
keyring-1h8sea keyring-kzkqpE mysqlbin.000086
keyring-1M5nUH keyring-lyNVMO mysqlbin.000087
keyring-2i7woO keyring-m0dxjd mysqlbin.000088
keyring-4hSFtb keyring-m1xNam mysqlbin.000089
keyring-68PwBN keyring-mTgrKf mysqlbin.000092
keyring-7JY4q3 keyring-NDLFUc mysqlbin.000093
keyring-7zjMDV keyring-nt2Ygk mysqlbin.index


Enjoy, you have mounted a remote directory locally that to on secured transmission.

How to get the percentage of Command executed?

Get percentage of Command executed

Do you want to know which command on you Linux box has executed the most and also would you like to get the Percentage. This is very Simple and can be achieved useing awk, grep. It can be acheived in many other possible ways too.


root# history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10

OutPut:

1 133 13.3% ls
2 127 12.7% cd
3 78 7.8% vi
4 35 3.5% netstat
5 32 3.2% postfix
6 31 3.1% tail
7 30 3% mv
8 29 2.9% telnet
9 29 2.9% ll
10 23 2.3% postqueue

In the above example ls was the most executed command with share of 13.3% following with cd with 12.7 and then rest.

Understanding and Reading Mail Logs, Postfix Logs, Sendmail Logs, Qmail Logs

How do I read Mail Log?

Logs play a crucial role in identifying the issue may it be Basic Problem or a Big Problem heap, logs generated can make a System Admins life quite easy if you know what exactly they say. Understanding Mail Logs, Postfix Logs, Sendmail Logs or any other MTA are all the same. All the Mail Server Programs are Logging aware.

Lets get to work and understand the Logs generated by RSyslog Daemon. I am Using Fedora 11 with Sendmail 8.14 installed on it. Sendmail Listens on Two ports
25 : MTA (Mail Transfer Agent)
587 : MSP (*Mail Submission Program/Port)

Example Log:

Feb 4 06:10:09 techy sendmail[5392]: o140e90B005392: from=, size=2434, class=0, nrcpts=1, msgid=<201002040040.o140e9Mi005380@techy.bounceme.net>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1]
Feb 4 06:10:09 techy sendmail[5380]: o140e9Mi005380: to=root, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=32168, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (o140e90B005392 Message accepted for delivery)



Feb 4 06:10:09 = Date of Mail received to the Mail Transfer Agent
techy = Hostname
sendmail[5392] = MTA or Application Name with PID for the Mail
o140e90B005392 = Queue ID
from= = Sender Address
to= Reciepient Address
Size=2434 = Size in Bytes
nrcpts=1 = Number of Recepients
msgid=<201002040040.o140e9Mi005380@www.linuxmaza.com> = Message ID (Unique of every Mail)
proto=ESMTP = Protocol Used
daemon=MTA = Application Handler In Sendmail it could be MSP or MTA
relay=localhost [127.0.0.1] = Mail relayed to the next Destination Server / Recipient Server
"delays=a/b/c/d"
where a=time before queue manager, including message transmission;
b=time in queue manager; c=connection setup time including DNS,
HELO and TLS; d=message transmission time. Numbers smaller than 0.01 seconds
are rounded to 0, to reduce the noise level in the logfile.

orig_to = This is omitted when the address did not change.
conn_use = This is omitted when a connection is used once.
ctladdr=root = Controlling user
dsn=2.0.0 = Delivery Status Notification, It can be 2.x.x, 4.x.x, 5.x.x Where 2.x.x is Successfully Sent, 4.x.x Is Mail Temporarily Deferred, 5.x.x stands for Permanent Failure
stat=Sent = Status of the Message. Values Sent,Deferred,Bounced

All the above parameters are almost same generated in Postfix Logs, Sendmail Logs, Qmail Logs also there could be some extra information about the mail added into the logs.

Solved: PHP Fatal error: Call to undefined function json_encode(), How to Install JSON in PHP

PHP 5.2 has included inbuilt json_encode() and json_decode() functions by default. Using any version prior to PHP 5.2 doesn't have native JSON support. You need to compile and install json.

JSON stands for "JavaScript Object Notation" is a lightweight data-interchange format. For More Information in JSON Visit JSON Website

Error : "PHP Fatal error: Call to undefined function json_encode()"

How to install JSON in 2 easy steps.
1. Use PECL JSON Package. Log in as root and run the command
root# pecl install json
This will compile the JSON package.
2. Create a file named json.in in /etc/php.d or Check the Module Location in php.ini. Add extension=json.so to the file, and you are done
root#touch /etc/php.d/json.ini
root# echo "extension=json.so" > /etc/php.d/json.in

Restart Apache and run you application.
root# service httpd restart

You have successfully installed JSON.

PHP Fatal error: Call to undefined function json_encode, How to install JSON in PHP

PHP 5.2 has included inbuilt json_encode() and json_decode() functions by default. Using any version prior to PHP 5.2 doesn't have native JSON support. You need to compile and install json.

JSON stands for "JavaScript Object Notation" is a lightweight data-interchange format. For More Information in JSON Visit JSON Website

Error : "PHP Fatal error: Call to undefined function json_encode()"

How to install JSON in 2 easy steps.
1. Use PECL JSON Package. Log in as root and run the command
root# pecl install json
This will compile the JSON package.
2. Create a file named json.in in /etc/php.d or Check the Module Location in php.ini. Add extension=json.so to the file, and you are done
root#touch /etc/php.d/json.ini
root# echo "extension=json.so" > /etc/php.d/json.in

Restart Apache and run you application.
root# service httpd restart

You have successfully installed JSON.

Friday, February 5, 2010

How to mount RAMFS, TMPFS in Centos, RHEL, Fedora, Ubuntu, Linux

How to Use and mount RAMFS / TMPFS in Centos, RHEL, Fedora, Ubuntu?
Definition :
RAMFS Random Access Memory Filing System
TMPFS Temporary File System / Temporary File Storage

RAMFS / TMPFS is used to assign or allocate a part of physical memory to be used as a individual partition which can be used for storing data (Reading, Writing) like all other Disk partition on a system. As the data is stored in RAM (Random Access Memory) it is much faster than that of writing or reading from a System Physical disk Partition.
Performance can be measured when writing or Reading a large file from this partition (Small File size wont give you the performance). Mounting RAMFS and TMPFS is same on almost all linux distribution RHEL, Centos, Ubuntu, Fedora.

1. How to mount Tmpfs

# mkdir -p /mnt/tmp
# mount -t tmpfs -o size=200m tmpfs /tmp/ashfs

The last line in the following df -h shows the above mounted /tmp/tmpfs tmpfs file system.

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/hda2 24G 11G 12G 47% /
/dev/hda1 99M 12M 83M 12% /boot
tmpfs 248M 0 248M 0% /dev/shm
tmpfs 200M 0 200M 0% /tmp/ashfs

2. How do I mount Ramfs on the System?

# mkdir -p /tmp/ashfs

# mount -t ramfs -o size=200m ramfs /tmp/ashfs

You can change the size of the partition on the Fly.

# mount -o remount,size=2G /tmp/ashfs
It grows dynamically
You can verify the mount using

# mount
/dev/hda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
tmpfs on /tmp/ashfs type tmpfs (rw,size=200m)


By adding the partition in /etc/fstab they RAMFS/TMPFS can be mounted on boot time.







RAMFS
Uses RAM of a System
It grows dynamically
Allows to write data above the partition created
After Full Usage system may hang or stop working.
Data would be deleted once system is rebooted or incase of crashing.
You have to make a process to transfer data from RAMFS to Physical disk periodically to loose data
Suitable to have less data and read intensive
TMPFS
Uses Swap of a System.
It doesn't grow dynamically.
Doesn't allow to write data above the partition created.
System would give an error display “No space left on device”.
Data would be deleted once system is rebooted or incase of crashing.
You have to make a process to transfer data from RAMFS to Physical disk periodically to loose data.
Suitable if you want to write very fast Write intensive.


Example : You have a system with 2GB RAM and 2 GB Swap Space
RAMFS : When a partition of 1 GB is created and mounted users can even write more than 1GB till the total RAM size is exhausted as the parition is made on RAM.

TMPFS : When a partition of 1 Gb is created and mounted users wont be able to use more than 1GB space as designated on the partition. If required the space can be extended on a fly. Useses dont need to worry to control the process that writes on tmpfs partition