About jontas

I like to make things that people find useful.

MySQL importing a sql file

To simply import a sql file (and get any results printed to the prompt)

mysql -uUSERNAME -p DBNAME < FILENAME

The parameters are
-u username (note no space between the u and USERNAME)
-p password (can be given as the username (without a space between p and PASSWORD; or if omitted mysql will ask for it)
DBNAME is the name of the database to import to (if the sql file don’t create a database on it’s own)
FILENAME is the name of the file to import

Other parameters can be
-h server host (dns name or ip) if -h is omitted then localhost is assumed.
-P port number
-f force (will not break on errors)

To import a sql file and then have to output to a file (for instance when running from a crontab)

mysql -uUSERNAME -p DBNAME < FILENAME > OUTFILE

Works just as the previous import except that any output will be written to OUTFILE.

PHP5: Quick on timing a script

Sometimes it is nice to know how long a scrip took to execute (or a part of a script).
This is a simple way to time a script in php.

$time_start = microtime(true);
 
usleep(500);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
echo "run time was " . $time . " seconds";

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).

Magento: New url on a existing installation (after a move)

Magento stores the base url in the database.
The easy way to change this is to use the admin interfece, but if that is not possible (for instance you have swapped ip for a dev machine – or moved data from a existing installation to another machine) then it is possible to do the update in the database.

  • In the table “core_config_data” find the keys “web/secure/url” and “web/secure/url” and just update these to the new address. Hint: The addresses should end with “/” otherwise magento won’t like it.
  • Clean out var/cache/* and var/session/* (use “rm -rf” on a *nix environment)

.htaccess to allow google ads (the indexing bot) [or limit user-agents via .htaccess]

Sometimes there is a need to only allow limit access to a site using a .htaccess file.
If you still wish to be able to serve Google adsense on this page then the following example is good for this (yes the allow line should be changed to suit your needs)

# Google_ads agents
BrowserMatchNoCase Mediapartners-Google good_pass
BrowserMatchNoCase Adsbot-Google good_pass
<LIMIT GET POST>
order deny,allow
deny from all
allow from 123.456.789
#Google ads
allow from env=good_pass
</LIMIT>

PHP: casting stdClass Object to a array

When using Soapclient to access a webservice the returned object will be of a stdClass Object.

If you don’t wish to access this as an object, but would find it easier to use it as an array then a quick cast solves this.

/*As an object*/
print_r($soapResult->contents);
/*cast to an array*/
$soapResult = (array)$soapResult;
print_r($soapResult['contents']);