Skip to main content

Posts

Showing posts from 2012

Python Pipe

Pipes can be used in python using the popen command. os.popen(command[, mode[, bufferSize]]). The default mode is read which can also be explicitly indicated by "r" as shown in the example below. The parameter bufferSize can be 0 (no buffering), 1 (line buffering), negative(system default buffering) and positive values greater than 1 (number defines the buffer size to be used). #Open a pipe to or from command import os # shell command to show all the files in current directory with ".py" in the file name shellCommand = ' ls | grep .py' pipe = os.popen(shellCommand,"r") while True: # read each output line line = pipe.readline() if not line: break print 'outputLine: ' print line

calculate hop count in a graph using python

The following program finds out the hop count of the nodes in a connected network. Hop Count means the number of point to point connections in a graph of network. For example, in the graph shown below the hop count from node a to node b is one, node b to node c is two, etc. This program assumes the graph given above. usage : getHopCount(dictNeigh, node1,node2) Where:   dictNeigh : dictionary of list of neighbor of each node in the network node1 and node2 are the nodes for which hop count is calculated by the program Exception: if the nodes are not connected then it returns Zero import copy dictNeigh = dict() # dictNeigh defines the neighbors dictNeigh['a'] = ['b','c','d'] # neighbors of 'a' dictNeigh['b'] = ['a']# neighbors of 'b' dictNeigh['c'] = ['a','d'] dictNeigh['d'] = ['a','c','e'] dictNeigh['e'] = ['d','f'] dictN

ubuntu (10.04) apache web server not running due to port conflict

Sometimes the port assigned to apache2 may be used by some other services. In such cases, if we try to live apache2 server then it fails.  The following message is shown in ubuntu 10.04 in my case: Starting web server apache2                                                   apache2: Could not reliably determine the server's fully qualified domain name, using 0.0.0.3 for ServerName (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs Here is one of the ways to overcome this problem: 1. see who is using the port 80 : sudo lsof -i :80 2. get the pid of the services in 1. E.g.: 1577 3. kill all the services in 2. : sudo kill 1577 4. restart apache2: sudo /etc/init.d/apache2 restart

Display dynamic content in Google Earth using KML

Keyhole Markup Language (KML) is a xml standard which can be used to define what and how to present geographic data in Google Earth and Google Maps. Useful examples to start can be browsed via https://developers.google.com/kml/documentation/kml_tut . I have to display KML data in googleearth. And the KML file has to be loaded dynamically and the updates in the kml file should be shown in the googleearth on the fly. For this we have to Create a Network Link. Goto Add -> Network Link Then specify the details as shown in the figure. To specify the frequency of updating the data, go to Refresh tab. The code for dynamicKML.kml is as follows: <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.1"> <document> <networklink> <link> <href>junk.kml</href> <refreshmode>onInterval</refreshMode> <refreshinterval&g

python code for querying own IP address

The  socket.gethostname() function is supposed to give the IP address of the machine but it may not give the real IP address but gives 127.0.0.1 . Even it is said that removing 127.0.0.1 from the /etc/hosts file (in ubuntu) can give the real IP but it could not help in my case. import socket nodeName = socket.gethostname() info = socket.getaddrinfo(nodeName, None, 0, socket.SOCK_STREAM) print [x[4][0] for x in info]   # prints 127.0.0.1 I came through another way which can print the real IP as well: import commands NodeIPS = commands.getoutput("/sbin/ifconfig | grep -i \"inet\" | grep -iv \"inet6\" | " +                          "awk {'print $2'} | sed -ne 's/addr\:/ /p'") print NodeIPS  # prints 127.0.0.1 and realIP(eg. 202.145.12.22) Note: code worked for ubuntu 12.04 and python 2.7 Sometimes its handy to have the IP address as list items. The following code does the work. import os listIPs=[] f=os.pope

Open file with unlisted application in ubuntu 12.04

In ubuntu 12.04, While browsing files in nautilus, if we want to open file with unlisted applications, for e.g. open a file with idle(but idle is not listed in the list of open with ). Enter the following command in the terminal: gksudo gedit /usr/share/applications/idle.desktop Then remove the line: Exec=/usr/bin/idle -n Replace it with: Exec=idle %U Now we can see idle listed in the list of open with applications. To set it as default application to open a file. Go to the file properites, select idle and click set as default.

Metaheuristics

Generally speaking heuristics aim to find a good enough solution to a problem in a reasonable amount of time. These are mostly based on experiences and common sense. Metaheutistics iteratively tries to improve a candidate solution with respect to a given measure of quality.They rarely make assumptions about the problem being optimized They can search very large spaces of candidate solutions. Whereas they do not guarantee the finding of an optimal solution. Some of the popularly used metaheuristics for optimization problems are: • Simulated Annealing (SA), • Particle Swarm Optimization (PSO), • Differential Evolution (DE), • Genetic Algorithm (GA), • Evolutionary Strategy (ES).

secure shell (ssh)

Two machines running a server and a client running respectively can be connected securely via ssh.  ssh user@serverIP example: ssh tom@203.159.11.42 Every time a user logs in to the server user have to provide password for getting into the server.Whereas passwordless authentication using the public key is considered more secure while loggin to a remote server. For that the following steps should be followed: 1. Generate public/private rsa key pair: ssh-keygen -t rsa When password is asked just hit a "ENTER" to leave it empty 2.Now use ssh to create a directory ~/.ssh as user (e.g. "tom") on server(e.g. 203.159.11.42). ssh tom@203.159.11.42 -p .ssh 3.Copy key to remote server cat .ssh/id_rsa.pub | ssh tom@203.159.11.42 'cat >> .ssh/authorized_keys' After finishing these three steps, login to the remote server with ssh will not require password.

Installing NS-3 in Ubuntu

Installing testing it and running NS-3 in Ubuntu 10.04 NS-3 is a discrete-event network simulator basically for simulating Internet systems. #The list of Dependency for ns 3.12(sorry if something is missing): sudo apt-get install gcc g++ python sudo apt-get install gcc g++ python python-dev sudo apt-get install mercurial sudo apt-get install bzr sudo apt-get install gdb valgrind sudo apt-get install gsl-bin libgsl0-dev libgsl0ldbl sudo apt-get install flex bison sudo apt-get install g++-3.4 gcc-3.4 sudo apt-get install tcpdump sudo apt-get install sqlite sqlite3 libsqlite3-dev sudo apt-get install libxml2 libxml2-dev sudo apt-get install libgtk2.0-0 libgtk2.0-dev sudo apt-get install libgtk2.0-0 libgtk2.0-dev sudo apt-get install vtun lxc sudo apt-get install uncrustify sudo apt-get install doxygen graphviz imagemagick sudo apt-get install texlive texlive-pdf texlive-latex-extra texlive-generic-extra texlive-generic-recommended sudo apt-get install python-sphinx dia t

Graph: Ms Excel 2010

To plot a graph in Microsoft Excel 2010 by taking the column data as axis in the graph: 1.Highlight the column data that you wish to plot. 2.Insert-> scatter graph If you wish to plot a chart of X1 and X2 against Y X2 X1 Y 88 123 100 188 234 200 350 345 300 500 456 400 Select these columns, goto insert and then click scatter chart. This will give the following chart: But this is not what we want. Now plot X2 and Y , then plot X1 and Y. Then copy the curve from a plot and paste the curve to another plot. If you wish to edit the style of the plot. For e.g. changing the thickness of the graph line: 1. Right click on the graph -> format data series -> line style -> width (you can specify the thickness value here) If you wish to scale the range of x-axis and y-axis data values: 1. Double click on th ex-axis -> axis options -> set the minimum/maximum values ( you can also change other values as you need) If you wish to label some data points: 1. Double click on the curve -&

VirtualBox in ubuntu10.04

VirtualBox is a virtualization tool which allows us to install and run different OS like windows and linux. I had tried installing ubuntu10.04 over ubuntu 10.04. To install it in Ubuntu (10.04 for now): i. go to Ubuntu Software Center and search for Oracle VM VirtualBox, then click install. ii. Goto virtualbox.org -> Downloads and get the package that you need for your system (For e.g. ".deb" package for Ubuntu) iii. Install virtualbox from commandline: sudo apt-get update sudo apt-get install virtualbox-4.1 If writing the version number fails then trying without version number might work: sudo apt-get install virtualbox After installing the Virtualbox, we can install any Operating System over it. Installing a new OS is more or less similar to installing an OS in a new computer but you need to do some more before proceeding to install the new OS: i. Open the VirtualBox window, then Press New button and create a new Virtual Machine. ii. Specify the Name (e.g

Post Scripts in blogs

Posting scripts in the blogs is not straightforward. I came across some ways to do so: Method #1: Simple Approach: This will display the script as follows: Print("This is my code in codebox"); Method#2: Better Approach: 1. Login to your Blogger account. 2. Goto Design and then Edit HTML. Then you will see the script for your current blog template. 3. Backup your current template by clicking at "Download Full Template". You may skip this step if you are confident that you do not need it afterwards. 4. Find ]]></b:skin> in the code and add the following code just before ]]></b:skin> : .code {color: #006AB0; border : 1px solpd #DfADADA;padding : 5px 5px;font-family : Consolas, "Courier New", Courier, mono, serif;font-size : 12px;background-color : #fAfAfA; width : 90%;overflow : auto;margin : 10px 0 10px 10px; border-left : 20px solid #DADADA;max-height : 500px;min-height : 16px;line-height : 16px;} .code:hover {background-c

Search File with specific content in Ubuntu

The following command can be used to search any files that is under /home/myFolder and which has the content 'my search string'. The man page for the given command can be browsed for more detail information. grep -i -r -n 'my search string' /home/myFolder Where: -i=ignore case, -r=recursive, -n= give line number

JDK6 in Ubuntu 10.04

To install JDk 6 (Java Developement Kit 6 ) in ubuntu 10.04 from the command line, we just need to use the following command. This will install all other dependencies automatically.  sudo apt-get install openjdk-6-jdk