Limit the download speed of apt-get

Sometimes it is nice to limit the download speed of apt-get.

The way I do this is to create a file “/etc/apt/apt.conf.d/76download” and enter the following to it

Acquire{Queue-mode "access";http{Dl-Limit "50";};};

This will limit apt-get to at most 50 kb/s for apt-get.
Some other how to’s suggest the use of dl-limit but be adviced, dl-limit is per each connection apt-get does (normally it does 2 at a time); the one used above caps apt-get in total.

SimpleXML tags with – in the name

Sometimes a xml file will have tags with “-“s in the name.
It is not to hard to parse this using simplexml, just use the tagname inside curly brackets like: “{tag-name}”

<?xml version="1.0" encoding="utf-8"?>
<medprod xmlns:npl="urn:schemas-instance" version="129">
  <statuses>
    <mpa_status xmlns:mpa="urn:schemas-mpa" datastatus="valid" procstatus="reviewed"/>
  </statuses>
  <mpa_pharmaceutical-form-group-lx xmlns:mpa="urn:schemas-mpa" v="OSD"/>
</medprod>

Let’s assume we load the above in a SimpleXML object called $simplemedprodXml

$simplemedprodXml= simplexml_load_string($xmlString);
echo (string)$simplemedprodXml['version'];
echo (string)$simplemedprodXml->statuses->mpa_status['datastatus'];
echo (string)$simplemedprodXml->statuses->mpa_status['procstatus'];
echo (string)$simplemedprodXml->{'mpa_pharmaceutical-form-group-lx'}['v'];

OpenSSH and password-less logins

Using SSH you can run commands at remote servers.

By default the remote server will ask for credentials but for instance when writing a script it is not a good idea to store the login credentials in a script.
The solution to this is to create a key pair at the origin machine (where the script is) and then send this key to the remoteserver (where the script needs to login).

  • First create a key pair (only has to be done once for this machine; it can be reused for other machines if you wish to be able to login to several computers)
     jonas@jonas-desktop:~$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/jonas/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /home/jonas/.ssh/id_rsa.
    Your public key has been saved in /home/jonas/.ssh/id_rsa.pub.
    The key fingerprint is:
    11:f9:5a:8f:7d:74:e4:68:3b:4b:22:1c:78:e6:be:2d jonas@jonas-desktop
    The key's randomart image is:
    +--[ RSA 2048]----+
    |        ..       |
    |        ..      .|
    |        .o     + |
    |        ..*   + o|
    |        SB = o o |
    |        . = + =  |
    |         . . + o |
    |          E.  .  |
    |          .o.    |
    +-----------------+

    Passphrase is needed to “unlock” this key before being able to use it. Leaving passphrase blank means that the key can be used without any inputs.

  • Next send the key to the server that you wish to be able to login to.
     jonas@jonas-desktop:~$ ssh-copy-id -i .ssh/id_rsa.pub user@remoteserver
    Warning: Permanently added 'remoteserver,11.222.333.222' (RSA) to the list of known hosts.
    user@remoteserver's password: 
    Now try logging into the machine, with "ssh 'user@remoteserver'", and check in:
     
      .ssh/authorized_keys
     
    to make sure we haven't added extra keys that you weren't expecting.

    -i is the identity file we created with ssh-keygen

  • This is it. Now you are able to login to remoteserver as user without a password.

tar extracting to a target directory

By default untaring will put the contents into the current directory.
Sometimes it is not desirable to change directory to the target (such as in a scrip).

tar -xvvf /source/tarfile.tar -C /target

This will extract(x), very verbosly(vv) the file(f) /source/tarfile.tar to the directory(-C) /target (it will change the output folder to /target (so make sure it exists).