Pages

Sunday, September 19, 2010

Fixing yum's corrupted database...

Today, I need to update my CentOS servers. So I fire up 
#yum -y update

It ends up with error that say "rpm database corrupted"
After googling, I get the way to fix it back.

To Fix The RPM's Corrupted Database
#rm -f /var/lib/rpm/__db*
#rpm --rebuilddb
#yum -y update

I hope that this tip may save someone out there. :)

Wednesday, September 1, 2010

Squid's Caching with Ramdisk on Freebsd

Updating the ports.
#portsnap fetch
#portsnap extract
#portsnap update

Installing Squid
#cd /usr/ports/www/squid31/ && make install clean

I uncheck SQUID_KERB_AUTH, SQUID_LDAP_AUTH, SQUID_NIS_AUTH and SQUID_IPV6 to shorten the compiling time. :)
I check SQUID_DELAY_POOLS. I may need this to limit bandwidth.
Press [ OK ]. It will download the source and start compiling. For other option boxes,just accept the default and hit ENTER. Compiling takes enough time for you to drink coffee, to chat with friends or to do whatever. For my case, I was making Instant Noodle and Coffee.
After installing squid,we need to create Ramdisk before we actually run the squid service.

To create ramdisk, add the following line in /etc/fstab. 
"md    /var/squid/cache    mfs    rw,noatime,-s512M    0    0"

Mount the ramdisk and verify it.
#mount -a && mount -l

Now ramdisk is ready and it is time for us set up squid. 
I edit the following configuration parameter in /usr/local/etc/squid/squid.conf
"acl localnet src 192.168.0.0/16" to "acl localnet src 192.168.1.0/24"
"http_port 3128" to "http_port 192.168.1.5:3128"  => 192.168.1.5 is ip address of my BSD box.
"#cache_dir ufs /var/squid/cache 100 16 256" to "cache_dir ufs /var/squid/cache 500 16 256"
We have created a ramdisk with 512MB. So I assign 500MB for squid cache.

I add the following lines to squid.conf
"visible_hostname freebsd.mydomain.net"
"cache_mem 128 MB"

Add squid to system startup
#echo "squid_enable=\"YES\"" >> /etc/rc.conf

Initialize squid cache
#squid -z

Start squid for the first time
#/usr/local/etc/rc.d/squid onestart

Verify whether it is listening on the right port
#netstat -a | grep 3128

Ok! Now our squid is running happily on FreeBSD. But it will not survive on reboot because cache is kept on ramdisk and will be destroyed upon reboot. So we need to find a way to reinitialize the squid cache before squid daemon starts. Other people may use different and brilliant method.
For me, I just add "/usr/local/sbin/squid -z" right below "unset files local_rc" in /etc/rc file. Finally Squid can survive through reboot.

P.S: I admit that my solution is very lame. If you get any better solutions, please share with me.

Friday, August 27, 2010

Update Locate Database On BSD!

If you are familiar with Linux, you will appreciate the usefulness of "locate" utility.  For some people who are not aware of it, Wikipedia says "locate is a Unix utility first created in 1983 used to find files on filesystems. It searches through a prebuilt database of files generated by updatedb or a daemon and compressed using incremental encoding. It is significantly faster than find, but requires the database to be updated regularly."

Before you use locate, you need to update the database first. On Linux, it is easily done by
#updatedb

On BSD, I simply could not find updatedb. If I run locate without updating database, Error! "locate database is too small". Googling, I found how to update it. Following command will update the locate database.
#/usr/libexec/locate.updatedb

Finally, I can use locate to find files in FreeBSD. :)

Thursday, August 26, 2010

Installing Bash Shell On FreeBSD!

Today, I give a try to FreeBSD 8. After installation, I find out that its default shell is C Shell. I have been using Linux for 4 years or more, I prefer Bash to any other shells. After googling for a while, I can install Bash and set it as default shell for root user on BSD.

To install Bash Shell:
#pkg_add -r -v bash

You need to know the path to Bash Shell binary. Type:
#which bash
It will give you "/usr/local/bin/bash"

OK, we know where it is. Let's change the default shell for root user.
#chsh -s /usr/local/bin/bash root

Log out and in again.
Da..da! I get my favorite shell.

Thursday, August 12, 2010

How To Add Multiple Accounts On *nix OS By Using Bash Shell

Yes! It is not a big deal to add multiple accounts by using Bash Shell.
We can do it by:

#for x in aaa bbb ccc;do useradd $x; done

This command will add three users accounts : aaa, bbb, and ccc with no password.
What is your next move? Assigning password to each user by:

#passwd aaa
#passwd bbb
#passwd ccc 

Three accounts is ok. How about adding 50 accounts? Will you repeat the same procedure to assign password for each account? It will be a time-consuming job. Here is the solution:

As you see, it will create three user accounts with default password "password"

My Motto - "Be Lazy In Outstanding Ways"

Monday, August 9, 2010

The Poorest Solutions To Send sms To Mobiles From Linux Box !

"HEY! Netlynker! We don't have a extra handset like you. So how?" 
I have already expected this question. Fortunately for people in Singapore, Starhub, Singtel and M1 provide web 2 sms service so that you can use internet to send sms to mobile phones. But there is one catch: we can only send sms via web interface and they don't provide API for you.So you must find a way to weaponize these services for Linux Box. How? Honestly, I also don't know how to do it. :P
But I do know how to make use of the things that other people have created.A gentleman called Zhi Xiong created the bash shell scripts for these three Mobile Operators. Because of his work, I can copy it without exhausting my brain.( Even if I do, it will be useless. :) )
Here are his works.
Ok! Now you can also make use of it,exploit it or whatever..You can also show off to your friends in the night club.
"Folks! All of my servers are down and sending sms to me. How capable I am! " :D
PS: All Credits and Complaints will go to him: Zhi Xiong

Wednesday, August 4, 2010

Sending SMS from Linux Box to My Mobile.

  • Handset == Nokia 6300 with usb cable
  • OS == Ubuntu 9.10 (upgraded from 9.04)
  • Goal == To test whether Linux Box can send sms alerts to any mobile numbers.
  • Software == gammu (opensource gsm software for *nix platform)
To Install -
 #apt-get install gammu

To Configure -
#gammu-config
port = /dev/ttyACM0             #your port may differ from me depending on your ph model and cable.
model = nokia autodetection
It will save a .gammurc config file in your home directory

#gammu --identify                  # it will list your connected mobile devices , In my case Nokia 6300.

To test sms to any phone -
#gammu sendsms text +659457xxxx
Enter message text and press ^D:
I love opensource!!
Sending SMS 1/1....waiting for network answer..OK, message reference=2



People may say "Hey guy. Why do you want to sms from Linux box while you can directly send from mobile phone?"  As A System Admin, sometimes we need a way to know critical events of our servers on the spot. For example, we can configure gammu with Nagios Monitoring Server so that it will sms to my mobile whenever a network service is down. Cool? Or Annoying?

Wednesday, July 21, 2010

Web Sites Linkscanner That Help Us To Stay Away From Bad Websites

Here are three weblink scanners that I found on internet. We can trust these programs 100%.
  1. McAfee Link Advisor
  2. AVG Link Scanner
  3. WOT
Personally I use McAfee Site Advisor. Here is my screenshot.
Green Mark means that you can trust this link. I don't need to explain about RED. Right? :P

Wednesday, June 16, 2010

Simplest and Easiest Web Server

There are a lot of different ways to share data between Linux and Windows. We can use NFS, Samba, Ftp, Secureftp, http and so on. However, sometimes all of those methods are not suitable for some particular conditions. For example, I have a Fax Server running on Debian Linux. The server is not powerful enough to deploy a separate server role to handle file sharing. Its main purpose is to receive and send the fax message through modem. It even does not have firewall.
One day, a user need to get some data from the server. For me, as long as server is running ssh service, I can get what I want by using sftp or scp. For user, it may be totally different universe. If I try to teach my user how to use sftp and scp at that time, he will complain,"Please,Mr.IT, don't make my life difficult.......".
Here is how I slipped out of difficulty by using a module of Python programming language.I ssh to server and on console I type:
ip add | grep inet ; python -m SimpleHTTPServer
This command will show ip addresses of server. All I need to do is asking my user to open up the browser and type myserver's ipaddress:8000. Now he can download the data on server to his computer easily. I included the screenshot to show you how it looks like in browser.
From

SSH & Me!

Question: What is SSH?

Answer: Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices

Here is how I utilize this ssh daemon in my daily life.

!)I have 3 intranet servers ( sshgateway, intrawebmail, and winterminal)
!)Firewall is configured so that I can only ssh to sshgateway from external i.e. My Home.

When I need to configure my linux server @ my office from anywhere,

#ssh root@sshgateway.mydomain.net

Ok internet connection is too slow...

#ssh -C –CompressionLevel=9 root@sshgateway.mydomain.net

Oh, I configured my server not to listen on port 22 (default sshd port) for security reason.

#ssh -p 443 root@sshgateway.mydomain.net

I must ssh to intrawebmail to fix some problems,but I can only access to sshgateway.

#ssh -t root@sshgateway.mydomain.net ssh root@intrawebmail

Now I get access to intrawebmail server.

Fixed! I need to check webmail from intrawebmail that is only accessible in office.

#ssh -L 8080:intrawebmail:80 root@sshgateway

1)In firefox, go to Tools>Option>Advanced>Network>Setting
2)In SOCKS host  127.0.0.1 port 8080
Now I can check my office webmail from outside.

How about my winterminal? Is it healthy?

#ssh -L 3389:interminal:3389

1)Now I can remote desktop to 127.0.0.1 that redirect to winterminal.

From public internet hot spot, I want to check my bank account. Network is not secure.
So, I will tunnel my traffic through my office network.(Some may call it "Bypassing Firewall")

#ssh -D 9999 root@sshgateway.mydomain.net

1)In firefox, go to Tools>Option>Advanced>Network>Setting
2)In SOCKS host  127.0.0.1 port 9999
Now my traffic is encrypted and  I can check my bank account securely

One of my friends asks me to help with setting up a linux server. Although I want to help, his server is in Myanmar and I can not ssh to it because it is extremely protected by ISP.

What I do is:
I ask him to ssh to my sshgateway from his highly protected server;

#ssh -R 9999:127.0.0.1:22 root@sshgateway.mydomain.net

Then, on my sshgateway server , I ssh back to his linux server by using:

#ssh -p 9999 root@localhost

Now I can ssh to a host behind NAT and Firewall.

LAST BUT NOT LEAST

Now you know how we can utilize SSH in various ways. But let me remind you that running a ssh host on internet is not as easy as you think.

Believe it or not: everyday I get 5 to 13 report emails from my ssh server that tell  
“ Some body is attacking ssh server by using bruteforcing............... and blah blah blah”

If you don't give enough attention to this log messages , somebody may be able to root your server one day. That compromised ssh daemon can even put you into jail.

Don't worry! I will show you how to secure ssh server, but not now. LATER.......

Hope it gives you some knowledge......
netlynker

Q: What is the best remote administration tool for Windows??
A: A car!

Monday, June 7, 2010

Teamviewer 4 Linux

In past, I used LogMeIn, Teamviewer and GoToMyPC to remotely control the Windows PCs.Since that time, I was always thinking why any of these company never came up with solutions for LINUX.
Some Linux Guru may say, "Why don't you use VNC?". I admit that VNC can do that job.However, if you have ever used VNC over WAN links such as ADSL, Cable Broadband and Dialup connection, you will see that it is impossibly slow to work with.
Finally, TEAMVIEWER brings the solution to LINUX's world. I installed the software on my lappy that runs Ubuntu Linux 10.04. Installation process goes well. After installing, I go to Applications>Internet> and run the Teamviewer software. Then, on another PC, I fire up browser and go to Teamviewer's Web Login Page. There I log in using ID, Password and Name.
If you have ever used TeamViewer before, it will be as easy as ABC.

Here are a few videos created by other. To save my time, I didn't create another video. These video will suffice your appetite.



Sunday, June 6, 2010

Philosophy of Python

Here are the Great Philosophy of Python. You can also read these by typing "import this" in python shell.

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Clear all files that came from a tar file.

The following command makes your life better...

 tar -tzvf filename.tar.gz | xargs rm -f 

Saturday, June 5, 2010

Tonnage/Price Calculator: Python

Every now and then, my colleagues need to calculate tonnages and price according to rate given by My boss. What they usually have been doing before is fetching a calculator and calculating it. Sometimes ago, I designed a excel formula to make this process fast. But they don't want to open excel sheet to calculate these small things. So I come up with an idea to write a python program that includes GUI. What they need to do is just fireing up this program and using it.
Here is the screenshot of Tonnage/Price Calculator program.
By using this program, you can calculate tonnages and price by 3 types of Units: Inch, Feet, Millimeter.

I includes the python code so that you can improve and use it.

"""TONNAGE/PRICE CALCULATOR
========================

AUTHOR = NETLYNKER
LANGUAGE = PYTHON 2.6.5
VERSION = 1.0

"""

from Tkinter import *
from math import *
import tkMessageBox


app = Tk()
app.title("Timber Tonnages/Price Calculator")
app.geometry("410x200+100+100")

#========================================================================

uom = StringVar()
uom.set(None)

option = OptionMenu(app, uom, "Feet", "Inch", "Millimeter")
option.grid(row=1, column=5, padx=5, pady=5)

#========================================================================
l1 = Label(app, text="Length").grid(row=1,column=0, padx=5, pady=5)

l2 = Label(app, text="Width").grid(row=2, column=0, padx=5, pady=5)

l3 = Label(app, text="Height").grid(row=3, column=0, padx=5, pady=5)

l4 = Label(app, text="Quantity").grid(row=4, column=0, padx=5, pady=5)

l5 = Label(app, text="Rate").grid(row=5, column=0, padx=5, pady=5)

l6 = Label(app, text="Measured By").grid(row=1, column=4, padx=5, pady=5)

l7 = Label(app, text="Tons",font="Tahoma").grid(row=4, column=6, padx=5, pady=5)

l8 = Label(app, text="S$",font="Tahoma").grid(row=5, column=6, padx=5, pady=5)

l9 = Label(app, text="Ton:", font="Tahoma").grid(row=4, column=4, padx=5, pady=5)

l10 = Label(app, text="Price: ",font="Tahoma").grid(row=5, column=4, padx=5, pady=5)

#========================================================================
ev1 = StringVar()
e1 = Entry(app, textvariable=ev1)
e1.grid(row=1, column=2, columnspan=2, padx=5, pady=5)

ev2 = StringVar()
e2 = Entry(app, textvariable=ev2)
e2.grid(row=2, column=2, columnspan=2, padx=5, pady=5)

ev3 = StringVar()
e3 = Entry(app, textvariable=ev3)
e3.grid(row=3, column=2, columnspan=2, padx=5, pady=5)

ev4 = StringVar()
e4 = Entry(app, textvariable=ev4)
e4.grid(row=4, column=2, columnspan=2, padx=5, pady=5)

ev5 = StringVar()
e5 = Entry(app, textvariable=ev5)
e5.grid(row=5, column=2, columnspan=2, padx=5, pady=5)

#=========================================================================
def in_to_ton():
global ton
try:
ton = ((float(ev1.get())*float(ev2.get())*float(ev3.get()))/86400)*int(ev4.get())
return ton
except ValueError:
valueerror()

def ft_to_ton():
global ton
try:
ton = ((float(ev1.get())*float(ev2.get())*float(ev3.get()))/7200)*int(ev4.get())
return ton
except ValueError:
valueerror()

def mm_to_ton():
global ton
try:
inc = float(ev1.get())/25.4
ton = ((inc*float(ev2.get())*float(ev3.get()))/86400)*int(ev4.get())
return ton
except ValueError:
valueerror()

def print_one():
global pr, ans
if uom.get() == "Inch":
ans = StringVar()
label = Label(app, textvariable=ans, bg="pink",width=10,font="Tahoma")
label.grid(row=4, column=5)
ans.set(round(in_to_ton(),3))
pr = StringVar()
prlab = Label(app, textvariable=pr, bg="pink",width=10,font="Tahoma")
prlab.grid(row=5, column=5)
pr.set(round(float(ton)*float(ev5.get()),3))
elif uom.get() == "Feet":
ans = StringVar()
label = Label(app, textvariable=ans, bg="pink",width=10,font="Tahoma")
label.grid(row=4, column=5)
ans.set(round(ft_to_ton(),3))
pr = StringVar()
prlab = Label(app, textvariable=pr, bg="pink",width=10,font="Tahoma")
prlab.grid(row=5, column=5)
pr.set(round(float(ton)*float(ev5.get()),3))
elif uom.get() == "Millimeter":
ans = StringVar()
label = Label(app, textvariable=ans, bg="pink",width=10,font="Tahoma")
label.grid(row=4, column=5)
ans.set(round(mm_to_ton(),3))
pr = StringVar()
prlab = Label(app, textvariable=pr, bg="pink",width=10,font="Tahoma")
prlab.grid(row=5, column=5)
pr.set(round(float(ton)*float(ev5.get()),3))
else:
tkMessageBox.showerror("Unit of Measurement","Select UOM(Feet/Inch/Millimeter)")

def reset():
#global e1, e2, e3, e4, e5
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
pr.set("")
ans.set("")

def valueerror():
tkMessageBox.showerror("Wrong Value","Value must be numbers")
#=========================================================================

b1 = Button(app, text="Calculate", command=print_one, width=7)
b1.grid(row=7, column=2, padx=5, pady=5)

b2 = Button(app, text="Quit", command=exit, width=7)
b2.grid(row=7, column=3, padx=5, pady=5)

b3 = Button(app, text="Reset", command=reset, width=7)
b3.grid(row=7, column=5, padx=5, pady=5)

#=========================================================================

app.mainloop()