Category Archives: knowledge

copying, zipping selecting files with Groovy

Today I had to select some files from different directories copy in another. Despite it seemed a simple task, it wasn’t. The files were a lot, so I had to pack it.

Basically the structure of the directories was something like:


/A/US/file1
/A/
US/file2
/A/
US/file3
/A/UK/file1
/A/UK/file2
/A/UK/file3
...
/B/US/file1
/B/US/file2
/B/US/file3
/B/UK/file1
/B/UK/file2
/B/UK/file3

and I had to select the N% of the files from each of the second level directories (AA,AB, AC…and so on).

I wrote this short script that select only N% (parameter inside the script) of the files from the directory, pack it (without any structure information), and copy on the destination directory:

import java.util.zip.*

def createZip(destination, fileList) {
    try{
        byte[] buffer = new byte[1024];

        ZipOutputStream out = new ZipOutputStream(new
            FileOutputStream(destination));
        out.setLevel(Deflater.DEFAULT_COMPRESSION);
        fileList.each { selected ->
            FileInputStream in_ = new FileInputStream(selected);
            //the new File(selected).getName() return just the name of the file,
            //in order to remove the directory structure inside.
            out.putNextEntry(new ZipEntry(new File(selected).getName()));

            int len;
            while ((len = in_.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            in_.close();
        }
        out.close();
    }
    catch (IllegalArgumentException iae)
    {
        iae.printStackTrace();
    }
    catch (FileNotFoundException fnfe)
    {
        fnfe.printStackTrace();
    }
    catch (IOException ioe)
    {
        ioe.printStackTrace();
    }

    null
}

/**
  * Script
 **/
def rootDirectory = /C:\inputDirectory/
def outputDirectory = /C:\outputDirectory/

if ((!new File(rootDirectory).list()) || (! new File(outputDirectory).list())){
    println "Error. Input or Output directory are not valid."
    throw new RuntimeException() //TODO: understand how to exit without raising an exception
}

def directories = ['A', 'B', 'C', 'D']
def percentageRules = 5    //5 = 5% of the rules for each country

directories.each{ subDirectory ->
    println "listing ${rootDirectory}\\${subDirectory}"
    def directoryList = new File("${rootDirectory}\\${subDirectory}").list()
    if( directoryList ){
        directoryList.each{ elem ->
            def fileList = new File("${rootDirectory}\\${subDirectory}\\${elem}").list().grep{ !it.contains('out') }
            def numberFiles = (fileList.size() * 5 / 100) as Integer
            if ((numberFiles == 0) && (fileList.size() > 0)) {
                numberFiles = fileList.size()
            }
            println "Zipping ${numberFiles} (${percentageRules}%) of files from ${rootDirectory}\\${subDirectory}\\${elem} into ${outputDirectory}\\${subDirectory}-${elem}.zip"

            if(numberFiles){
                fileList = fileList.collect{ e ->
                    "${rootDirectory}\\${subDirectory}\\${elem}\\${e}"
                }
                createZip("${outputDirectory}\\${subDirectory}-${elem}.zip", fileList[0..<numberFiles])

                /* The old version copied the files instead of zipping it, using AntBuilder,
                   you can use it if you don't need to zip the files. */
                /*
                fileList[0..<numberFiles].each{
                    ( new AntBuilder ( ) ).copy ( file : "${rootDirectory}\\${subDirectory}\\${elem}\\${inputF}" , tofile : "${outputDirectory}\\${inputF}" )
                }
                */
            }
        }
    }
}

I wrote it rushing, and I copied somewhere the first method (which was java), so might be errors or better ways. Anyway any suggestion are welcome.

About these ads

How to OpenVPN over Proxy

Sometimes there are places where is impossible to reach to internet without pass through a proxy. Using proxy is problematic because usually is impossible to read mail or use chat, irc and any application which work on a port different from 80 or 443.

This how to should work on most of the cases, unless the proxy policy is too restrictive.

Basically, the idea is to use the main connections to all the application which support proxy and are simple to configure and a customized route only for services that can’t pass thought a proxy.

Server

Openvpn uses default port 1194 (TCP or UDP), to pass over a proxy you must use the 443 port. I suggest to leave default openvpn port and apply a prerouting rule on iptables which map the 443 port on 1194:

iptables -A PREROUTING -i eth0 -p tcp -m tcp --dport 80 443 -j DNAT --to-destination 192.168.10.127:1194

Let’s start to configure openvpn service.

First of all you must read this official howto section to understand how to generate certificate (there are a lot of scripts and sample configuratino files shipped with openvpn package); you can also modify and use my configuration file.
Here my server configuration file:

mode server
local 192.168.10.127
;port 443
proto tcp
dev tun
ca keys/ca.crt
cert keys/server.crt
key keys/server.key # This file should be kept secret
dh keys/dh2048.pem
server 10.8.0.0 255.255.255.0
push "route 192.168.10.0 255.255.255.0"
keepalive 10 120
tls-auth keys/ta.key 0 # This file is secret
cipher AES-128-CBC # AES
comp-lzo
user nobody
group nobody
persist-key
persist-tun
verb 5
mute 20

I stored my certificates into /etc/openvpn/keys and my openvpn configuration file into /etc/openvpn.
I want to spend just few words about network configuration:

  • 192.168.10.0/24 is my home network (192.168.10.127 is my server network address)
  • 192.168.x.x/x is network I’m connected with client
  • 10.8.0.0/24 is the tunnel network

Client

Here a basic configuration (you can find a well explained file into sample configuration openvpn files):

client
dev tun
proto tcp-client
remote public_ip_address 443 #Public ip address of your home network
resolv-retry infinite
nobind
persist-key
persist-tun
cipher AES-128-CBC
ca "/etc/openvpn/keys/home/ca.crt"
cert "/etc/openvpn/keys/home/client1.crt"
key "/etc/openvpn/keys/home/client1.key"
tls-auth "/etc/openvpn/keys/home/ta.key" 1
comp-lzo
verb 5
http-proxy proxy.ras 80 passwd_file basic
#http-proxy-retry
http-proxy-option AGENT Mozilla/5.0+(Windows;+U;+Windows+NT+5.0;+en-GB;+rv:1.7.6)+Gecko/20050226+Firefox/1.0.1

I will not explain about keys and certificates here because openvpn how to give you a good explanation about it.
If your proxy need authentication, you must put proxy username and proxy password into your passwd_file, respectly on first and second line.

Now, you can start openvpn on server (service start openvpn).
Then you have to start openvpn on client. If you pass through a proxy, services can return you a FAILED, in this case, you should check /var/log/messages to get information about it.

If you got something like:

Initialization Sequence Completed

the tunnel is started. To verify that it work, just try to ping other tunnel part.

Natting and fowarding
Now is necessary to enable NAT and forward on your openvpn server, to allow certain flows, forwarded througt your vpn can reach internet by passing on your home router.

Just apply this few rules:

/bin/echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -A FORWARD -i tun0 -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE

now the server configuration is done.

Now we have to create static routes:

route add -host ip_you_want_to_staticize gw your_vpn_tunnel_address

for example: jabber, you have to retrieve your jabber server ip address, and insert into route command.as “ip_you_want_to_staticize”.

If you don’t have a dns into your subnet, to maintain transparency in applications, is better to use /etc/hosts to map every ip address to his name.

I’m using vpn only for jabber and email, I want to use also mugshot but it doesn’t work…dunno why.

Thanks to Kiwi to help me.

This post is under construction…so If you have suggestion or any issue to propose me, don’t hesitate to tell me.

ICH5 module fixing

Yesterday, I resolved a problem of a soundcard with Linux. That PC had a Realtek Integrated soundcard (ICH5) and the rear output didn’t work, to listen audio, it was possible only using headphone in the front microphone.

Here there is the model, using lspci:

00:1f.5 Multimedia audio controller: Intel Corporation 82801EB/ER (ICH5/ICH5R) AC’97 Audio Controller (rev 02)

The solution is quite easy. Time ago I wrote a similar document about my soundcard. The problem is the hardware implementation of soundcard is based only on a driver (that you can install only with windows) and in Linux no information are provided to use correct soundcard’s settings.
So, as I wrote, is necessary to give when module (in this case snd_8x0) is probed, a options. To see which options, you should install source code or documentation of kernel and you find in Documentation/sound/alsa/ALSA-Documentation.txt file.

After found the correct module, you should edit /etc/modprobe.conf as follow:

options snd-intel8x0 ac97_clock=0 ac97_quirk=inv_eapd

The more important option, in this case, is ac97_quirk, and for my soundcard is set to “inv_eapd”, there are many other options, and, if you are not sure, is better to try all. You can find in the “ac97_quirk” section in ALSA-Documentation.txt file.

After this, (this is a particular settings of my soundcard) I choose 4 channels and shared computation (instead indipendent) and it work with rear output.

I’m sorry if this article is not explain so good. I know but I wrote fast. If you find mistakes, tell me by comments.

perl regexp tips #1

I didn’t know how to set the title of this post, so I choose a generic and useless title :P

I have a text file with 81 characters (one and zero) per rows. I want to add a semicolon (“;”) between every character.

Eg. if the line is:

010101101010

and I want to became:

0;1;0;1;0;1;1;0;1;0;

This is the solution script (thanks dfa)

perl -ne ‘s/([01])/\1;/g; print’ file_input > file_output

a script to clone a directory but with empty files :P

This is my first seriuous bash script. Thanks in advance to people from #bash and #sed, thanks to Jigen, Dani and Arbiter.

I have a directory with some file inside. I want to create another directory with the same file (with same name) but empty (created with touch).

My code take two parameters:

copianomi.sh -i input_dir -o output_dir

Here my code:

#!/bin/bash
# ./copianomi.sh -i dirInput -o dirOutput

NO_ARGS=0
if [ $# -eq "$NO_ARGS" ]  # Script invoked with no command-line args?
then
   echo "Usage: $0 dirInput dirOutput"
exit        # Exit and explain usage, if no argument(s) given.
fi

#
# check parameters
#
while getopts ":o:i:" Option
do
   case $Option in
      i 	) INPUT=$OPTARG;;
      o	) OUTPUT=$OPTARG;;
   esac
done

#
# Copy names
#
for NAME in "$INPUT"/*; do
   touch "$OUTPUT/${NAME##*/}"
done

GmailFS installing and configuration (Fedora 7)

Disclaimer: Don’t trust gmail storage disk. Is possibile to lose all store data or get your account blocked (if you copy too much data, eg 100mb). Use it only to have fun or testing. If you wanna do backup, don’t store sensible data (is Google..remember) or be sure to have another backup.

GmailFS is a virtual filesystem (developed by Richard Jones) that permit you to mount your gmail box as an external hard drive. Is usefull for small backups similar to NFS.
Let’s start to install and configure GmailFS. I suppose here that you have a gmail address (if not, go here).

Packages need to be installed:
- python 2.3
- fuse
- libgmail (you can found here packaged by Ville SkyttĂ)
- fuse python bind (watch my (temporary) repository)
- gmailfs (watch my (temporary) repository)

After downloaded and installed all my packets, if your pc don’t crash, we can go ahead and configure gmailfs.

Add a line about gmailfs to your /etc/fstab as explain in the official project site: usr/local/bin/gmailfs.py /path/of/mount/point gmailfs noauto,user 0 0

If you want to access to your storage gmail disk with root user, you have to modify the configuration file /etc/gmailfs.conf adding username, password and disk name (disk name must be something difficult to guess to avoid someone can mess up your email).

To mount, simply use (as root):
mount /mnt/path/of/mount/point

If something go wrong, you can find log files in ~/gmailfs.log. Remember also if you need to use proxy configuration, you need appropriate ssl packages (eg. pythong-openssl) to use it.

To mount partition as normal user, you need to create a file similar to /etc/gmailfs.conf in your home and call it .gmailf (~/.gmailfs).
After that, you have to modify permission of mount point:
chown root:fuse /mnt/path/of/mount/point
chmod 775 /mnt/path/of/mount/point

Now you need to add your user to fuse group (I used administrator panel of GNOME). After that exit and log-in again, you should be able to mount fuse partition, using:

mount /mnt/path/of/mount/point

For information, correction, error and so on….use comments :-P

Update 01/10/2007@00:29  I have problems to unmount device from normal users. I get this error “unmount: /mnt/backup mount disagrees with the fstab”.

This, because when mtab is written, is different from fstab. I try to modify manually mtab but mtab change when a new partition is added, so is better to use root to unmount.


Simple & Easy NAT between wifi and eth

Sometimes happen the same situation: one ethernet port, one ethernet cable, no hub/switch and many PCs with wireless.
A good solution is to connect a pc with ethernet and create a WLAN (ad hoc or infrastructure) to connect other PCs to internet.
Too many time I had this problem and the solution is always the same or similar, so I wrote this useful and reusable bash script to configure wifi card (chipset Atheros with madwifi driver) and create a NAT to connect AdHoc WLAN.


#!/bin/bash
# inizializing ethernet (I suppose outer network have address 193.205.22.12)
# I suggest to disable NetworkManager and kill dhclient
ifconfig eth0 193.205.22.12

# Now I need to unload and reload module with option "autocreate=adhoc". This
# simplify the creation of virttual interfaces athX (See how madwifi work for more informations)
/sbin/rmmod ath_pci
modprobe ath_pci autocreate=adhoc


# Configuring essid (In this case I use essid "spongepowa"
iwconfig ath0 essid spongepowa
# Configuring WLAN address (I suppose my network is 192.168.1.0/24)
ifconfig ath0 192.168.1.1

# Enabling forwarding
/bin/echo "1" > /proc/sys/net/ipv4/ip_forward

# Inizializing iptables
iptables --flush
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P PREROUTING ACCEPT

# Making rules:
# - 192.168.1.0/24 is local WLAN addresses
# - eth0 is the output interface (ethernet interface), ath0 is the input interface (wifi interface)
iptables -A FORWARD -s 192.168.1.0/24 -d 0/0 -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -d 192.168.1.0/24 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.1.0/24 -j MASQUERADE

I hope this is correct. If you have any question ask me by comment.

If my english is full of mistakes, tell me (AYBABTU is not allowed without motivation :þ)…with my mistakes, of course.