Pages

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()