tag:blogger.com,1999:blog-32680306703013722082024-03-08T12:43:09.860-08:00techrcNotes from a software developer.Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-3268030670301372208.post-66367878750259670112010-11-24T19:46:00.000-08:002010-11-24T19:46:47.830-08:00Ubuntu desktop as a nas serverI had a spare Desktop in my basement with Ubuntu installed on it. So had, in past added a spare hard drive to it.So I decided to use it as my own Nas server. Use it to back up pictures, mp3s etc.<br />
<br />
Here's a list of steps that I had to follow in order to finally set it up:<br />
<b><br />
</b><br />
<b>Step 1</b>: Upgraded ubuntu to 10.4.<br />
<br />
<b>Step 2</b>: By default ubuntu will be configured with DHCP. That's no good. Got to fix he ip address so the network drive mounts do not break everytime the server starts up.<br />
<ul><li>The easiest way to do this is to edit /etc/network/interfaces (don't mess with the gui it was kinda flaky). </li>
</ul><ul><ul><ul></ul></ul></ul>-------------------------------- <br />
auto eth0<br />
iface eth0 inet static<br />
address 192.168.1.100<br />
netmask 255.255.255.0<br />
network 192.168.1.0<br />
broadcast 192.168.1.255<br />
gateway 192.168.1.1<br />
-----------------------------------<br />
<ul><li>edit /etc/resolv.conf. Add your DNS name servers here. Or just add 8.8.8.8 ( gooogle's free dns service) </li>
<li>now just restart networking: /etc/init.d/networking restart ( or sudo service networking restart)</li>
<li> >ifconfig should now tell you that the ipaddress has changed. ping google.com to make sure you are up and running. </li>
</ul><b>Step 3</b>: Install samba.<br />
<ul><li>The best resource that I found was this : http://ubuntuforums.org/showthread.php?t=202605. </li>
<li>Note that in the link above the setting "guest = ok". This will allow any user to access your samba share. It should be set to no.</li>
<li>Make sure that you set "security = user" and "users = winuser, otheruser" . This will restrict the access to users who have password.</li>
</ul><b>Step 4</b>: Map network drive in a windows machine. <br />
<ul><li>You might have to change the windows firewall setting for it to be able to connect to your shared network via smb.</li>
<li>Use map network drive option in Windows explorer to map your machine. Use the following for folder:</li>
</ul>//192.168.1.100/media/ <br />
<ul><li>Click on the "Connect using a different user name". link. Enter usename and password.<br />
</li>
</ul>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-10033941862166951312010-11-23T09:37:00.000-08:002010-11-23T09:53:30.312-08:00Date in java difference anyone ?Turns out it not simple function call to get the difference between two dates and display the result is a nicely formatted output. I ended up doing this (With liberal help from google )<br /><br /><table border="1" cellpadding="1" width="800"><tbody><tr><br /><td bgcolor="#aabbcc"><br />import java.util.*;<br />import java.text.*;<br /><br /><br />public class Test1 {<br /><br /> private static String getDateDifference(String format, Date d1, Date d2 ){<br /> long dl1 = d1.getTime();<br /> long dl2 = d2.getTime();<br /><br /> long sec2Millisec = 1000;<br /> long minute2Millisec = sec2Millisec*60;<br /> long hour2Millisec = minute2Millisec*60;<br /> long day2Millisec = hour2Millisec*24;<br /><br /> long diff = (dl2-dl1);<br /> long days = (diff)/(day2Millisec);<br /> diff = diff%day2Millisec;<br /> long hours = (diff)/(hour2Millisec);<br /> diff = diff%hour2Millisec;<br /> long mins = (diff)/(minute2Millisec);<br /> diff = diff%minute2Millisec;<br /> long secs = diff/sec2Millisec ;<br /> return String.format( format, days, hours, mins, secs);<br /> }<br /><br /> public static void main(String[] args){<br /><br /> try {<br /> Date d1 = new Date();<br /> Date d2 = new Date(d1.getTime() + 1113660*1000);<br /> System.out.println (Test1.getDateDifference(" %d days, %d hours, %d mins, %d secs", d1, d2));<br /><br /> }catch (Exception e){<br /> System.out.println("err = " + e);<br /> e.printStackTrace();<br /> }<br /><br /> }<br />}<br /><br /></td></tr></tbody></table>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-10786897554776472692010-06-21T11:16:00.000-07:002010-06-21T11:52:45.955-07:00Search And ViewSimple script for searching a file and viewing it in one go:<br /><br /><br /><table border="1" cellpadding="1" width="200"><br />#!/bin/bash<br /><br />usage="$0 <pattern>"<br />if [[ $# -ne 1 ]];then<br />echo $usage<br />exit 1<br />fi<br />files=`find . -name "$1";`<br />if [[ -z $files ]];then<br />echo "$1 not found"<br />fi<br />count=0<br />for f in $files; do<br />((count += 1))<br />done<br />echo "Found $count files matching $1"<br /><br />for f in $files; do<br />echo $f<br />echo -n "Enter [Open : y , quit : q , skip : any other char] "<br />read ans<br />if [[ $ans == "q" ]];then<br /> break;<br />fi<br />if [[ $ans == "y" ]];then<br /> vim $f<br /> break;<br />fi<br />done</pattern><span style="color: rgb(153, 51, 153);"></span><br /><br /></table>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-18998215518740393482010-01-19T07:59:00.000-08:002010-01-19T10:13:10.466-08:00Making cygwin workYou usually try to recreate the environment that you are most comforatable with. So when I got my work laptop, one of the first things I did was to install cygwin. Now cygwin works pretty well for the most part, however its a disaster when converting between windows style and unix style of file paths. Then of course you discover cygpath command which does exactly that.<br /><br /><strong>Classpath for JAVA:</strong><br /><br />However this does not fix you java classpath issues. I struggled with this for a long time till I found this: just make sure that the path separator is ";" and put a double quote around the classpath :<br /><br />cygwin > java -cp ".;./classpathdir/;dir" Test<br /><br /><strong>Using Console for cygwin<a href="http://4.bp.blogspot.com/_kKYBnk2T56E/S1Xk9-49qpI/AAAAAAAAEpY/c6cjnoyVLaE/s1600-h/console.JPG"><img id="BLOGGER_PHOTO_ID_5428496679370074770" style="FLOAT: right; MARGIN: 0px 0px 10px 10px; WIDTH: 320px; CURSOR: hand; HEIGHT: 192px" alt="" src="http://4.bp.blogspot.com/_kKYBnk2T56E/S1Xk9-49qpI/AAAAAAAAEpY/c6cjnoyVLaE/s320/console.JPG" border="0" /></a></strong><br /><br />If you have cygwin on the windows cmd console, then its time to switch to console (Yes its console, though I wish they could have dreamt up a better term just to avoid any confusion.) Its available at <a href="http://sourceforge.net/projects/console/">http://sourceforge.net/projects/console/</a>. It's pretty lets you do multiple tabs , unlimited resizing etc. Best of all you can set cygwin bash as your default shell !!<br /><br /><br /><br /><br /><strong>Use SSH instead of Putty</strong><br /><br />If you have made is so far, why not open ssh instead of putty. Don't get me wrong. I love putty myself, however it doesn't have tabs (well it does but you have to download putty manager for that). You get the advantage of multiple tabs, plus you can script your access now. (<strong>Note</strong>: cygwin will not download openssh by default, you need to download it). If you use password less access to your servers, you can use ssh-agent to cache your keys in memory. I have a function in my .bashrc that I just run to cache the keys:<br /><br /><code><br />alias keyon='ssh-add /cygdrive/c/Documents\ and\ Settings/user/My\ Documents/PrivateKeys/id_rsa'<br />alias keyoff='ssh-add -D'<br />alias keylist='ssh-add -l'<br /><br />function ssh_agent_start<br />{<br />SSHAGENT=/bin/ssh-agent<br />if [[ ! -a $SSHAGENT ]]; then<br />echo "SSHAGENT not found $SSHAGENT"<br />return<br />fi<br />echo Starting SSH-agent<br />SSHAGENTARGS="-s"<br />if [ -n "$SSH_AUTH_SOCK" ]; then<br />echo "Killing existing agent"<br />eval `$SSHAGENT -k`<br />fi<br />eval `$SSHAGENT $SSHAGENTARGS`<br />trap "kill $SSH_AGENT_PID" 0<br />keyon<br />}<br /></code><br />Now you can easliy login to your favourite server without password:<br />> ssh_agent_start<br />> ssh <a href="mailto:user@myserver">user@myserver</a> <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><strong></strong>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-69737222254412984142009-05-18T05:50:00.000-07:002009-05-18T05:58:00.102-07:00Can you sudo ?One of the big improvements that ubuntu has over other distributions is that it disables root account by default. So every time you have to do something as root, you have to run it as sudo. This by no means is an replacement to the man page, but some things that I found useful.<br /><br />1. To change sudo prefs, try visudo. Don't edit sudoers manually, as visudo makes sure that the syntax checks out before you save.<br />2. To change default timeout try:<br /> Defaults:your_user_name timstamp_timeout=7200<br />3. Other than that the file is pretty well self documented.Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-64954883287389621772009-02-23T09:39:00.000-08:002009-02-23T09:56:02.499-08:00I am an avid vim user. I cannot compare it to emacs, as I have rarely used emacs because of its werird keyboard sequence. Anyway its important to carry around your vimenv with you. I am posting below my .vimrc file. In addition, I have had to add the following extensions to make it do my bidding:<div><br /><div>1. <a href="http://www.wana.at/vimshell/">vimshell </a>: Vim has had buffers for a few versions. But it does not support shell access in any of its buffers. Enter vimshell. Its extremely convenient when debugging your code. Thing is you have to patch the vim code and recompile it. But its totally worth it.</div><div>2. matchit.vim plugin: google it. Its lets you to match on html/xml tags as well Super userful</div><div><br /></div><div>-------------------------vimrc----------------------------------------------</div><div>"type :help option to get help on options below</div><div><div>set tags=tags;/</div><div>set nocompatible</div><div>set backspace=2</div><div>set confirm</div><div>set dictionary=/usr/share/dict/words</div><div>set formatoptions=tcq2</div><div>set incsearch</div><div>set listchars=tab:»·,trail:·</div><div>set report=1</div><div>set shortmess=fnrxotTI</div><div>set smarttab</div><div>set textwidth=78</div><div>set title</div><div>set whichwrap=</div><div>set wildmenu</div><div>set tabstop=2</div><div>set shiftwidth=2</div><div>set expandtab</div><div>set paste</div><div>set ruler</div><div>set incsearch " do incremental searching</div><div>set history=50</div><div>set hlsearch</div><div><div>"set viminfo='20,\"50 " read/write a .viminfo file, don't store more</div><div> " than 50 lines of registers</div><div>set viminfo='10,\"100,:20,%,n~/.viminfo</div><div>au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif</div><div><br /></div><div>filetype plugin on</div><div><br /></div><div>syntax on</div><div><br /></div><div>function CodePrefs()</div><div> set cindent</div><div> set cinoptions=(0,u0</div><div> set expandtab</div><div> set makeprg=gmake</div><div> set shiftwidth=4</div><div> set textwidth=0</div><div>endfun</div><div><div>if has("autocmd")</div><div> autocmd FileType c,cpp,html,make,perl,php,php3,php4,inc,sh,dc call CodePrefs()</div><div> autocmd FileType make set noexpandtab nosmarttab</div><div> autocmd FileType mail set textwidth=72 titleold=mutt</div><div> autocmd FileType html set shiftwidth=2</div><div>endif</div><div><br /></div><div>" Transparent editing of gpg encrypted files.</div><div>" By Wouter Hanegraaff <wouter@blub.net></wouter@blub.net></div><div>augroup encrypted</div><div> au!</div><div><br /></div><div> " First make sure nothing is written to ~/.viminfo while editing</div><div> " an encrypted file.</div><div> autocmd BufReadPre,FileReadPre *.gpg set viminfo=</div><div> " We don't want a swap file, as it writes unencrypted data to disk</div><div> autocmd BufReadPre,FileReadPre *.gpg set noswapfile</div><div> " Switch to binary mode to read the encrypted file</div><div> autocmd BufReadPre,FileReadPre *.gpg set bin</div><div> autocmd BufReadPre,FileReadPre *.gpg let ch_save = &ch|set ch=2</div><div> autocmd BufReadPost,FileReadPost *.gpg '[,']!gpg --decrypt -q -a 2>/dev/null</div><div> " Switch to normal mode for editing</div><div> autocmd BufReadPost,FileReadPost *.gpg set nobin</div><div> autocmd BufReadPost,FileReadPost *.gpg let &ch = ch_save|unlet ch_save</div><div> autocmd BufReadPost,FileReadPost *.gpg execute ":doautocmd BufReadPost " . expand("%:r")</div><div><br /></div><div> " Convert all text to encrypted text before writing</div><div> autocmd BufWritePre,FileWritePre *.gpg '[,']!gpg --encrypt --default-recipient "Punit Rathore <first.last@gmail.com>" -q -a 2>/dev/null</first.last@gmail.com></div><div> " Undo the encryption so we are back in the normal text, directly</div><div> " after the file has been written.</div><div> autocmd BufWritePost,FileWritePost *.gpg u</div><div>augroup END</div><div><br /></div></div><div><br /></div></div></div></div>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-887890627108495732009-02-06T05:49:00.000-08:002009-02-09T07:21:53.096-08:00Automate tedious tasks using AutoitOne of my collegues was looking for a script that would let him automate his daily ritual of opening up 4-5 putty sessions to his linux host. I had used <a href="http://www.autoitscript.com/autoit3/index.shtml">autoit</a> before and suggested it to him. I hadn't used it in a while and was curious as to how it worked. After downloading it took me all of 45 minutes to come up with the following script. It asks for your password, then uses it to log on to 5 different putty sessions!!<div>---------------------------------------------------</div><div><div>; this script logs you on to 5 different putty sessions</div><div><br /></div><div>$answer = InputBox ("enter password", "please enter your password","","*",150,80)</div><div><br /></div><div>If $answer = "" Then</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>MsgBox(0, "AutoIt Example", "Enter valid password")</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Exit</div><div>EndIf</div><div><br /></div><div>Opt("WinTitleMatchMode", 2)</div><div><br /></div><div>For $count = 1 To 5</div><div>Run("C:\Program Files\PuTTY\putty.exe -ssh username@hostname")</div><div>WinWaitActive("PuTTY")</div><div>Sleep(1500) </div><div>;MsgBox(0,"Sending password" ,$answer)</div><div>Send($answer)</div><div>Send("{ENTER}")</div><div>Next</div><div>--------------------------------------------------------------</div></div>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-55609253759331573032009-02-05T06:04:00.000-08:002009-02-05T06:12:05.378-08:00Traps in printfI was debugging this segmentation fault that we were seeing at a customer. It wasn't caught because in never hit the code path in dev/QA. It would happen when logging was enabled. GNU debugger seemed to indicate that the logging library was using the vnsprintf call which uses va_list (variable argument list) as input. I spent close to 2 hrs trying to debug this before realizing the error. So lets see if you can try to figure it out. The same problem exists in printf as well:<div><br /></div><div>printf(" some randmon long %l and string %s \n", 4, "hello");</div><div><br /></div><div>compile the above and it will throw segv.</div><div><br /></div><div>Thing is, its easy to assume that %l is a specifier for long. Its not. Its a length specifier not type specifier. You have use %ld. In absense of that printf will assumen that the first arguement ( 4) is a string and throw segv.</div><div><br /></div><div>Lesson: variable arugement is very powerful. but be aware of the traps.</div>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-64066943137230264682009-02-02T08:26:00.000-08:002009-02-02T08:29:31.470-08:00The case of missing core dumpsHave you had situations when your program seg faulted and yet did not create a core file. Well run the following commad :<div>> ulimit -a</div><div>if you see "core file size 0" then there's your answer.</div><div>To enable run the follwoing :</div><div>> ulimit -c unlimited</div><div><br /></div><div>viola.</div><div><br /></div><div><br /></div>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0tag:blogger.com,1999:blog-3268030670301372208.post-69692506591146590052009-01-27T10:20:00.000-08:002009-01-27T11:07:07.125-08:00configuring your shell envHow often did you have to write your .bashrc file all over again because you changed your job. I end up writing/copying it every time and its a bit tired. So here I aim add template bashrc for reuse.<div>-------------------------------------------------------</div><div><div>#!/bin/sh</div><div># .bashrc</div><div><br /></div><div># User specific aliases and functions</div><div><br /></div><div># Source global definitions</div><div>if [ -f /etc/bashrc ]; then</div><div> . /etc/bashrc</div><div>fi</div><div><br /></div><div># use vim style editing of commandline</div><div>set -o vi</div><div><br /></div><div>#make sure cp always creates a backup file</div><div>alias cp='cp --backup'</div><div><br /></div><div># have history igonre rm. save you from accidently doing !r and deleting stuff you</div><div># did not intend to delete</div><div>export HISTIGNORE="rm*:&"<br /></div><div><br /></div><div>export PATH=/usr/local/ant/1.6.5/bin:~/scripts/:$PATH</div><div>export JAVA_HOME=/usr/local/jdk/1.5.0_09/</div><div><br /></div><div>export PATH=/usr/local/jdk/1.5.0_09/bin:$PATH</div><div><br /></div><div>export CVSROOT=:pserver:${LOGNAME}@10.1.1.40:/CVSrep</div><div><br /></div><div># if you are mysql user, this sets your mysql prompt</div><div>export MYSQL_PS1="\\d@\\h>"</div><div><br /></div><div><br /></div><div>unalias vi</div><div>alias vi='~/bin/bin/vim'</div><div><br /></div><div># hack for weird promts when using patch vim for vimshell</div><div>if [[ $TERM == "screen" ]]; then</div><div>PROMPT_COMMAND=''</div><div>fi</div><div><br /></div></div>Punithttp://www.blogger.com/profile/04674567380427318550noreply@blogger.com0