Paul - The Programmer

simple & stupid

Good Linux tool, Network Manager.

Network Manager is a GUI application powered by dbus for network configuration. Network Manager provides a user-friendly interface to set up the wired and wireless network interface.

Network Manager consists of a background daemon process running as root and a front-end applet application.

Install Network Manager with command,

$ sudo apt-get install network-manager network-manager-gnome

To utilize the Network Manager, for Debian users, the user has to be a member of netdev group. For other distribution users, the similar user privileges are required.

$ sudo usermod -a -G netdev paul

Verify the user group setting with

$ id paul
uid=1000(paul) gid=1000(paul) groups=1000(paul),4(adm),24(cdrom),25(floppy),29(audio),44(video),46(plugdev),109(netdev),110(bluetooth),116(scanner)

By default, Network Manager only handles the interface not declared in /etc/network/interfaces.

Not declared interface means the interface is not described in the interface or the interface is disableed with a #NetworkMangaer# prefix.

For instance,

# The primary network interface
allow-hotplug eth0
#NetworkManager#iface eth0 inet dhcp

To enable Network Manager to handle the interfaces in /etc/network/interfaces, the Network Manager's configuration file /etc/NetworkManager/NetworkManager.conf should be updated as:

[main]
plugins=ifupdown,keyfile

[ifupdown]
managed=true

Then restart network-manager service:

$ sudo /etc/init.d/network-manager restart

 

Remove empty lines by VIM global command

The VIM global command can select the lines that match a certain pattern in the scope of whole file. And the appened commands can operate on the selected lines.

Either the following command can be used to remove the empty lines.

:g/^\(\t\|\s\)*$/d
:v/\S/d

The command ':v' is similar to the command 'grep -v' which select the inverted matched lines.

\S is the meta character for the characters which is not white space or tab.

Alter the word order in lines by VIM regular expression

Somehow, I am required to alter the order of word in lines in a text file.

For example, the input text file is like below.

hello world
hello paul
this  is
for   test

I want this file be updated was below.

world hello
paul hello
is  this
test   for

Thanks to regular expression, I do not need to update the lines one after another.

The vim regular expression is a little different than the Perl's. But most of syntax are similar.

\w  : word charaters, includes both alphabet and number characters.

\s   : space characters.

*   : match zero or more preceding character or meta character like \s, \w.

The patterns can be grouped by enclosing with \( and \). 

& :  the whole matched pattern

\0:   the whole matched pattern

\1:   the matched pattern in the first pair of \( and \)

\2:   the matched pattern in the second pair of \( and \)

So, to accomplish the above task, the substitution regular expression is :

:%s/\(\w*\)\(\s*\)\(\w*\)/\3\2\1/

 

The proper way to access to file in resource by Java

The below code demonstrates how to access to a resource file.

import java.io.*;

public class FileTest{
    public void test() throws IOException {
        String filePath = this.getClass().getResource("/test.txt").getPath();
        System.out.println( filePath );
        System.out.println( file.exists() );
    }

    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

Try to execute:

~/lab/test$ java FileTest 
/home/paul/lab/test/test.txt
true

It seems work perfectly. But this code is acturally not 100% right. When the class file is executed in a directory which name contains space character, the resource file will not be found.

~/lab/my test$ java FileTest 
/home/paul/lab/my%20test/test.txt
false

The space character in the file path was replaced by the '%20'. So, I update the path string before use it to create the File object.

import java.io.*;

public class FileTest{
    public void test() throws IOException {
        String filePath = this.getClass().getResource("/test.txt").getPath();
        System.out.println( filePath.replace("%20", " ") );
        File file = new File(filePath.replace("%20", " "));
        System.out.println( file.exists() );
    }
    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

Try this again:

~/lab/my test$ java FileTest 
/home/paul/lab/my test/test.txt
true

Looks like it's all right this time. Even the space charater in pah is supported now.

But this is still not the best way.

The above code acturally use the URL to access to the file. But the best way is use URI to access to file. Only the URI can directly support both the space and Chinese characters in file path.

import java.io.*;

public class FileTest{
    public void test() throws IOException, URISyntaxException {
        System.out.println( this.getClass().getResource("/test.txt").toURI( ).getPath());
        File file = new File(this.getClass().getResource("/test.txt").toURI());
        System.out.println( file.exists() );
    }
    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

The execution result:

~/lab/my test$ java FileTest
/home/paul/lab/my test/test.txt
true 

It works properly and the code is more simple with the URI.

The above code does not support to access to the files in another Jar file's resouce. But I'd like to talk about this topic in another post.  ;-)

Connect to wireless network with wireless tools

Wireless tools is a package of command line tools for configure wireless network.

With the help of wireless tools, it is no need to install the graphical network configuration tools, such as the wifi-radar.

The wireless tools consists with ifrename, iwconfig, iwgetid and iwlist.

To connect to a wireless network, we only need the help of iwlist.

iwlist can list all the nearby wireless nework access point and their addional information.

$ sudo iwlist ath0 scan 
ath0      Scan completed :
          Cell 01 - Address: 00:25:86:49:16:F4
                    ESSID:"TP-LINK_4916F4"
                    Mode:Master
                    Frequency:2.422 GHz (Channel 3)
                    Quality=21/70  Signal level=-74 dBm  Noise level=-95 dBm
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              12 Mb/s; 24 Mb/s; 36 Mb/s; 9 Mb/s; 18 Mb/s
                              48 Mb/s; 54 Mb/s
                    Extra:bcn_int=100
                    Extra:ath_ie=xxxxxxxxxxxxxx

Now, we get the available access point. To access to the wireless network, we need to configure the network interface.

For Debian, the network interface configuration file is /etc/network/interfaces.

To use the interface ath0 to access the acccess point 'TP-LINK_4916F4', the configuration show like below:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug ath0
iface ath0 inet dhcp
	# wireless-* options are implemented by the wireless-tools package
	wireless-mode managed
	wireless-essid TP-LINK_4916F4
	wireless-key1 s:xxxxxxxx

Connect to the wireless network by restarting the networking init script.

# /etc/init.d/networking restart