27 January, 2011

python xmlrpc : a simple client server connection mechanism with greater possibilities

A byte of theory . . .

XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data. This module supports writing XML-RPC client code; it handles all the details of translating between conformable Python objects and XML on the wire.

A byte of surprise . . .


Here the xmlrpc library of the python does the magic, by leaving the  programmer give maximum attention to logic by freeing him from the concepts of the sockets, binding, listening, accepting, etc ... which are the primary constraints while developing a  network based program. Remote Procedure Call (RPC) mechanism allows the client to call any function defined and registered as 'remote' in the server.

A byte of reality . . .


xmlrpc library offers communication between the client and server in such a way that, we can define functions in one machine (say a server) and access those functions from the other machine (say a client). Here the communication occurs in two ways.
    >< client calling the remote server function can 
           pass arguments to it. So the server recieves this 
           arguments 
   ><  Server can transport some data as the return 
           value of the function that the client called

A byte of practical . . .


    Inorder to make the client and server communicate together we need to create the server object and client object with the ip and port of the server. Here i am running the client and server on the same machine. You can replace 127.0.0.1 in sample code with the ip of the server where you need to connect the clients. Port should be greater than the 1024.

server program

   import xmlrpclib
   from SimpleXMLRPCServer import SimpleXMLRPCServer
   def add(a, b) :
         return a+b
   server=SimpleXMLRPCServer(("127.0.0.1",9009))
   server.register_function(add)
   server.serve_forever()


In server program we have to create the object of the server with a method called SimpleXMLRPCServer() with parameters as ip and port. Then the function has to be registered inorder to access it remotely. The serve_forever() function is used to put the server wait for the client request infinitely.
client program
    
    import xmlrpclib
    server = xmlrpclib.ServerProxy('http://127.0.0.1:9009')
    a=input("No 1: ")
   
b=input("No 2: ")
    result = server.add(a,b)
    print "Result  :  "+result

In the client program we will use the ServerProxy() method to specify the details(port, ip) of the server which is having a function named add() which can be called remotely. So two integers a,b are passed as parameters to the remote function. Remote function calculates the sum and returns the result to the called function.

I felt this xmlrpc library as the most awesome mechanism provided by the python. It makes the programming to the very simplest extent so that the users can give maximum attention to developing logic, instead of worrying about the networking constraints and errors related to it...



 
Read rest of entry

11 October, 2010

Troubleshooting Network problems in Ubuntu


                Internet connectivity is one of the important means of communication in todays life. So troubleshooting the problems in our own networks or reconfiguring it for the easiness is not to be praised as a "geekness". In my opinion you don't need to be a geek for setting an internet connection or recovering your machine from a network problem.

Before getting into more troubles just checkout wheather the physical network components (cables, NICs, and so on) are connected properly and working. If everything is right then proceed to the following steps



Checking the Connectivity to a Host

The troubleshooting is done in a bottom up manner. So we will troubleshoot the network layer first and then move on to transport layer. Here we will check the connetivity by pinging  to your default gateway. You should have either configured the default gateway in the /etc/network/interfaces file, i have written a post about it in the last month or leave it to the system using service such as DHCP. To check your default gateway in the actual routing table, use the ip command as follows:

$ ip route


The gateway for the default route in this example is  192.168.0.5 To make sure wheather there is internet connectivity to that gateway, use the ping command as follows, passing the address for your default gateway:

$ ping 192.168.0.5

By default, ping continues until you press Ctrl+c. Other important ping options include the following:
$ ping -a 10.0.0.1
            [You can here a buzzer sound @ each pings]
$ ping -c 4 10.0.0.1 
            [Ping 4 times and exit (default in Windows)]
$ ping -q -c 5 10.0.0.1 
            [Show summary of pings (works best with -c)]
$ sudo ping -f 10.0.0.1 
            [Send a flood of pings (must be root)]
$ ping -i 3 10.0.0.1 
            [Send packets in 3-second intervals]
$ sudo ping -I eth0 10.0.0.1  
            [Set source to eth0 (use if multiple NICs)]



Use the ping flood option with caution, if the packet size is larger than usual  it can cause connections to stand out.

Checking Address Resolution Protocol (ARP)


If you’re not able to ping your gateway, you may have an issue at the Ethernet hardware address layer. The Address Resolution Protocol (ARP) can be used to find information at the MAC layer (hardware layer). 

ARP stands for Address Resolution Protocol, which is used to  find  the  media  access control [MAC] address of a network neighbour for a given IP Address.


To view and configure ARP entries, we can use the arp or ip neighbor command. Arp  command displays  the  kernel’s IPv4 network neighbour cache. It can add entries to the table, delete one or display the current content.

$ arp -v    [List ARP cache entries by name]



Above command will show the names of other computers that the local computer’s ARP cache knows about and the associated hardware type and hardware address of each computer’s NIC. You can disable name resolution to see those computers’ IP addresses instead of the hostname by using the following command:

$ arp -vn     [List ARP cache entries by IP address]

          To delete an entry from the ARP cache, use 
          the -d option:
              $ sudo arp -d 10.0.0.50
                   [Delete address 10.0.0.50 from ARP cache]

          Instead of just letting ARP dynamically learn 
          about other systems, you can add static ARP 
          entries to the cache using the -s option:
               $ sudo arp -s 10.0.0.51 00:0B:6A:02:EC:95


To query a subnet to see if an IP is already in use, and to find the MAC address of the device using it, use the arping command. The arping command continuously queries for the address until the command is ended by typing Ctrl+c. Typically, you just want to know if the target is alive, so you can run one of the following commands:

$ arping -f 10.0.0.50 
[Query 10.0.0.50 and stop at the first reply]

                      
$ arping -c 2 10.0.0.51 
[Query 10.0.0.50 and stop after 2 counts]


Tracing Routes to Hosts

After verifying that you can ping your gateway and even reach machines that are outside of your network you can use traceroute (traceroute package should be installed) to find the problem correctly.

$ traceroute  www.mywebsite.com  
[Follow the route taken to a host]
 

If there are lines of asterisks (*) in the end of  the list  it is because of the firewalls inside your office/college network that block traffic to the target. However, if you see several asterisks before the destination, those can indicate congestion or network failures and points to bottleneck issues.

traceroute command options are
$ traceroute -I www.google.com 
                        Use ICMP packets to trace a route
$ traceroute -p 25 www.google.com 
                        Connect to port 25 in trace
$ traceroute -n www.google.com 
                        Disable name resolution in trace
$ tracepath www.google.com 
                        Use UDP to trace the route

Using the ip command...
You can do the same activities with ip command similar to route command. Here are three different ways to show the same basic routing information:

$ ip route show 
[Display basic routing information]

options for adding and deleting routes with ip are as follows:
$ sudo ip r add 192.168.0.0/24 via 10.0.0.100 dev eth0                  - Add route to interface
$ sudo ip r add 192.168.0.0/24 via 10.0.0.100  
                           - Add route no interface
$ sudo ip r del 192.168.0.0/24 
                           - Delete route

To make a new route permanent, edit the /etc/network/interfaces file and place the information about the new route in that file. For example, to add the route added with the ip command above, add the following lines to /etc/network/interfaces:
       iface eth0 inet static
    address 192.168.0.0
    netmask 255.255.255.0
    gateway 10.0.0.100

Read rest of entry

07 October, 2010

Complete reference to SSH tunnelling

an introduction . . .

In simple words tunneling is a method of bypassing firewall or proxy restrictions using some tunnelling protocols. It works by creating a "tunnel", or a communications channel that makes the firewall think that it is getting traffic from a web browser. Communications content is delivered through this tunnel to our gateway or your own personal gateway. The gateway extracts your content from the tunnel and sends it to the destination. When your counter party responds everything goes back the same way. Tunnelling can be used when your company, college, university, ISP blocked certain programs, FTP, telnet.

On the other hand, tunneling can also allow inherently insecure protocols to cross your firewall. For this reason, it may be advantageous to use a firewall solution that does content based checking of HTTP connections, so that you can disallow connections that are actually tunneling other protocols. This can be quite difficult to do. SSH tunnelling techniques have a wide range of applications among the other tunnelling protocols.  


SSH tunnelling ...
To set up an SSH tunnel, one configures an SSH client to forward a specified local port to a port on the remote machine. Once the SSH tunnel has been established, the user can connect to the specified local port to access the network service. For setting up the ssh application in your machine please  read my previous post from here

A Secure Shell (SSH) tunnel consists of an encrypted tunnel created through an SSH protocol connection. Users may set up SSH tunnels to transfer unencrypted traffic over a network through an encrypted channel.

Configuring the ssh tunnel

X11 forwarding
Ubuntu comes with X11 forwarding facility for the server. We have to enable for the current session only on the client side using the following command 

$ ssh –X  user@myserver

To enable X11 forwarding permanently, you should edit the file ssh_config as sudo  as follows

    $ sudo gedit /etc/ssh/ssh_config

     # for all users, add the line ForwardX11 yes

     # To enable it permanently for a specific user 
         only, add the line to that user’s ~.ssh/config file.
 
Once that setting has been added, the -X option is no longer required to use X11 Tunneling. Run ssh command to connect to the remote system as you would normally. 

To test that the tunneling is working, run xclock after ssh’ing into the remote machine, and it should appear on your client desktop.Thus SSH Tunneling is an excellent way to securely use remote graphical tools!

Logging in Remotely with ssh


To securely log in to a remote host, you can use either of two different syntaxes to specify the user name:

$ ssh -l  user  myserver
or
$ ssh user@myserver

 
Accessing SSH on a Different Port

For security purposes, a remote host may have its SSH service listening a different port than the default port number 22. If that’s the case, use -p option to ssh to contact that service:

$ ssh -p 12345 user@myserver.com
[Connect to SSH on port 12345]


Applications

One of the important advantage of the SSH is that you can forward any TCP port with SSH. This is a great way to configure secure tunnels quickly and easily. No configuration is required on the server side.

Tunneling for remote X11 Clients  

                   
                Inorder to use applications that are available on a remote machine follow the steps

1.[Start ssh connection to myserver]
$ ssh user@myserver
Then terminal for prompt for the password

2.Run the applications
 
$ echo $DISPLAY    -Show the current X display entry
$ xeyes                  -Show moving desktop eyes
$ gnome-cups-manager     -Configure remote printers
$ gksu services-admin       -Change system services
$ firefox                             -starts the web browser

Tunneling for Remote Printing Administration               
                 Let myserver is a print server with the CUPS printing service’s web-based user interface enabled (running on port 631). That GUI is only accessible from the local machine. We tunnel to that service in myserver from the client pc using ssh with the following options:

$ ssh -L 1234:localhost:631 myserver

This example forwards port 1234 on the client PC to localhost port 631 on the server. We can now browse to http://localhost:1234 on the client PC. This will be redirected to cupsd listening on port 631 on the server.

Tunneling to an Internet Service
 

                 Another example for using SSH tunneling is when your local machine is blocked from connecting to the Internet (using proxies, firewalls ...), you can get to another machine (myserver) that has an Internet connection and utilize its connectivity.

The following example lets you visit the Google.com web site (HTTP, TCP port 80) across an SSH connection to a computer named myserver that has a connection to the Internet:

$ ssh -L 12345:google.com:80 myserver

With this example, any connection to the local port 12345 is directed across an SSH tunnel to myserver, which in turn opens a connection to Google.com port 80. You can now browse to http://localhost:12345 and use myserver as a relay to the Google.com web site. 

Since you’re only using ssh to forward a port and not to obtain a shell on the server, you can add the –N option to prevent the execution of remote commands:

$ ssh -L 12345:google.com:80 –N myserver
 

Tunnelling to use SSH as a SOCKS Proxy 

                 Inorder to utilize the full power of tunnelling the best way is to get your browser traffic out of your local network via an encrypted tunnel is using the SSH built-in SOCKS proxy feature.This will make you anonymous (in ip) in the outside world and let you break through the firewall/proxy restrictions imposed on your network.

$ ssh -D 12345 myserver

The dynamic (-D) option of ssh lets you log in to myserver (as usual). As long as the connection is open, all requests directed to port 12345 are then forwarded to myserver.

Next, set your browser of choice to use localhost port 12345 as a SOCKS v5 proxy. Do not enter anything on the fields for HTTP and other protocols, They all work over SOCKS.




Let me conclude...

The SSH tunnelling can be used as a power tool for comunication in environments where limited connectivity is available due to proxies, firewalls and content filtering mechanisms. It provides a secure encrypted channel channel to hide your data from being monitored by anyone. Also the remote access concept will bring your office into your bedroom so that you can login and work on your office machine with your home pc....
Have a nice time in tunnelling ... 

Read rest of entry

05 October, 2010

Copying Remote Files with scp



To use scp to transfer files, the SSH service (usually the sshd server daemon) must be running on the remote system. Here are some examples of useful scp commands:






$ scp myfile francois@server1:/tmp/ 
Password: ******
Above code copies myfile to server1


$ scp server1:/tmp/myfile 
Password: ******
Copy remote myfile to local working directory. Use the -p option to preserve permissions and timestamps on the copied files:

$ scp -p myfile server1:/tmp/If the SSH service is configured to listen on a port other than the default port 22, use -P to indicate that port on the scp command line:

$ scp -P 12345 myfile server1:/tmp/
 
Connect to a particular port.To do recursive copies, from a particular point in the remote file system, use the -r option:

$ scp -r mydir francois@server1:/tmp/ 
Copies all mydir to remote /tmp .Although scp is most useful when you know the exact locations of the file(s) youneed to copy, sometimes it’s more helpful to browse and transfer files interactively.

Read rest of entry

25 September, 2010

How to assign an IP address in Ubuntu using command line

Computers may be assiged a static IP address or assigned one dynamically. Typically servers and institutions will use a static IP which will not change each time u get connected. Workstation will use Dynamic Host Configuration Protocol (DHCP) for IP address assignment. It is more easily to find a system if the IP address does not change and is static.


Use the Command Line:

    /sbin/ifconfig eth0 192.168.10.12 netmask 255.255.255.0 broadcast 192.168.10.255                  
 
The ifconfig command does NOT store this information permanently. Upon reboot this information is lost. Manually add the network configuration to /etc/network/interfaces  as shown in my next post .
Read rest of entry

IP Configuration in Ubuntu

Here is a howto about the configuring ip using networking related files in Ubuntu / Debian systems.

File: /etc/network/interfaces
    This file contains network interface configuration information  for the ifup and ifdown commands. This is where you configure how your system is connected to the network.

Three important Interfaces are:
        # lo: Loopback interface (it is an internal 
            networking mechanism. It is used to
            test applications...)
        # eth0: First ethernet interface card
        # wlan0: First wireless network interface


Use following command to edit the interfaces file

$ sudo gedit /etc/network/interfaces

Add one or more of the stanzas below 

    Static IP example:
    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet static
            address 208.88.34.106
            netmask 255.255.255.248
            broadcast 208.88.34.111
            network 208.88.34.104
            gateway 208.88.34.110
                   

    Dynamic IP (DHCP) example:
    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet dhcp

    auto eth1
    iface eth1 inet dhcp

    auto eth2
    iface eth2 inet dhcp

    auto ath0
    iface ath0 inet dhcp

    auto wlan0
    iface wlan0 inet dhcp
                   
    Lines  beginning with the word "auto" in the interfaces file are used to identify the physical interfaces to be brought up when ifup is run with the -a option.  (This option  is  used by the system boot scripts.)  Physical interface names should follow the word "auto" on the same line.

GUI Network Tools:

    * NetworkManager or wicd can be used as the GUI tools for the network management. Better use wicd network manager if you have to rely more upon the wireless internet than the wired ones
.
Read rest of entry

29 April, 2010

How to find the open ports when you get connected

This post is all about the open ports created in your system. This ports are mainly tcp or udp ports. 

Ports below 1024 are reserved for common services, and only root can use them. Standard port numbers can be found in /etc/services. 

Take your terminal and type  in as follows to see them

$sudo gedit /etc/services

The maximum number of ports is 65k, so you have more than enough Internet ports for all your services. Here are some useful utilities. Netstat is a command that will list both the open ports and who is connected to your system. You should run it like this:

$netstat -an | more

This way you can find out who is connected to which service. Another interesting command is the fuser program. This program can tell you which user and  process owns a port. For example, the following command will tell you who owns port 0
$fuser -v -n tcp 0


Read rest of entry

24 April, 2010

How to connect Windows XP to Ubuntu over LAN cable

                 This post is all about connecting Ubuntu and Windows system using a LAN cable for sharing data between them . . . A LAN cable is a preferable mechanism for sharing files in a good speed in the range of Mbps.

Connection requirements :
        LAN cable
        Ubuntu linux with Samba (or any other file sharing application)
        Windows machine

Connect LAN cable to both of the systems. On connecting both the Windows and Ubuntu try to connect automatically between each other.

Now follow the steps below... 

Configuring the Linux system


1.  Right click on the Network manager icon on the top sidebar. Select 
     "Edit  connections".

2.  Under the "wired" tab on the pop up Network connections window choose the
     "add" button to add a new connection.

3.  Make changes in the opened window as in figure

        # specify the connection name as u like. I call it "Ubuntu XP network"
        # Check the connect automatically box enabled
        # Click on the IPv4 settings
            * Click on the methode field and choose "Manual"
            * Click below the address feild and enter an arbitray ip say, 
               192.100.100.1                   ....you can choose it your own
            * Add 255.255.255.0 in the Netmask (no options for you, enter it as if)
        # click on the apply button to make the changes available.



4. That's all you need to do in Ubuntu machine.


Configuring the Windows system

1.  Open the Network Connections folder from Start ---> Network Connections

2.  Right click on the connection with the name "Local Area Connection" and 
     click on the properties


3.  Under the general tab select Internet Protocol(TCP/IP) and click 
     "properties"


4.  Choose 'Use the following IP address' and enter
        IP Address  :  192.100.100.3
        Subnet Mask :  255.255.255.0




5.  Press the OK button to complete the configuring.

            Thats all about configuration. Now restart the connection from the Ubuntu system using network manager (No need to pull out the cable physically)
Inorder to share a particular file from windows, right click on the file and select properties.In the properties window check the corresponding
feilds related to sharing under the "sharing" tab.

To access those files from Ubuntu system, goto Places ---> Network ---> Windows network
Read rest of entry
 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger