Sunday, August 22, 2010

How I read the balance information from a 3G/GPRS data connection

For last few months I am using BSNL 3G internet connection. I am using a prepaid connection through USB 3G device. So, before and after every internet session I need to know the remaining balance of my sim card, but the device have no LCD screen/keypad. That is why every time I have to insert the sim card into a mobile to get the balance of the sim card.

Recently I have solve this problem. I have wrote a python code to get the balance of the sim card directly through USB 3G device. I have wrote a supporting shell script also to create GUI interface for that python code.

The only dependencies to run the code are Python and Zenity.

I have wrote the code for my BSNL 3G connection, and have to customise for other connection.

==mobile_balance.py==============================
import sys
import serial
try:
serial_device = serial.Serial(sys.argv[1],timeout=8)
except IndexError:
print "\nMobile Balance\n\nSyntax: mobile_balance.py /dev/ttyUSB[2..5]\n"
except serial.serialutil.SerialException:
print "\nUnable to open port",sys.argv[1],"\n"
else:
serial_device.write('AT+CUSD=1,*123#,15\r\n')
while (1):
text_output = serial_device.readline()
if text_output.find('+CUSD')==0:
text_output = text_output.lstrip('+CUSD: 0,"')
print text_output.rstrip('",15\r\n')
break
serial_device.close()
quit()

===================================================

==mobile_balance.sh================================
#!/bin/bash
PreviousBalance=`cat /tmp/mobile_balance.tmp`
python `echo $0 | sed 's/.sh/.py'/` `ls /dev/ttyUSB{2..5} 2>/dev/null` > /tmp/mobile_balance.tmp | zenity --info --text="Mobile balance calculation in progress\nClick 'OK' and\nWait for a while....."
echo -e "Current Balance\n"`cat /tmp/mobile_balance.tmp` "\n\nPrevious Balance..\n" $PreviousBalance | zenity --text-info --title="Mobile Balance" --width=250 --height=300

====================================================

Friday, May 7, 2010

How an administrator can unlock a table to overcome deadlock situation in MySQL

This is a common problem for a MYSQL DBA if the application has table lock bug. To unlock a table I use session kiil procedure. The steps are....

Step 1: login into mysql user root user.
Step 2: list all the sessions running in MySQL server.

show processlist;

The output will be looks like....

+-----+----------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info | +-----+----------+-----------+------+---------+------+-------+------------------+
| 349 | his_user | localhost | NULL | Query | 0 | NULL | show processlist | +-----+----------+-----------+------+---------+------+-------+------------------



...if the MySQL server has no other running process. Otherwise the output will show other process id also. For locked processes the status will indicate the work lock.

Step 3: The last step is to kill the locked running process using process Id.

kill xxx;

'xxx' is the process Id value.

Monday, May 3, 2010

How to reset MySQL root password

These steps are required if we have lost(for my case) the root password for MySQL administration.

Step 1: At first I stop the MySQL service from Linux root user.

/etc/init.d/mysql stop

Step 2: Then I start again MySQL service with --skip-grant-tables, so that at the time of login into the MySQL service system should not ask for authentication.

mysqld_safe --skip-grant-tables &

Step 3: In this step I first login into MySQL root user without password.

mysql -u root

Then I issue the following MySQL command to change the root password and quit to Linux shell..

use mysql;
update user set password=password('password') where user='root';
flush privileges;

Step 4: The last stage is to restart MySQL server using default option.

/etc/init.d/mysql restart


The password for root user is changed now. I have done all these procedures on the OS Debuan 5.

Wednesday, April 28, 2010

How I connect internet using my bluetooth enabled mobile(GPRS) phone

Initially I starts searching the device of my mobile phone, after traversing the menu of mobile phone I have found the device id 00:0d:92:31:66:0d.

In next stage I have bound the device id with the named device /dev/rfcomm0 using the command rfcomm

The command I have issued is....

rfcomm bind /dev/rfcomm0 00:0d:92:31:66:0d

In the third or last stage I have change the value of Modem directive in /etc/wvdial.conf file.

Modem = /dev/rfcomm0

New my job is complete. After these steps now I can connect internet using wvdial through bluetooth enabled mobile phone.

Friday, July 3, 2009

How I solve the "PHP Fatal error: Class 'DOMDocument' not found in ....." problem on zend framework at the time of project creation.



I have fedora 9 with PHP 5 installed in my PC. While creating project using the command

"zf.sh create project myproject"

on Zend framework(minimal) I have faced a PHP fatal error just like..

"PHP Fatal error: Class 'DOMDocument' not found in /var/www/html/ZendFramework-1.8.3/library/Zend/Tool/Project/Profile/FileParser/Xml.php on line 74"

I thought that the error is generated because I was trying to install minimal version. After then I have installed full version of Zend framework which is more than 38MB in size.

But the problem was not solved.

Again I start searching the net to find out any solution.

At last I have found a solution.....some one wrote a document almost 2 years ago. He said that the problem might occur because of unavailability of libhtml2 support for php and he suggested to install php-xml to solve the problem.

This suggestion helps me to solve the problem. Now it is working and I have already created projects on Zend framework.


Saturday, June 13, 2009

How I customize 'Change Password' plugin of SquirrelMail so that the plugin can change '/etc/passwd' & '/etc/shadow' of remote server.


The Scenario

In my organization we have following setup . Mail server has user accounts and runs SMTP and IMAP / POP service. Incomming mails are stored in mail server following normal mail route.

Users have the option of checking their mail from local network through installed mail clients like thunderbird / outlook etc. and checking them through webmail. http server runs webmail and access user inboxes IMAP and SMTP service from mail server.

The requirement was to enable user password change through squirrelmail interface.

I have faced problem when I installed 'Change Password' plugins in SquirrelMail system. The plugin can't change the password of mail user. This is because the working logic of 'Change Password' plugin is something different.

The working logic of 'Change Password' plugin

The 'Change password' plugin contains more than one php files and one executable ('chpasswd') file. The executable file is the core execution program of the plugin. This plugin can only work on '/etc/passwd' or '/etc/shadow' files of local server (http server) on which the SquirrelMail application is installed.

When ever an user issue the command to change the password through web form, the SquirrelMail executes 'chpasswd' command with proper parameters to achive the job.

The modified working logic of 'Change Password' plugin

I have changed the working logic of plugin to incorporate the remote password change feature.

The new working logic is, when ever some user will issue the command to change the password, the SquirrelMail server (http server) will pass the control of execution to the mail server to change the password of mail server rather than http server.




The implementation of modified working logic


To implement the logic, first of all I break up the whole job in three parts.

Part One

In the first part I have configured the mail server by coping 'chpasswd' executable program from the plugin directory of http server to the same directory of mail server. In my case it is in '/usr/share/squirrelmail/plugins/change_passwd'. At the time of copy I have followed all the guidance as guided by the README file of plugin. Please check the README file of the plugin for the configuration commands.

Part Two

In the second part I have modified the configuration php file of plugin itself. The actions I have followed in second phase are listed below.

1. Open the file config.php from the 'Change password' plugin directory.
2. Find out the line containing the string

$overridePathToChpasswd = '';

3. Replace the line with

$overridePathToChpasswd = 'ssh {user}@{ip_address} {full_path}/chpasswd';

[PLEASE NOTE: replace the {user} with your administrative user_id of your mail server, replace {ip_address} with your mail server's ip address and also replace the {full_path} with absolute path of 'chpasswd' program on mail server]

Part Three

This is the most important part in this customization phase. As we know ssh is secured terminal connection between server & client. ssh has two type of authentication method; one is password authentication and another is encryption key authentication. In case of password authentication ssh will ask the user for password interactively. Now, imagine how can one enter password non-interactively through php code. No way..! Wait...one way left,as I know. I have used encryption key based authentication (without pass phrase) procedure. The steps are as follows..

1. Find out which server in this case is acting as ssh client. In my case the http server is acting as ssh client and the mail server as ssh server.

2. Now find out which linux user is the owner of the http/apache server. You can find out this information from the configuration file 'httpd.conf' of apache in /etc/httpd/conf/ directory.

3. Switch to that user from root using the command.

su - apache

[NOTE 1: In my case the 'apache' user is the owner of http/apache server]
[NOTE 2: debian users can do this very easily. But the fedora/redhat users can't., Because in fedora/redhat 'nologin' shell has been defined as login shell in /etc/passwd file.

[IF YOU ARE A FEDORA/REDHAT USER; THEN FOLLOW NEXT STEP OTHER WISE SKIP TO THE STEP 5]

4. Open /etc/passwd file from root user. Go to the proper line where login information has been written for that specific user who is the owner of http/apache server. In my case it is 'apache'. Now replace the characters '/sbin/nologin' with '/bin/bash' or '/bin/sh' from that particular line only. Now you can switch to the user, owner of the http/apache server issuing the command.

su - apache

[NOTE: BE CARE FULL ABOUT TASK. ANY MISTAKE MIGHT CHANGE THE SYSTEM TO UNSTABLE ONE]

5. Now issue the command.

ssh-keygen -t dsa

[NOTE: System will ask for a pass phrase. Keep it blank and press enter for pass phrase field]

A file 'id_dsa.pub' will be created in the directory '/home/{user}/.ssh/'. In my case it is in '/home/apache/.ssh' directory.

6. Copy the dsa public key file to the user directory of administrator user of ssh server(in my case it is in mail server).

7. Issue the command

cat id_dsa.pub >> .ssh/authorized_keys

Now, please try to login from ssh client to ssh server. If all the processes have been done properly then it is highly possible to login from ssh client to ssh server.

Every thing have been done. Now, try to change the password from SquirrelMail web form.