bash

I have configured a few VPNs on my Ubuntu notebook. Some of them are OpenVPN, and in fact they are fairly easy to configure.
However, sometimes I receive this error:

I wasn’t able to exactly identify the situation when this happens – it seems to be related to networking, and it happens more often when I log into my desktop with WI-FI connection, OpenVPN daemon starts, then I plug an ethernet cable.

SHORT ANSWER

A quick solution is simply to log out and log in again. It is simple but I don’t like it very much.

Other than that everything regarding VPNs on Ubuntu Desktop seems to be a bit of a mess. In the long answer you can find some more details regarding OpenVPN.

LONG ANSWER

In Internet you can find a lot of guides on how to set up an OpenVPN on Ubuntu Desktop. One simple and effective is Ubuntu’s Official: Help Ubuntu – OpenVPN. Anyway, most of the times everything is reduced to

to install a service, or

to install an extension to the Network Manager GUI.

However, the GUI option has some limitation – for example, you can only connect to one VPN at the time. If you have more than one VPN (and you can handle all routing problems with overlapping networks that can occur), you have to go with the service. To go with the service, you have to put all your OpenVPN configuration files, with extension .conf, in folder /etc/openvpn . Then (if your configuration files are correct, if your connection is ok, and a billion other ifs) if you restart openvpn service, you should see something like this:

That means that your VPNs are up and running.

However, if you have password authentication, for some reason, even if you have your credentials in a file connected to the configuration file, sometimes the service will hang, simply because the VPN restarts and it is waiting in background for a password. So, you have to restart the service.

But the agent of the first error it’s not this service. They are two completely different things. I think that this agent is something GUI related, for I meet this error only on the network manager GUI, and not with the service – which keeps running. However, it could just be that the service it’s still using WI-FI connection.

Unfortunately I have no real explanation for this and no real solution; just a quick workaround. It is a fairly complicated matter, which would require a lot of debug for a very simple task, and I don’t think it is worth the time.

Sed Tips And Tricks

by Michele Brusoni on

This post is just a “Long Answer”. It’s the sequel of this simpler post on a sed problem I had: BigBrus’ Corner – Sed Disable Regex . Here are some tips and curiosity I found.

sed -i ‘s/http://127.0.0.1:8080/webinterface/http://192.168.50.13:9091/my/web/interface/g’ /home/bigbrus_corner/test_sed


Let me explain field per field the above sed command:

  • sed : – well, obviously it’s sed
  •  -i   : means “in-place”, which means “replace in the source file”. Default sed behaviour is to output on stdout – which will probably be on the screen – leaving the file unchanged.
  • the next part is between two single quotes ( ‘  ‘ ) . sed does not need them to run, but it’s a good habit to use, for single quotes tells bash not to interpret what’s between them. In this way you will only have to handle sed syntax and special characters. If you want bash to use substitution, you should probably use double quotes ( ” ” ).
  • s : means substitution; it’s the function you want to execute and it’s absolutely the most common. For more functions, read this: Sed Manual – Other commands .
  • / : just after s there’s the delimiter character. By convention it’s / , for there are many other tools that use the same concept and the same character. sed anyway uses the first character after s as delimiter, so you can really use any character.
  • http://127.0.0.1:8080/webinterface : the text you want to be replaced. It needs to be properly escaped
  • / : another delimiter
  • http://192.168.50.13:9091/my/web/interface : the text to put in place. It needs to be properly escaped
  • / : another delimiter
  • g : it means global. sed will replace all occurrence in the text, not just the first. For more options of the function s, read this: Sed Manual – The “s” Command
  • /home/bigbrus_corner/test_sed : the file you want to edit.

Some other specifications on sed:

  • If you’d like to know, sed uses basic regular expression by default – sed Manual – Reporting Bugs – Regex syntax clashes (the first sentence of this paragraph is quite neat). Regarding which characters you should escape within a text to replace, you should know that it’s a POSIX standard and you can find them, for example, here: Wikipedia – Regular Expression basic
  • Well, I have to admit that I lied to you in the examples. It was to make more evident the difference of choosing the right separator. The fact is, “.” is a special character in POSIX regex. So, I should have escaped it. But, “.” in regex sintax means “match any single character”; incidentally, any dot in IP addresses IS “any single character”, so the string matches correctly anyway. But remember, you won’t be so lucky again! 😉
  • If you would like to use extended regular expression instead of basic ones, you must use -r | –regexp-extended flag.
  • sed with s and without options is “line-oriented”. This means that it will replace only the first occurrence per line
  • What amazed me the most, you can use sed as an interpreter to create scripts. It’s rather complicated, but just the fact you can use it it’s awesome. More informations here: Sed Manual – Example Scripts . If you want to know more on how to use interpreters in scripts, you can read this post of mine: BigBrus’ Corner – SHEBANG! (or #! )
USEFUL LINKS

Sed Disable Regex

by Michele Brusoni on

This started like a “small” post on this matter, but eventually it became a very long one, and finally decided to split the argument in two. This is about the problem I had regarding regex. You can find a “more in depth” post here:  BigBrus’ Corner – Sed Tips and Tricks . sed is a command line tool. Of course, I’m no expert of sed, I use it mostly to replace parts of configuration files in my installation scripts. It’s very useful for its ability to use regex and to replace in the same file part of lines, but in fact it is almost a programming language, with so many options to manipulate strings you won’t believe.

Anyway, the ability to use regex can become a problem when you have strings with lots of special characters. Like, for example, a password or a web address.
SIMPLE ANSWER
Well, the really simple answer is: sed cannot run without regex. There’s no parameter, no switch, nothing that can turn off regular expression matching. There may be other tools (I found something with perl, something with pure bash, a partial solution with grep and a “replace” tool, which should come installed with MySQL or something similar…), but not sed.LONG ANSWER

Anyway, most of the times, the problem isn’t with regular expression but with bash escape character – which is . Incidentally, it’s also the most used delimiter of sed (delimiter = a character chosen to separate the different fields of sed).
For example, if you have to correctly replace “http://127.0.0.1:8080/webinterface” with “http://192.168.50.13:9091/my/web/interface”, your sed command will be something like
sed -i ‘s/http://127.0.0.1:8080/webinterface/http://192.168.50.13:9091/my/web/interface/g’ /home/bigbrus_corner/test_sed
which may be fascinating to someone but in my opinion is madness in a string. By the way, this pattern is called ‘Picket Fence’.
The most beautiful option of sed is that you can set its separator to almost any character. And in the most simple way: using it. sed will simply understand.
So, the command above can become
sed -i ‘s|http://127.0.0.1:9091/webinterface|http://192.168.50.13:9091/my/web/interface|g’ /home/bigbrus_corner/test_sed
or even
sed -i ‘s#http://127.0.0.1:9091/webinterface#http://192.168.50.13:9091/my/web/interface#g’ /home/bigbrus_corner/test_sedor even

sed -i ‘sshttp://127.0.0.1:8080/webinterfaceshttp://192.168.50.13:9091/my/web/interfacesg’ /home/bigbrus_corner/test_sed
(it DOES work this last one! I couldn’t believe!! you can use s as a separator)

You can use really ANY character! You only have to put it in the right place and eventually escape any matching character in the string. This example, using i as separator, could make it clearer:sed -i ‘sihttp://127.0.0.1:8080/webinterfaceihttp://192.168.50.13:9091/my/web/interfaceig’ /home/bigbrus_corner/test_sed

Note that, having used i as a separator, I had to escape every other i in the text to have it work. Anyway, that’s the reason you usually use “strange” characters like @#!/: or similar…

So, as you can see, probably most of the times (with small portions of not too complicated text) you actually don’t need to turn off regex.
USEFUL LINKS

Static IP address on Red Hat RHEL

by Michele Brusoni on

To be more specific I’m referring to RHEL (Red Hat Enterprise Linux) 5.5, the only version I could test.
SHORT ANSWER
You have to edit the proper network file:
vi /etc/sysconfig/network-scripts/ifcfg-ethX
where X is the interface number (usually you’ll have ifcfg-eth0 or ifcfg-eth1).
A working situation should be something like this (replace IP addresses with your own):
DEVICE=eth0
BOOTPROTO=none # in some places I found “static”, but with “none” it works
ONBOOT=yes
HWADDR=00:00:00:00:00:00 # you should already have a MAC address, so leave that one
                                                   # if you don’t, I think you can simply delete this field
NETMASK=255.255.255.0
IPADDR=192.168.0.10
GATEWAY=192.168.0.254
Beware to write your correct IP addresses and, if you’re remotely connected, be sure to have another way to access you system. Errors in this file could shut you out of your server, for they will break your network connection.
After carefully checking you configuration at least 3 times, restart your networking service:
/etc/init.d/network restart
Now you should have a static IP address and a working connection.
LONG ANSWER
This time the “long answer” is just a collection of various tips regarding networking on RHEL.
When I had a problem and I had to search how to configure a static IP address in RHEL, I found a post (that I won’t link) with a configuration file where there was no default gateway (that is, “GATEWAY=” line was missing). I didn’t receive any error after restarting network service, but then I couldn’t connect to any IP address. Mind you, I was connected directly to the machine. If I was connected via, for example, SSH, I would not have been able to access to any service (including SSH) hosted on that machine.
The reason is that, not setting any default gateway, RHEL couldn’t create the right routes, so traffic simply wasn’t leaving the machine.
If you want the exact opposite of this post (which is to use DHCP to assign IP addresses), delete from the above configuration the NETMASK, IPADDR and GATEWAY lines and edit the BOOTPROTO line from none (or static) to dhcp. The new configuration file should look something like this:
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HWADDR=00:00:00:00:00:00 # if you already have this field configured.
then again, restart your network manager:
/etc/init.d/network restart
Last but not least, if you set a static IP address, don’t forget to set one or more static DNS servers! Otherwise ping google.com won’t find any server called google.com and you’ll think the world is about to end!! 😉
vi /etc/resolv.conf
If there isn’t anything already in there, put something like the following, editing the fields according to your needs:
search bigbrus # this should be your ISP; if you’re behind a firewall I think it doesn’t matter
nameserver 8.8.8.8 # put here your favourite DNS IP address
nameserver 8.8.4.4 # these two are Google’s DNS severs
Then save your file, and restart network service:
/etc/init.d/network restart
USEFUL LINKS
RELATED POSTS