Posts Tagged ‘Code’

A quick-tips Emacs post

May 22, 2009

In this post, I’m basically going to just cut-paste code from my .emacs file, customizations that I feel have been most useful to me:

1) Cscope customizations

;for cscope
(load-file "~/.emacs.d/xcscope.el")
(require 'xcscope)
(setq cscope-do-not-update-database t)
(define-key global-map [(control f3)] 'cscope-set-initial-directory)
(define-key global-map [(control f4)] 'cscope-find-this-file)
(define-key global-map [(control f5)] 'cscope-find-this-symbol)
(define-key global-map [(control f6)] 'cscope-find-global-definition)
(define-key global-map [(control f7)] 'cscope-find-this-text-string)
(define-key global-map [(control f8)] 'cscope-pop-mark)
(define-key global-map [(control f9)] 'cscope-find-functions-calling-this-function)
(define-key global-map [(control f10)] 'cscope-find-called-functions)
(define-key global-map [(control f11)] 'cscope-display-buffer)
;cscope settings end here

An earlier tutorial I wrote for the cscope-newbie.

2) Auto-completion with Smart Tab
(more…)

Advertisements

Linux: utorrent and desktop integration

November 7, 2008
After a bit of work with python I am back to good old bash scripting. Before I begin the actual post a few words about utorrent. This is the only windows based program that I am still to find a substiture for. I know there are lots of good torrent clients there for linux but each one of them has some feature missing ( rtorrent cant queue torrent individually, transmission does not proide advanced options) or problems ( deluge can delete pieces while rechecking) or are resource hogs (azureus because of the java backend) and so I am still using utorrent. As and when rtorrent will get the queing feature implemented I will shift to it. Till then enjoy using utorrent with full desktop integration.
Here I am providing a bash script to provide desktop integration features for utorrent. These include open torrent with utorrent on dbl click, direct open from firefox etc.

Before using the script make sure that all ur drives are recognized by wine and have paths associated with them. It should look something like this picture after you use auto detect through winecfg.


After ensuring this do the following.

$ sudo gedit /usr/bin/utorrent

Paste following in the file and save it.

#!/bin/bash
LANG=en_US.utf-8
LC_MESSAGES=en_US.utf-8
TORRENT_FILE="$1"

path=`winepath "$TORRENT_FILE"`
final=${path:31}
#where 31 is no. of characters in /home/chinmay/.wine/dosdevices/
#change that number accordingly
wine "C:\Program Files\uTorrent\uTorrent.exe" "$final

Change the owner of the file and make it executable.

$sudo chown username /usr/bin/utorrent
$chmod +x /usr/bin/utorrent

Our script is now ready to be used. For it to work with Firefox change the setting in Edit->Preferences->Applications and select this utorrent script for torrent files. Should look something like this when you are done.


Do the same for general desktop integration ie. open with custom application and select the bash script and we are done.

Enjoy using utorrent till rtorrent adds a few more features. Btw, those interested in rtorrent can check Aditya’s and Vedang’s blogs for a guide. Happy downloading. 🙂

Script to restart ADSL modem automatically

November 6, 2008
These days a lot of ISPs are giving free hours or night unlimited packages. However it seems that for the free hour to apply one need to start a new session ( ie restart the router/modem ) after those happy hours begin( a few people have discovered this the hard way 😛 ). For some ISPs this is ok but for people with BSNL connections this can be a big pain considering the 0200 shift time. So, with the help (and motivation) of my friend Aditya, I am presenting here a python code for doing the same. It can be scheduled to be run at the desired time according to your plan.
The basic script should be downloaded from this article at ubuntuforums. The .py file alone will suffice.

Next, we will need to make a few modifications to the code to make sure that it runs with our modem / router.

  • Prerequistes :

For this code to run it necessary that you have the python-pexpect package installed. Ubuntu users click here for the installation.

The code uses telnet to connect to your modem and then send required commands. But to configure the code correctly we will need to make sure that the inputs we provide are the ones expected by the modem interface.

First of all go the modem interface page through your web browser. ( this is usually at 192.168.1.1 or 192.168.1.254). Provide the necessary login detials. ( defualts are usually username == admin and password ==admin ). Once you are on the config page navigate to the services page to make sure that telnet is enable. The page should appear similar to this. Enable the telnet interface for lan.


Now that we know that the telnet interface is enable time to access it to check the settings. Open a terminal and do the following::

$telnet 192.168.1.1 #where 192.168.1.1 is the address that we previously used

This should take u to the telnet interface of your modem. I am pasting my output here. See the input required and the changes made to the final code pasted below ::

$ telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
BCM96338 ADSL Router
Login: admin #Input required here
Password: #Input
Note: If you have problem with Backspace key, please make sure you configure your terminal emulator settings. For instance, from HyperTerminal you would need to use File->Properties->Setting->Back Space key sends.

Main Menu

1. ADSL Link State
2. LAN
3. WAN
4. DNS Server
5. Route Setup
6. NAT
7. Firewall
8. Quality Of Service
9. Management
10. Passwords
11. Reset to Default
12. Save and Reboot
13. Exit
-> 12 #Input

Save current configuration and reboot? Confirm [1-yes,2-no] ==> 1 #Input

Now in the final code pasted next observe that the string passed to p.expect() must match exactly with the ones at the #Input lines. Except the final one. Here you can directly send 1. Final code is pasted next and changes according to my interface made bold. Unnecessary lines are commented.

#!/usr/bin/env python
#
# This script will ping to google.bg and if network is
# unreachable it will connect to the modem and reboot it.

import os
import commands
import pexpect

def adsl_reboot():
p = pexpect.spawn('telnet 192.168.1.1')

p.expect('Login: ')
p.sendline('admin') # Sending Username.

p.expect('Password: ')
p.sendline('admin') # Sending Password.

# print "loggedin",
p.expect('-> ')
p.sendline('12') # Sending command to the shell.

# p.expect('Save current configuration and reboot? Confirm [1-yes,2-no] ==> ')
p.sendline('1')

os.system('zenity --notification --text="The modem has been rebooted."')
# Show an icon in the notification area

result = commands.getoutput("ping -c 1 google.bg")
#if result.find("Unreachable") == -1:
# result = True
# print 'Connected!'
#else:
result = False
print 'Not connected! - Rebooting the modem.'

if result == False:
adsl_reboot()

#
# This was my first script.
# Author: Boris Bolgradov
#
# Big thanks to: walkerk, days_of_ruin and Psykotik!
# From http://ubuntuforums.org


The reboot script works perfectly for my setup. For the scheduling part refer to this article on Aditya’s blog.

DISCLAIMER :: This code has been written at a friends request. The author or rather modifier has no knowledge of python (other than that needed here). For any queries regarding the original script please ask the original author at ubuntuforums. For queries regarding python ask Aditya ( I am sure he will be more than happy to help.) As far as the actual modifications are concerned all questions are welcome.

Enjoy your free net time. 🙂