Tuesday, October 2, 2012

VNC viewer: copy and paste text

If you are unable to copy text from and to a VNC client to your windows operating system, make sure the vncconfig application is running on the remote server.

Run "vncconfig &" from the vncviewer, and check all checkboxes.

From Unix to Windows from a VNC session:
1) Highlight the text you want to copy by holding down the left mouse button and dragging, just like in Windows. This copies it to the Windows clipboard.
2) Go to your Windows window and paste it from the clipboard as usual.

By the way, to paste it into the Unix window, put your cursor where you want it to go, then click the middle mouse button.

Reference:
http://www.unix.com/windows-dos-issues-discussions/40732-vnc-copy-paste-problem.html

redirect STDIN, STDOUT and STDERROR

You can redirect STDIN, STDOUT and STDERRORs when running applications from the shell (or command prompt).

To redirect STDIN, use "<". The command looks like:
$ some-command < /path/to/some/file

To redirect STDOUT, use ">". The command looks like:
$ some-program > /path/to/some/file

To append STDOUT redirect output use ">>".The command looks like:
$ some-program >> /path/to/some/file

To redirect STDERR, you need to know about File Descriptors (FD). Check the reference for additional details. You need to use "n>&m" operator. It means to redirect FD n to the same places as FD m. Eg, 2>&1 means send STDERR to the same place that STDOUT is going to. The command looks like
$ some-program >> /path/to/some/file 2>&1

Reference:
1. http://www.linuxsa.org.au/tips/io-redirection.html

Add external jar files in classpath when running Java programs from command prompt or shell

If you simply want to compile or run Java applications from command line, you can just add the jars to the classpath:
javac -classpath c:\path1\ext1.jar;d:\path2\ext2.jar de\example\MyClass.java

When running a java application, you have to add the classes folder as well
java -classpath bin;c:\path1\ext1.jar;d:\path2\ext2.jar de.example.MyClass

When running from command prompt on windows, you can also replace "bin" with a "."

Note that under *nix OS not a semicolon(;), but a colon(:) is used to separate classpath parts.

Reference:
1. http://www.coderanch.com/t/423052/java/java/solved-add-external-jar-windows

Run Java console application by double click

Assume you have written a console java application in Eclipse and want to distribute it to users as an executable. You export the class files as an executable JAR file from eclipse, however to run the JAR file you need to use Windows Command Prompt and type in the command
java -jar Filename.jar arguments

To make it more easily runnable - by a double click - we need to create a .bat file with the following contents  and present in the same folder as the JAR file.

@echo off
 set jarpath="JavaApp.jar"
 java -jar %jarpath% %CD%Config.txt
 PAUSE

Here, %CD% is a pseudo-variable which holds the working directory. It’s useful when you want to load a config file located in the same folder as your .bat file. You can also replace it with any arguments for the JAR file. PAUSE displays a localized version of “Press any key to continue…” message.

References:
http://blog.mwrobel.eu/how-to-properly-run-java-consol-application-with-a-doubleclick-windows/

Saturday, July 7, 2012

resize vmdk/vdi virtualbox


When using VirtualBox, you might come across situations when you need to resize the hard disk space.
The VM resize feature provided by VirtualBox only works for VDI & VHD and must be a dynamic drive.


Using VBoxManage clonehd you can convert the drive image from VMDK to VDI, and then use modifyd to change the virtual disk size. 


See Chapter 8.23 VBoxManage clonehd & then 8.22 VBoxManage modifyhd instructions in the reference.




References:
1. https://forums.virtualbox.org/viewtopic.php?f=3&t=46852
2. http://www.virtualbox.org/manual/ch08.html

Python easy_install proxy settings

Python easy_install may not take in the http_proxy environment variable.

It's a problem with sudo. If you use sudo, the variable $http_proxy is unknown in this context.

"sudo -i" opens up a root shell. There you can set the $http_proxy variable again and then easy_install works - you don't have to use sudo because you are already superuser.
$ sudo -i
# export http_proxy=http://proxy:port
# easy_install virtualenv

Or you can have your actual environment in sudo and the save the "get root step" via:
$ sudo -E easy_install virtualenv
References:
http://superuser.com/questions/258819/easy-install-will-not-connect-through-proxy

Configure Mercurial Proxy Settings


Edit the file "hgrc" with the following lines:
$ cat /etc/mercurial/hgrc
[http_proxy]
host=yourproxyhost:yourproxyport

See man hgrc for other options including name and password. You can verify the proxy is configured using the --debug flag:

References:
http://d.hatena.ne.jp/falkenhagen/20091007/1254909363

Linux Screen Command


What is Screen for Linux?
As the man page states, “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” This can be a life saver when working on your dedicated server. Screen has a several great features for helping you administer your server more productively and safely.

Installing Screen on Linux
Chances are that you already have screen on your system. To see if screen is in your path, you can use the which command:
which screen


Using Screen
Screen is started from the command line just like any other command:
screen
You may or may not get a text message about screen. If you do not, then you probably think nothing has happened, but it has. You are now inside of a window within screen. This functions just like a normal shell except for a few special characters. Screen uses the command "Ctrl-A" as a signal to send commands to screen instead of the shell. To get help, just use "Ctrl-A" then "?". You should now have the screen help page.


Key bindings are the commands the screen accepts after you hit "Ctrl-A". You can reconfigure these keys to your liking using a .screenrc file. The power of screen will become obvious, especially if you need to bounce around to different file system locations and leave processes running. For example, when I go in to clean out wasted disk space, I can remove files in one screen while hunting for other files in another.

Multiple Windows
Screen, like many windows managers, can support multiple windows. This is very useful for doing many tasks at the same time without opening new sessions.

To open a new window, you just use "Ctrl-A" "c". This will create a new window for you with your default prompt. For example, I can be running top and then open a new window to do other things.

You can create several windows and toggle through them with "Ctrl-A" "n" for the next window or "Ctrl-A" "p" for the previous window. Each process will keep running while your work elsewhere.


Leaving Screen

There are two ways to get out of screen. The first is just like logging out of a shell. You kill the window with "Ctrl-A" "K" or "exit" will work on some systems. This will kill the current windows. If you have other windows, you will drop into one of those. If this is the last window, then you will exit screen.

The second way to leave screen is to detach from a windows. This method leaves the process running and simple closes the window. If you have really long processes, you need to close your SSH program, you can detach from the window using "Ctrl-A" "d". This will drop you into your shell. All screen windows are still there and you can re-attach to them later. This is great when you are using rsync for server migration. Attaching to Sessions
Use the screen listing tool to see what sessions are running:
screen -ls
To re-attach to a session, use the re-attach command:
screen -r <session name>
Just use screen with the -r flag and the session name. You are now re-attached to the screen. A nice thing about this, is you can re-attach from anywhere. If you are at work or a clients office, you can use screen to start a job and then logout. When you get back to your office or home, you can login and get back to work.


Screen Logging
Using "Ctrl-A" "H", creates a running log of the session. Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.

Other Tips
Screen can monitor a window for activity or lack thereof. This is great if you are downloading large files, compiling, or watching for output. If you are downloading something or compiling, you can watch for silence. To start the monitor, go to the screen you want to monitor and use "Ctrl-A" "M" to look for activity or "Ctrl-A" "_" to monitor for silence. When the monitor detects activity or silence, you will get an alert at the bottom with the window number. To quickly go to that window, use "Ctrl-A" "(thats a quote mark, ctrl-a then a “). After you do this, just type in the number of the window and enter. To stop monitoring, go to that window and undo the monitor with the same command. For example, to stop monitoring for activity you would use "Ctrl-A" "M" again.


References:
http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/

Reverse SSH tunnel to connect to a machine inaccessible due to firewall or NAT


We cannot directly connect to a machine if it is behind a NAT (Network Address Translation) or a firewall. Almost all large networks (corporate and universities) have firewalls and may also employ NATs. Even home routers are now using some sort of NAT.

We need to create a reverse SSH tunnel to establish a connection. If you are familiar with Hamachi or gotomypc type software do the exact same thing  - they connect to computers behind NAT/Firewalls, only they use their severs as the middle man. We are going to have to find our middle man on our own.

In order for you to create a reverse tunnel you must have SSH access to a middle computer that you can connect to from origin computer.

Step-1:
On the destination computer type the following command. Replaceing middleuser with your name and replacing middle with the domain of the middle computer.
ssh -R 10002:localhost:22 middleuser@middle
This will open port 10002 for listening and forward all future connections to port 22 at destination. This connection must remain on the entire time to ensure that you can access your destination computer whenever you want.

Step-2:
Now if sshd is set to use GatewayPorts you should be able to connect with this:
ssh destinationuser@middle -p 10002
If you are not sure if GatewayPorts is on or you don’t have the access to change it use the following method to connect:

First connect to the middle computer how you would normally.
ssh user@middle
Then connect to the localhost of the middle computer on port 10002.
ssh user@localhost -p 10002
Note: The port 10002 is arbitrary you can use any port you want.

You should now be remotely logged into your computer behind the NAT/Firewall. Enjoy :)

Refer [2] to make the reverse ssh tunnel permanent, and restart automatically if the tunnel gets closed.

Reference:
1. http://www.marksanborn.net/howto/bypass-firewall-and-nat-with-reverse-ssh-tunnel/
2. http://www.clingmarks.com/setup-a-unbreakable-ssh-tunnel/21

Monday, June 25, 2012

ARP Cache size and interval configuration


It is possible to increase the size of engine’s ARP cache by changing values of /proc/sys/net/ipv4/neigh/default/gc_thresh3 (The hard maximum number of entries to keep in the ARP cache. Defaults to 1024.) and /proc/sys/net/ipv4/neigh/default/gc_thresh2 (The soft maximum number of entries to keep in the ARP cache. Defaults to 512.)

To change the timeout value of the entries, you need to modify the gc_interval file, indicating the timeout value in seconds. For other kernal variables checkout the man page of arp (http://www.kernel.org/doc/man-pages/online/pages/man7/arp.7.html).
Not sure how to make the changes permanent in ubuntu, but in the reference, the following is stated.
The changes should be made to /data/run-at-boot  file in order for changes to survive boot. The run-at-boot file must be created with execute permission for root.
Add the following lines to /data/run-at-boot file in all of the engines of the cluster:
#!/bin/sh
echo 8192 >/proc/sys/net/ipv4/neigh/default/gc_thresh3
echo 4096 >/proc/sys/net/ipv4/neigh/default/gc_thresh2
The change activates after boot or by executing the run-at-boot script. The change should be activated as simultaneously as possible in all the nodes of the cluster, so that the functioning of the nodes in the cluster would be same all the time.


References:
1. http://stoneblog.stonesoft.com/2009/02/arp-cache-overflow/
2. http://www.ibm.com/developerworks/opensource/library/os-iptables/index.html

Sunday, June 24, 2012

Floodlight installation on a machine within proxy.

When installing Floodlight openflow controller on a corporate machine behind corporate proxy, you might face some connection timed out errors and dependency errors. Some of the errors are:
[exec] [artifact:dependencies] Downloading: org/slf4j/slf4j-api/1.5.8/slf4j-api-1.5.8.pom from repository central at http://repo1.maven.org/maven2
[exec] [artifact:dependencies] Error transferring file: Connection timed out
.....
[exec] [artifact:dependencies] [WARNING] Unable to get resource 'org.slf4j:slf4j-api:pom:1.5.8' from repository central (http://repo1.maven.org/maven2): Error transferring file: Connection timed out
[exec] [artifact:dependencies] Downloading: commons-lang/commons-lang/2.5/commons-lang-2.5.pom from repository central at http://repo1.maven.org/maven2
[exec] [artifact:dependencies] Error transferring file: Connection timed out
.....
[exec] BUILD FAILED
.....
This happens after you download Floodlight through the proxy and are trying to install. Floodlight uses ant and maven packages, so appropriate changes need to be made for each to use the system proxy. The setting of environment variables http_proxy and https_proxy is not sufficient.

For ANT:
To compile floodlight using "ant eclipse" or "ant", setup proxy using:
export ANT_OPTS="-Dhttp.proxyHost=proxyname/ip -Dhttp.proxyPort=3128"

For Maven:
add ~/.m2/settings.xml with following content:
<settings>
  <proxies>
   <proxy>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy</host>
      <port>3128</port>
      <username>proxyuser</username>
    </proxy>
  </proxies>
</settings>

Using GIT from inside corporate proxy

Shamelessly lifted from the below reference.

Many corporate firewalls prevent git from using its efficient binary protocol by blocking outbound network connections. Sometimes, you are lucky and are trying to clone a repository that is hosted on a site like github which exports their repositories over HTTP, which would enable you to get through the firewall using the http_proxy environment variable. However, you are usually not that lucky and are only given a git:// URL to clone from.

Fortunately, most corporate firewalls allow for tunneling connections through their HTTP proxies, using HTTP CONNECT. This is normally used for allowing browser to connect to secure websites (using SSL over port 443), but if you are lucky, you can have your firewall administrator configure the proxy to also allow CONNECT for port 9418, which is the port used by git.

Once they have appropriately configured the proxy, you should then be able to use tools like netcat-openbsd or socat to connect through as follows…

STEP-1: Install `socat`. For example, on Debian/Ubuntu, just 'sudo apt-get install socat'.

STEP-2: Create a script called `gitproxy` in your bin directory;
#!/bin/sh
# Use socat to proxy git through an HTTP CONNECT firewall.
# Useful if you are trying to clone git:// from inside a company.
# Requires that the proxy allows CONNECT to port 9418.
#
# Save this file as gitproxy somewhere in your path (e.g., ~/bin) and then run
#   chmod +x gitproxy
#   git config --global core.gitproxy gitproxy
#
# More details at http://tinyurl.com/8xvpny

# Configuration. Common proxy ports are 3128, 8123, 8000.
_proxy=proxy.yourcompany.com
_proxyport=3128

exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport

You will need to replace proxy.yourcompany.com with the name of your proxy host and the port with the port used by the proxy (common ports include 3128, 8123 and 8000).

 STEP-3: Configure `git` to use it:
 git config --global core.gitproxy gitproxy

References:
http://www.emilsit.net/blog/archives/how-to-use-the-git-protocol-through-a-http-connect-proxy/

apt doesnt work with system proxy


System proxy details are generally indicated in the environment variables "http_proxy" and "https_proxy". They can be set by applying System Wide Proxy in the GUI.

They can be set from shell or terminal using:
export http_proxy="http://proxy:port"
export https_proxy="https://proxy:port"

You can check using:
echo $http_proxy
echo $https_proxy

Sometimes apt-get doesn't not take in the system proxy information.
Check the file /etc/apt/apt.conf for proxy details. Sometimes, you might need to check /etc/apt/apt.conf.d and a separate proxy file. The proxy entries should be as below:

Acquire::http::proxy "http://proxy:port/";
Acquire::ftp::proxy "ftp://proxy:port/";
Acquire::https::proxy "https://proxy:port/";

You might need to add username and password information, as below:
Acquire::http::proxy "http://<username>:<password>@<proxy>:<port>/";
Acquire::ftp::proxy "ftp://<username>:<password>@<proxy>:<port>/";
Acquire::https::proxy "https://<username>:<password>@<proxy>:<port>/";

Save the file once you make the changes.

TIP: Add these lines in another file, /etc/apt/apt.conf.d/80proxy. This will ensure that after a version upgrade changes won't be lost.

References:
http://askubuntu.com/questions/89437/how-to-install-packages-with-apt-get-on-a-system-connected-via-proxy

Fedora - Ubuntu differences


When working with fedora16, I noticed there were significant differences in the commands used by Fedora and Ubuntu. In most cases, the trick lies in identifying the command equivalents between the two distributions. 
Fedora mostly uses "yum" and Ubuntu generally uses "apt-get".


gcc, g++ compilers, some other generic packages and utilities

In Ubuntu, we use build-essential.  

sudo apt-get install build-essential

Its equivalent in fedora with yum is

su
yum groupinstall "Development Tools"
yum install kernel-devel kernel-headers

References:
http://forums.fedoraforum.org/showthread.php?t=198819
http://blog.htbaa.com/news/yum-equivalent-for-apts-build-essential

Monday, May 21, 2012

Vertical xtick Labels in Matlab graphs

This was taken from the mailing list entry available at: https://mailman.cae.wisc.edu/pipermail/help-octave/2009-July/036066.html
--------------------------------

here is a script that illustrates how to rotate the xtick label and put them vertically.

clear
## init demo plot
clf
plot(1:4);
xtick=[1 2 3 4];
set(gca,'xtick',xtick);
xticklabel=["a";"b";"c";"d"];
set(gca,'xticklabel',xticklabel);

## get position of current xtick labels
h = get(gca,'xlabel');
xlabelstring = get(h,'string');
xlabelposition = get(h,'position');

## construct position of new xtick labels
yposition = xlabelposition(2);
yposition = repmat(yposition,length(xtick),1);

## disable current xtick labels
set(gca,'xtick',[]);

## set up new xtick labels and rotate
hnew = text(xtick, yposition, xticklabel);
set(hnew,'rotation',90,'horizontalalignment','right');

Saturday, May 19, 2012

Xor Range without LOOP

I am lazy, and I wont be able to explain it more clearly than him. Hence, I lifted the content directly from there, to have all the coding tricks in my blog for my reference. 
----------------------
HOW to get Xor between numbers from 1 to 300000000
you will get TLE if you try to use Loop for all numbers
Question was asked on the codeforces site. Click Here to see the discussion 

jacob answer
Let's introduce
 f(x) = x ^ (x-1) ^ (x-2) ^ ... ^ 1
Then anwer would be  
f(end) ^ f(start - 1)


Let's now learn to calculate f(x) fast enough. First, notice that f(4*k - 1) = 0 (provable by induction). So to calculate f(x) you only need to take at most 4 last numbers and xor them.
Finally,
f(4*k) = 4*k
f(4*k + 1) = 1
f(4*k + 2) = 4*k + 3
f(4*k + 3) = 0 


Expressions "at most 4 last numbers" and "only last 4 numbers" are different. In fact, you need to take exactly (x+1)%4 last numbers.

Conclusion:Use this function 


int f(int n){  
switch(n%4){  
case 0: return n;  
case 1: return 1;  
case 2: return n+1;  
default: return 0;  
}  
}  


Saturday, April 7, 2012

Iterative, recursive and heap algorithms to generate permutations

A very good article on iterative and recursive algorithms for generating permutations of an array is here:
http://www.cut-the-knot.org/do_you_know/AllPerm.shtml

I shall shamelessly lift the algorithms listed in the page, for my reference here in the blog.
(Just for reference, another good algorithm is described at http://introcs.cs.princeton.edu/java/23recursion/Permutations.java.html)
Recursive algorithm for Permutations
It's executed with a call visit(0). Global variable level is initialized to -1 whereas every entry of the array Value is initialized to 0.
  void visit(int k)
  {
    level = level+1; Value[k] = level;    // = is assignment
    if (level == N)  // == is comparison
      AddItem();     // to the list box
    else
      for (int i = 0; i < N; i++)
        if (Value[i] == 0)
          visit(i);
    
    level = level-1; Value[k] = 0;
  }

  void AddItem()
  {
  // Form a string from Value[0], Value[1], ... Value[N-1].
  // At this point, this array contains one complete permutation.
  // The string is added to the list box for display.
  // The function, as such, is inessential to the algorithms.
  }

Lexigraphic order to find the next permutation
Permutation f precedes a permutation g in the lexicographic (alphabetic) order iff for the minimum value of k such that f(k)≠ g(k), we have f(k) < g(k). Starting with the identical permutation f(i) = i for all i, the second algorithm generates sequentially permutaions in the lexicographic order. The algorithm is described in [Dijkstra, p. 71].
  
  void getNext()
  {
    int i = N - 1;
    while (Value[i-1] >= Value[i]) 
      i = i-1;

    int j = N;
    while (Value[j-1] <= Value[i-1]) 
      j = j-1;
  
    swap(i-1, j-1);    // swap values at positions (i-1) and (j-1)

    i++; j = N;
    while (i < j)
    {
      swap(i-1, j-1);
      i++;
      j--;
    }
  }
B. Heap's algorithm 
Heap's short and elegant algorithm is implemented as a recursive method HeapPermute [Levitin, p. 179]. It is invoked with HeapPermute(N).
  
void HeapPermute(int n)
{
  if (n == 1) 
    AddItem();
  else {
    for (int i = 0; i < n; i++) 
      HeapPermute(n-1);
      if (n % 2 == 1)  // if n is odd
        swap(0, n-1);
      else            // if n is even
        swap(i, n-1);
}

Monday, March 12, 2012

MongoDB and Python

For my project I had to store and operate on a large amount of data. I was mostly using dictionaries and lists in Python. Since the storing entire dictionary/list in memory was impossible, I was looking for alternatives. The obvious fall back option was to use disk storage. Persistent storage databases like MongoDB allow directly storing python objects on disk.

To install MongoDB on windows, I followed the quick tutorial provided on the MongoDB site: http://www.mongodb.org/display/DOCS/Quickstart+Windows. Don't forget to check the --dbpath directive while running mongod.exe, as it helps to specify the database path.

Being new to MongoDB, and having prior experience with SQL, it is good to know how the two approaches differ. The tutorial provided at http://www.mongodb.org/display/DOCS/Tutorial, gave a very good overview of how MongoDB works.

To setup Python to interact with MongoDB, follow the instructions here: http://www.mongodb.org/display/DOCS/Python+Language+Center

To learn about setting some fields unique (apart from the _id field), and to create indexes to boost the search latency, read tutorials here http://www.mongodb.org/display/DOCS/Indexes#Indexes-unique%3Atrue.

The book titled "MongoDB and Python:Patterns and Processes for the Popular Document-Oriented Database" provided all the needed python commands to interact with the database. It was very helpful.