Tuesday, November 30, 2010

Opening nfs:// URLs in Gnome

By default, Gnome will not handle NFS URLs, but a handler can be registered. First, enable NFS and setup autofs to handle dynamic NFS links (google on how to do this). I am assuming that your auto NFS mounter is setup to use /net/ for NFS.

Next, create a script to convert nfs:// to /net/:
/usr/local/bin/nfs-handler

#!/bin/bash

NFSURL=$1

if [ -z "$NFSURL" ]; then
 echo 'URL required' >&2
 exit 2
fi

NFSURL=`echo $NFSURL | sed 's~^nfs://~/net/~'`

nautilus "$NFSURL"

Now register this with gnome (run from a terminal):

gconftool-2 --set --type=string /desktop/gnome/url-handlers/nfs/command '/usr/local/bin/nfs-handler "%s"'
gconftool-2 --set --type=bool /desktop/gnome/url-handlers/nfs/need-terminal false
gconftool-2 --set --type=bool /desktop/gnome/url-handlers/nfs/enabled true

That is it. Test using:xdg-open "nfs://somecomputer/somemountpoint"


© Copyright 2007 - Andrew Robinson. Please feel free to use in your applications under the LGPL license (http://www.gnu.org/licenses/lgpl.html).

Tuesday, September 15, 2009

Raise a window to the front

I wanted to be able to detect in a python program if it was already running (pygtk) and bring it to the front if so. Thought this code could be useful to others:

#!/usr/bin/env python2.6
import sys, re, subprocess as sp, os
if __name__ == "__main__":
  program_name = os.path.basename(sys.argv[0])
  window_title_starts_with = 'My Application' # Change to the window title of your application
  p = sp.Popen([ '/usr/bin/pgrep', program_name ], stdout = sp.PIPE)
  out = p.communicate()[0]
  if p.returncode == 0:
    p = sp.Popen([ '/usr/bin/wmctrl', '-lp' ], stdout = sp.PIPE)
    out = p.communicate()[0]
    if p.returncode == 0:
      result = [ a[0] for a in 
        [ re.split('\s+', l, 4) for l in re.split('\n', out) ]
        if len(a) == 5 and a[4].startswith(window_title_starts_with) ]
      if len(result):
        print 'Application is already running, bringing to front'
        sp.check_call([ '/usr/bin/wmctrl', '-ia', result[0] ])
        sys.exit(0)

Note that paths are the ones used by Ubuntu. Requires pgrep and wmctrl to be installed.



© Copyright 2009 - Andrew Robinson.
Please feel free to use in your applications under the LGPL license (http://www.gnu.org/licenses/lgpl.html).

Wednesday, April 8, 2009

Links in Thunderbird will not open in existing Firefox

Just recently, Thunderbird was causing Firefox to open my ProfileManager instead of using my existing running firefox instance. So in order to address this, I did some research and found that there are "hidden" environment variables passed that cause Firefox to do this. I hate this behavior, so I found a work-around: Open the Thunderbird configuration editor and set the following:
  • network.protocol-handler.app.http to "/home/username/bin/ff"
  • network.protocol-handler.app.https to "/home/username/bin/ff"
(Replace the username with your username)

Then create the file "/home/username/bin/ff" as:

#!/bin/bash
unset MOZ_NO_REMOTE
unset MOZ_LAUNCHED_CHILD
unset MOZ_PIS_API
unset MOZ_PIS_MOZBINDIR
unset MOZ_PIS_SESSION_PID
unset MOZ_PIS_USER_DIR
/usr/bin/firefox $@

Remember to make it executable

Viola, it now works as it should.


© Copyright 2007 - Andrew Robinson.
Please feel free to use in your applications under the LGPL license.

Monday, February 16, 2009

Create a menu item for aptitude

I love Aptitude, it is the best UI package manager for Ubuntu and Debian functionality wise. Since it is a curses library, it doesn't have a menu item to run it. So for those that also like Aptitude and want to be lazy about creating a menu for it, here you go.

Just save this content to ~/.local/share/applications/aptitude.desktop:

  [Desktop Entry]
  Type=Application
  Encoding=UTF-8
  Name=Aptitude
  GenericName=
  Comment=Aptitude
  Icon=/usr/share/icons/oxygen/64x64/apps/system-software-update.png
  Terminal=false
  Exec=gksu -- /usr/bin/gnome-terminal --title=Aptitude --command=/usr/bin/aptitude --geometry=150x37
  Categories=System

Just tweak the geometry, which terminal you like and the Icon as you see fit.

Monday, July 21, 2008

KDE4, Gnome or XFce -- My Experience

There is constantly a battle of users of which linux desktop is best. Frankly, there is no answer to the question, but to here why one user picked one over another often can make someone else's decision easier. For this reason, I am sharing my thoughts and opinions on my experiences with KDE4, Gnome and Xfce as it relates to usage on my Dell laptop with an Intel video card and using multiple monitors at work.

What I Look For

My goals are important to share, at least partially, to put into scope of why one desktop may appeal to me over others. Here are some of the things that I look for:

  • Graphic appeal across many applications
  • Mutiple application support, GTK as well as QT applications
  • Customizability
    • Theming
      • Icons
      • Window managing
    • Panels
      • Panel applets
  • Support for Compiz

I prefer to use applications that are the best for the job. So for example, I use firefox with no desire to ever use Konqueror for web browsing even if I were to run KDE. This results in my use of Gnome, KDE, generic GTK and console applications as I desire.

KDE4

My first experience on Linux was with KDE3, but I have not used it for many years. For that matter, I do preach that my opinion may be a fair overview of it. I will share my thoughts on it though as there are things that some people may appreciate to hear. Just today, I tried out KDE4 and was very disappointed.

First, the new Plasma functionality in KDE4 does not support multiple monitors. I loaded the new configuration and the Apply button stayed grayed out. The old KDE3 multiple monitor support uses xorg.conf, with the Intel 965 driver does not seem to work with. When I used XRandr 1.2 to enable my monitors, and position one above the other, one turned bright gray. No matter what I tried, KDE4 refused to enable both monitors. This made my decision right here, but there were other things about KDE4 that I did not like.

The customizability of the panel was not very friendly. The clock applet for example, had a huge font with no obvious setting to change the size. When I changed the panel size to tiny, the clock text extended below the screen. The KDE start icon also did not correctly resize and also extended below the screen. I also find the selection of available panel applets to be minimal.

KDE4 also seems to be not ready for prime time. For example, Konsole's 4 version is missing functionality than was in version 3. Also, in the short time I tried KDE4 I had several crash dialogs come up, one of which being KWin.

GTK applications are not supported in KDE. Firefox, Skype and other GTK applications looked horrid and out of place. Since KDE is based on Qt, there are no GTK theme configurators.

Gnome

I have tried Gnome a few times and like several things about it, but I chose not to use it. KDE and GTK applications run just fine in Gnome and the desktop environment is well integrated. But there are a few things that prevent me from being happy using it.

The configuration in Gnome is nice for UI, but its flexibility is limited. For example, the themes are very easy to install for example, but the ability to customize a theme isn't there. I find the gnome icon themes to be reminiscent of OS2/Warp, and I do not mean that as a compliment. KDE may be "cartoony", but Gnome is too close to old operating systems to my liking. Some may prefer that though.

What really disappoints me about Gnome, with at least Ubuntu's version of it, is the lack of functionality with the panel. It has no ability to stretch applets for example to take up space. So, if I place the window list and the tray applet in the same panel, I cannot have it so the tray takes only the room it needs for all of its icons and no more and the rest goes to the window list. This is also a problem with moving applets. If I add an applet between two, like the dictionary applet, and then remove it, the space is not reclaimed. As a result, I had to individually move each applet back to where I had it. I was also disappointed in the lack of numbers of available applets and the lack of ability to customize the applets. I also noticed that the panels were having problems repainting. With two monitors, I moved a panel from one to the other, and the area that the panel used to occupy would not repaint. The background of the tray icons also had issues, especially the Skype tray icon for some reason.

Even though these may seem to be a small issues, it is enough for me to not use it.

XFce

I have been using XFce for a few years as my primary desktop after I finally made the conversion from Windows to Linux. I have used it with Slackware, Debian and now Xubuntu. What I love about XFce is that it is very customizable. It is nice that it uses less resources than Gnome or KDE, but that is not the reason I like it.

XFce's panel is the best by far of the three. There are several applets to choose from, especially with the XFce goodies project. The applets have more functionality and custom settings than those of Gnome or KDE as well. My largest problems with an XFce applet is the volume applet which crashes very often on session startup, and panel restart (the end result is that it removes itself from the panel). The bug has gone unfixed for over a year now. XFce also can embed Gnome applets in the panel, so in the unlikely event that you find one in Gnome that isn't in XFce, you can use it.

XFce allows you to configure your GTK theme, the GTK icon theme and mouse cursor theme very easily. KDE and gnome applications run just fine with XFce. My major problems with XFce are:

  • you have to use the Gnome control center and KControl to customize KDE and Gnome applications (not that I expect XFce to do this, but this is a consideration for users)
  • you have to manually create .desktop files to change the system menu as there is no GUI configuration (you can modify the XFce menu, just not the system menu)
  • To create application launcher applet instances, there are no icon pickers, and you cannot select an application from the menu, you have to hand configure each launcher instance.
  • The XFce developers on the mailing list are extremely defensive and not very friendly
  • The XFce developers seem to have fewer developers than the other desktops and therefore may make its growth slower.

Despite these problems, the ability to customize XFce and the quality of its panel make it my preferred desktop. There is no Compiz support, but running "compiz –replace" works fine.

If you are not a power user, I would not recommend XFce as it seems to require more manual configuration. It seems to be the most stable and be the most customizable though.

Summary

Well, this may not be a very thorough review, and many people will not have my same goals and share my opinions, but hopefully this blog will help some users make a decision on which desktops to try.


© Copyright 2007 - Andrew Robinson.
Please feel free to use in your applications under the LGPL license (http://www.gnu.org/licenses/lgpl.html).

Thursday, November 22, 2007

HOWTO: Configuring A Linux Laptop For Roaming

I have a laptop for work and wanted a way to be able to get things to "just work" when moving between home, work and supporting roaming in places like hotels.

Goals:

  • Support an HTTP proxy for external access at work
  • No proxy away from work
  • Must be able to switch without restarting X
  • Support going into and out of hibernation
  • Automated approach with minimal manual intervention when relocating
  • Be able to have different custom settings for DHCP based on location
  • Support static IP addresses by location

Contents:

The solution is for ubuntu (I am using Xubuntu), but will work fine with debian. Other distributions may need some tweaking. I have broken this into three steps for each library:

  1. guessnet for location detection
  2. switchconf for location configuration differentiation
  3. squid for proxy support

guessnet

The first step is to be able to change our network settings by location. Being on a laptop, I have 2 Ethernet cards: eth0 which is wired and eth1 which is my wireless. I use WPA, WEP and open wireless connections, and wpa_supplicant allows me to use all of these.

Unlike other solutions, guessnet works with the ifupdown debian architecture. Other programs on ubuntu and debian expect this configuration. For this reason, I saw guessnet as the only viable solution. Other programs that I considered were:

  • laptop-net
  • laptop-netconf
  • whereami
  • divine
  • intuitively

Overview

We will be setting up /etc/network/interfaces to use the mapping configuration. Instead of just configuring eth0 and eth1, we will setup virtual interfaces for each location. I will be using "straight" guessnet for the wired interfaces and guessnet used with wpa_supplicant for the wireless interfaces.

Prerequisites

sudo aptitude install guessnet wpasupplicant wpagui ifplugd

Configuration

Setup wpa_supplicant

wpa_supplicant is easier to work with and have roaming supported (for wireless in restaurants, hotels, etc.) with a separate configuration file. You can have wpa information in the interfaces file, but I will show using the wpa_supplicant.conf file instead.

/etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=/var/run/wpa_supplicant

# Example, WEP encryption at work
network={
  id_str="wpa_work"
  ssid="atwork"
  key_mgmt=NONE
  wep_key1=1234567890
  wep_tx_keyidx=1
  auth_alg=OPEN
}

# Example WPA encryption at home
# Use wpa_passphrase to generate the psk
network={
  id_str="wpa_home"
  ssid="athome"
  psk=3655222ca4387e672b9c0c9cb0eb325f536ed6d39d16c31c3e9228221bea9995
}

# Enable roaming here, just uncomment the lines below
#network={
#  key_mgmt=NONE
#}

The one thing that is not obvious here is the "id_str". This identifier will be used as the interface name to match in /etc/network/interfaces.

/etc/network/interfaces

Now we will setup the base configuration. We will be changing this file later when we add switchconf. My goal with this step is to just get guessnet and wpa_supplicant working.

auto lo eth0
iface lo inet loopback

allow-hotplug eth1

mapping eth0
  script guessnet-ifupdown
  map default: none
  #map timeout: 10
  #map verbose: true
  #map debug: true

iface eth1 inet manual
  wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
  wpa-roam-default-iface default-wparoam
  wpa-mapping-script guessnet-ifupdown
  wpa-map default: none

iface eth0-home inet dhcp
  test peer address 192.168.0.1 mac 01:02:03:04:05:06 source 192.168.0.2

iface eth0-work inet dhcp
  test peer address 10.1.1.1 mac 01:02:03:04:05:06 source 10.1.1.2

iface wpa_work inet dhcp
  test wireless essid atwork

iface wpa_home inet dhcp
  test wireless essid athome

iface none inet dhcp

iface default-wparoam inet dhcp

Okay, I don't want to write a book, so I will highlight details here only. Use the man pages for interfaces, wpa_supplicant and guessnet to get more information. The auto line specifies which interfaces to bring up automatically. I set the eth1 to hotplug. On my Dell, Latitude D630, I can turn the wireless radio on and off using a hard switch. The ifplugd will allow the eth1 to come up automatically when I turn the switch on. This should also configure my eth0 when I plug in the cord.

The mapping eth0 block sets up guessnet in ifupdown mode as the mapping script. guessnet will examine all the interfaces in the file, attempting to find a matching interface. See the guessnet man page for good documentation. In this example file, I have two virtual interfaces for eth0, named eth0-home and eth0-work (the names are not important, I just named them this for clarity). "none" is the default, so if the home and work fail, "none" will be used. The wireless will fail since I have a wireless test on those.

eth0-home and eth0-work each have a peer test. I found that the best candidate is the default gateway of the network as it is more likely to respond to a ping. I also have a MAC address specified so that if I go to another location that has a computer with the same IP address, I can avoid a collision. To get a MAC address for a server use arc:

$arp -i eth0 -a 192.168.0.1
computer.domain (192.168.0.1) at 01:02:03:04:05:06 [ether] on eth0

The wpa works pretty much in the same way except that the interface is determined by the wpa_supplicant.conf. The wireless test is not necessary, I only used it to have it not be used for eth0. As mentioned earlier, the iface name should be the same as the id_str from wpa_supplicant.conf.

Testing

To test to make sure it is working run this (from the guessnet FAQ):

cat /dev/null | guessnet --debug -i eth0

switchconf

There is nothing I need to say about this library as it is very simplistic. The examples for it show switching the /etc/network/interfaces file, but with guessnet, this is not needed or desired. I use it mostly for changing my proxy configuration, ntp servers and some details with my dhclient.conf. Feel free to use it for whatever you want. In its default configuration it simply maps files under a location to the standard locations. For example to configure my ntpdate at work I create /etc/switchconf/work/etc/default/ntpdate. When I call "switchconf work" /etc/default/ntpdate becomes a softlink to this file.

Put any scripts in /etc/switchconf/after.d to restart any daemons (for example, I restart squid)

Hooking it up

We can use the "pre-up" and "post-up" commands in the interfaces file to integrate guessnet and switchconf

auto lo eth0
iface lo inet loopback

allow-hotplug eth1

mapping eth0
  script guessnet-ifupdown
  map default: none
  #map timeout: 10
  #map verbose: true
  #map debug: true

iface eth1 inet manual
  wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
  wpa-roam-default-iface default-wparoam
  wpa-mapping-script guessnet-ifupdown
  wpa-map default: none

iface eth0-home inet dhcp
  test peer address 192.168.0.1 mac 01:02:03:04:05:06 source 192.168.0.2
  pre-up /usr/sbin/switchconf home || true

iface eth0-work inet dhcp
  test peer address 10.1.1.1 mac 01:02:03:04:05:06 source 10.1.1.2
  pre-up /usr/sbin/switchconf work || true

iface wpa_work inet dhcp
  test wireless essid atwork
  pre-up /usr/sbin/switchconf work || true

iface wpa_home inet dhcp
  test wireless essid athome
  pre-up /usr/sbin/switchconf home || true

iface none inet dhcp
  pre-up /usr/sbin/switchconf other || true

iface default-wparoam inet dhcp
  pre-up /usr/sbin/switchconf other || true

That should be pretty self-explanatory. I use pre-up instead of post-up because I switch the dhclient.conf in my configuration that contains DHCP information. You can add as many pre- and post-up commands per interface as desired. The " || true" ensures that ifupdown doesn't fail if the switchconf fails (like if squid failed to re-start for example).

squid

Overview

My work has a proxy, I don't use one anywhere else. Linux uses environment variables for proxy support and cannot be changed from in an existing X session, without restarting X. So I decided to run a local proxy that I always use, and based on my location, either use an upstream proxy (at work) or a direct connection.

I first tried tinyproxy as it is much easier to configure than squid, but I started having issues with it communicating to an upstream proxy, so I switched to squid.

Prerequisites

sudo aptitude install squid

Configuration

Since I now have switchconf, I can write two different configuration files, one for work and one for home/other.

Work example:

# Port of the server
http_port 3128

#ACLs
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl WORK dstdomain .workdomain1.com
acl WORK dstdomain .workdomain2.com
acl purge method PURGE
acl CONNECT method CONNECT

# Caches
cache_peer work-proxy parent 80 0 no-query default name=atwork

#Specify caching rules:
# acl QUERY urlpath_regex cgi-bin \?
# cache deny QUERY
cache deny all

# Apache mod_gzip and mod_deflate known to be broken so don't trust
# Apache to signal ETag correctly on such responses
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache

# log
access_log /var/log/squid/access.log squid
# access_log none

hosts_file /etc/hosts

http_access allow localhost
http_access deny all

#Special direction rules.  These state to only have the local server direct request
#to the oracle network.  Everything else should be handled by the parent.  In
#non-firewall cases, all request should be directed.
always_direct allow WORK
never_direct allow all

This shows using a direct connection for work domains and using the proxy for everything else. Home/other (comments removed see above for them):

http_port 3128
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl purge method PURGE
acl CONNECT method CONNECT
cache deny all
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
access_log /var/log/squid/access.log squid
hosts_file /etc/hosts
http_access allow localhost
http_access deny all
always_direct allow all

This just has the proxy make every connection direct, basically turning it off.

Setup bash:

Add this to your ~/.bashrc to have your command-line apps use the local proxy:

export http_proxy="http://localhost:3128"
for VAR in proxy \
  HTTP_PROXY \
  https_proxy \
  HTTPS_PROXY \
  ftp_proxy; do
  export $VAR=$http_proxy
done

Don't forget to configure applications that don't use the environment. This may include skype, pidgin, firefox, thunderbird and your gnome or KDE global configuration.

Summary

Now you have a laptop that automatically detects its location, reconfigures its network and other programs and does not need to reconfigure proxy settings without ever having to run programs or scripts when you change location. By ensuring your hibernate scripts restart networking, your computer will adapt to its new location when it wakes up.


© Copyright 2006 - Andrew Robinson. Please feel free to use in your applications under the LGPL license (http://www.gnu.org/licenses/lgpl.html).