Using variables in sed

March 10th, 2009

In a world of Perl, sed seems to be an archaic method of doing things reserved to the real brave geeks!

But sometimes sed does perfectly fantastic job, like it did for me few minutes ago… with some caveats.

My goal was to replace $i with the value of the variable, determined by the first part of the script:

for i in `seq 1 50`;

the problem was that I made rest of the cycle as follows:

do cat newtest.xml|sed –e ‘s/variable/$i/’ >> multifirewallimport.xml; done

and the result was that through all my file the word variable was replaced with the word $i.

What was wrong? Well… Googling for it I discovered the issue was in usage of single quote instead of double quote.

The correct form is:

for i in `seq 1 50`; do cat newtest.xml|sed -e "s/variable/$i/" >> multifirewallimport.xml; done


Email this post Email this post

Enabling root access to vsftpd

March 6th, 2009

It took me a while to figure out due to misleading information over the vast Internet suggesting to search for non existing /etc/vsftpd.users or something like this.

Thing you have to do is to edit /etc/ftpusers and remove root from there.


Email this post Email this post

How to check if LDAPS is really running on a server?

February 9th, 2009

Sometimes this is necessary since the server “appears” to be running (netstat -an|grep 636 returns port in LISTEN state, but the daemon behind is not operative because (for instance) the certificate has not been installed.

If this is the case, grab an openssl client and issue the following command:

openssl s_client host <address of the target host> –port 636 (this is LDAPS standard port)

If server does have valid certificate you should get answer like:

If it does not, you’ll get something like this:

image


Email this post Email this post

Assigning privileges to a user on MySQL DB

February 3rd, 2009

Quick and dirty commands from commandline:

mysql -u <user> -p<password>
mysql> CREATE DATABASE wordpress;
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO ‘wpuser’@'localhost’ IDENTIFIED BY ‘dbpassword’;
mysql> SET PASSWORD FOR ‘wpuser’@'localhost’ = OLD_PASSWORD(‘dbpassword’);
mysql> FLUSH PRIVILEGES;
mysql> quit;


Email this post Email this post

Scripting elegance: reading filenames containing spaces

February 3rd, 2009

You might need to do something with a list of files contained in a file… and maybe these file names include spaces, therefore using a standard

for i in `cat filename`
do
echo $i
done

does not work since it will consider every word as a single variable.

This second script does the job:

IFS=\$    ==> this sets the line separator as EOL
while read i    ==> this read variables separated by EOL and stores value in $i
do
echo $i    ==> this echoes the value of $i. Replace echo command with whatever you want to do with filenames
done < file-containing-the-list    ==> this ends the while do cycle and takes input from file where filenames are stored


Email this post Email this post

I don’t want that NIC to be probed

January 20th, 2009

Suppose you are on a Linux box with multiple NICs.

It happens that there is one of them that loads with a driver disturbing test/activity you’re doing and you want to exclude it from modprobe.

You should edit file /etc/modprobe/blacklist and add there the driver name used by the card you want to get rid of, as follows:

/etc/modprobe.conf

[snip]
blacklist 8139cp
blacklist 8139too
[snip]

The example above (no, you don’t have to add snips, it is just to show that this is part of a bigger file…) will avoid Linux to probe for Realtek based NICs.


Email this post Email this post

Bluetooth and headless Linux

January 11th, 2009

Ever wondered how to connect your linux box via bluetooth, for instance to allow file transfer with your phone?

I found some cool and working hints on the Net, reporting here below what worked for me.

  1. Connect your USB dongle
  2. Install what you need:
    apt-get install obexftp bluetooth
  3. Then you need to set the system to use a predefined PIN, since you have no console and very probably you would like to digit only from phone side to connect with this box.
    To do this, you need to edit file /etc/bluetooth/hcid.conf to change the following defaults:
    # Security Manager mode
    #   none – Security manager disabled
    #   auto – Use local PIN for incoming connections
    #   user – Always ask user for a PIN
    #
    security auto; <== this is set by default to user

    and
            # Default PIN code for incoming connections
            passkey "58336342"; <== this is set by default to 1234, not enough secure IMHO…

Now, back to cellular connections:

  1. Scan for cellular address:
    hcitool scan
    this will return the address of your device, like:
    magicbox:~# hcitool scan
    Scanning …
            00:17:E4:82:F4:64       RoarinPenguin
  2. Now we need to search the channel for file tranfers
    magicbox:~# sdptool browse 00:17:E4:82:F4:64      
    This will return all details about services provided by the phone:
    magicbox:~# sdptool browse 00:17:E4:82:F4:64
    Browsing 00:17:E4:82:F4:64 …
    Service Name: AVRCP Target
    Service Description: Audio Video Remote Control
    Service Provider: Symbian Software Ltd.
    Service RecHandle: 0×10000
    Service Class ID List:
      "AV Remote Target" (0×110c)
    Protocol Descriptor List:
      "L2CAP" (0×0100)
        PSM: 23
      "AVCTP" (0×0017)
        uint16: 0×100
    Profile Descriptor List:
      "AV Remote" (0×110e)
        Version: 0×0100
  3. Service RecHandle: 0×10001
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 1

    Service Name: Dial-Up Networking
    Service RecHandle: 0×10002
    Service Class ID List:
      "Dialup Networking" (0×1103)
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 2
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "Dialup Networking" (0×1103)
        Version: 0×0100

    Service Name: OBEX Object Push
    Service RecHandle: 0×10003
    Service Class ID List:
      "OBEX Object Push" (0×1105)
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 9
      "OBEX" (0×0008)
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "OBEX Object Push" (0×1105)
        Version: 0×0100

    Service Name: Hands-Free Audio Gateway
    Service RecHandle: 0×10004
    Service Class ID List:
      "Handfree Audio Gateway" (0×111f)
      "Generic Audio" (0×1203)
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 28
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "Handfree Audio Gateway" (0×111f)
        Version: 0×0101

    Service Name: Headset Audio Gateway
    Service RecHandle: 0×10005
    Service Class ID List:
      "Headset Audio Gateway" (0×1112)
      "Generic Audio" (0×1203)
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 29
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "Headset" (0×1108)
        Version: 0×0100

    Service Name: Imaging
    Service RecHandle: 0×10006
    Service Class ID List:
      "Imaging Responder" (0×111b)
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 15
      "OBEX" (0×0008)
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "Imaging" (0×111a)
        Version: 0×0100

    Service Name: SyncMLClient
    Service RecHandle: 0×10007
    Service Class ID List:
      UUID 128: 00000002-0000-1000-8000-0002ee000002
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 10
      "OBEX" (0×0008)
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "" (0×00000002-0000-1000-8000-0002ee000002)
        Version: 0×0100

    Service Name: OBEX File Transfer
    Service RecHandle: 0×10008
    Service Class ID List:
      "OBEX File Transfer" (0×1106)
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 11
      "OBEX" (0×0008)
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "OBEX File Transfer" (0×1106)
        Version: 0×0100

    Service Name: Nokia OBEX PC Suite Services
    Service RecHandle: 0×10009
    Service Class ID List:
      UUID 128: 00005005-0000-1000-8000-0002ee000001
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 12
      "OBEX" (0×0008)
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "" (0×00005005-0000-1000-8000-0002ee000001)
        Version: 0×0100

    Service Name: SyncML DM Client
    Service RecHandle: 0×1000a
    Service Class ID List:
      UUID 128: 00000004-0000-1000-8000-0002ee000002
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 13
      "OBEX" (0×0008)
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "" (0×00000004-0000-1000-8000-0002ee000002)
        Version: 0×0100

    Service Name: Nokia SyncML Server
    Service RecHandle: 0×1000b
    Service Class ID List:
      UUID 128: 00005601-0000-1000-8000-0002ee000001
    Protocol Descriptor List:
      "L2CAP" (0×0100)
      "RFCOMM" (0×0003)
        Channel: 14
      "OBEX" (0×0008)
    Language Base Attr List:
      code_ISO639: 0×454e
      encoding:    0×6a
      base_offset: 0×100
    Profile Descriptor List:
      "" (0×00005601-0000-1000-8000-0002ee000001)
        Version: 0×0100

  4. The interesting channel for us is Obex File Transfer, thus 11. Let’s browse what telefone has to offer:

    obexftp -b  00:17:E4:82:F4:64 -c / -l

    This command will access the cellular of the given address the "/" (root) directory and will list its contents

Now, once could ask how to send files to the mentioned box. Well, so far I have discovered how to “pull” them from the box from the phone using obexftp command as follows:

obexftp -b 00:17:E4:82:F4:64 -c /<path_on_the_phone> -g picture1.jpg

There should be another way to put the box in listening mode through rfcomm command, but that’s food for another article ;) since I have first to read and “digest” articles like this one


Email this post Email this post

Cool simple way to remove dead links from iTunes Library

December 12th, 2008

Found this by chance after looooong dig on the ‘Net, therefore reporting here since it’s as simple as efficient!

1. Make a smart playlist called “All Files” with this rule: “Artist” is not “123456789″ (or any nonsense name that won’t be in your library).

2. Make a static playlist called “All Live Files”.

3. Make a smart playlist called “Missing Files” with these rules: Match all of the following rules, Playlist is “All Files”, Playlist is not “All Live Files”

4. Select all the files from “All Files” and drag them into “All Live Files”. The dead files marked (!) will not copy over.

5. “Missing Files” will contain all of your dead files. Select all and delete. Voila, a nice clean iTunes library.

I have these three playlists in their own folder. Whenever I gather more than a couple dead tracks for whatever reason, I delete all the tracks in “All Live Files” and repeat steps 4 and 5.

NOTABENE: when you select all files in the Missing Files Smart Playlist, Delete option is grayed out. You need to use key combination SHIFT+DELETE in Windows and OPTION+DELETE in Mac to perform deletion.


Email this post Email this post

Replace characters in MySQL

December 10th, 2008

It happens sometimes that due to database migration, you see spurious characters in MySQL tables and you would like to intervene massively (thick for instance to forums) and replace them with proper characters.

Luckily this is easily doable if you manage to have connection to DB with command line interface or web based system allowing you to issue SQL statements.

Command to use is:

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);


Email this post Email this post

My Linux changed eth0 with eth1…

December 10th, 2008

This is happening, for instance, when moving a Virtual Machine from one Server to another or when changing the physical network adapter of a server.

The reason is that NIC’s MAC Address changes, therefore Linux perform a new HW detection and creates new adapter definition, ending in situation where there’s no more eth0 but only eth1… and if /etc/networks was configured to use eth0 it will default to DHCP mode causing issues.

How to solve?

You need to edit file /etc/udev/rules.d/70-persistent-net.rules

In this file you will find one line per each network device detected.

For instance in case of detected eth1 and disappeared eth0 it will probably read something like:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:74:1f:c6", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:72:6f:c8", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

where the second entry reports the currently detected MAC address.

How to solve this?

Delete the first line and edit the second line changing "eth1" in "eth0"… then I guess you need to reboot and eth0 will automagically reappear.


Email this post Email this post