Sunday, January 06, 2019

VirtualBox: Creating a Centos VM...

Create the VM from the DVD ISO, including GNOME.

Make sure on Settings -> System -> Pointing Device is set to USB Tablet.

Then...

'visudo' and add the following: 'paul ALL=(ALL) NOPASSWD: ALL'

sudo yum install VBoxAdditions, gcc kernel-devel dkms perl, make
cd /run/media/****/VBOXADDITIONS*
sudo ./VBoxLiuxAdditions

sudo yum install java java-1.8-openjdk-devel git

in /etc/sysconfig/network-scripts/ifcfg-enp0s3 (or the appropriate network adaptor) set 'ONBOOT=yes'

mkdir ~/git
cd ~/git
git clone -u 'sh gup.sh' paulp@*****:d:/Users/****/Documents/git-server/miscellany.git

ln -sf ~/git/miscellany/vimrc ~/.vimrc
ln -sf ~/git/miscellany/bash_aliases ~/.bash_aliases
ln -sf ~/git/miscellany/gitconfig ~/.gitconfig

Set Terminal Custom Font to DejaVu Sans Mono Book (10 or 9)

Wednesday, December 19, 2018

Instagram Download Links

Hmmm..... this no longer appears to work. Needs reworking, and check here.

To view a full size Instagram picture, append

media/?size=l
, e.g.

https://www.instagram.com/p/B3hoo93BLA1UtIa4QJvMu1iyBPvX1PGbeH5XfI0/

should be updated to:

https://www.instagram.com/p/B3hoo93BLA1UtIa4QJvMu1iyBPvX1PGbeH5XfI0/media/?size=l

A simple Tampermonkey script to add a link to an Instagram page to allow the image to be downloaded.

// ==UserScript==
// @name Instagram Download Link
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add a link to download photos....
// @author Me
// @match https://www.instagram.com/*
// @grant none
// @require https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==

var currentPage;
var domChangeTimer = '';
var listenerInstalled = false;

$(window).bind('load', MainAction);

function MainAction() {

  // add a listener to the page so this function is run each time the page changes...
  if (!listenerInstalled) {
    document.addEventListener("DOMSubtreeModified", HandleDomChange, false);
    listenerInstalled = true;
  }

  // check the current URL. This will change as AJAX changes the page...
  if (currentPage != window.location.href) {
    currentPage = window.location.href;

    // is the link to the image available...?
    var link = $('meta[property="og:image"]').attr('content');

    // if not, and the page is just of a single image
    // e.g. https://www.instagram.com/p/BuRxiC5Hr4i/
    // then reload the page so the link is available...
    // this is clumsy, but can't find another way to get at the DOM as updated by AJAX
    if (window.location.href.match(/\/p\//) && link == undefined) {
      window.location.reload();
    } else if (!window.location.href.match(/\/p\//) && link != undefined) {

      // and if we've just mmoved off a single image page
      // reload again to get rid of the image metadata from our copy of the DOM
      // that has since been updated by AJAX
      window.location.reload();
    }

    console.log("Download link : " + link);

    // if the link is available, add it to the page...
    if (link != undefined) {
      $("article").append("Download Link");
    }
  }
}

function HandleDomChange(zEvent) {

  // why do we do this here...?
  if (typeof domChangeTimer == "number") {
    clearTimeout(domChangeTimer);
    domChangeTimer = '';
  }

  // DOM has changed, so set a timer...
  // Note that this may be updated numerous times, but the function
  // will only be called when the timer hasn't been set for x ms
  domChangeTimer = setTimeout (function() {
    MainAction();
  }, 500); // in ms
}

Wednesday, November 28, 2018

Counting Files

A quick bash script to count the number of files in a set of directories:

DIRS=`ls -d */`
for D in $DIRS;
do
  printf "$D "
  find $D -type f|wc -l
done

Tuesday, November 27, 2018

MySQL Scheduling

Managing Events

Switch on the event scheduler in the database

SET GLOBAL event_scheduler=on;

To set it on startup, it is necessary to update "/etc/mysql/my.cnf" to add the following:

[mysqld]
event_scheduler = on

Create event to periodically update the database

USE temperatures;
CREATE DEFINER = 'root'@'localhost' event
  IF NOT EXISTS snapshot
ON SCHEDULE EVERY 15 minute STARTS '2018-11-27 00:00:00'
DO
  INSERT INTO temperatures.minmax
    SELECT curdate(), round(min(outside_temp), 1) as min, round(max(outside_temp), 1) AS max
      FROM temperatures.readings WHERE datetime > curdate()
    ON DUPLICATE KEY UPDATE
      min = min, max = max;
Show and delete events

SHOW EVENTS;
SHOW CREATE EVENT snapshot;
DROP EVENT snapshot;
SHOW PROCESSLIST;

Note that code blocks may need the delimiter to change to allow the client to accept nested statements:

DELIMITER //

BEGIN
  SELECT * FROM mytable;
END //

To show all events:

SHOW EVENTS;

To show existing event scripts for a given event

SHOW CREATE EVENT snapshot;

And to alter event scripts

ALTER EVENT snapshot
DO
[new code];

Wednesday, October 24, 2018

Basic MySQL

To get rid of the annoying Ctrl+c issue use mysql -u root -p --sigint-ignore

Managing Users

SELECT user, host FROM mysql.user;
CREATE USER 'user'@'server' IDENTIFIED BY 'mypassword';

SHOW GRANTS FOR 'user'@'server';
GRANT INSERT, SELECT, UPDATE ON stuff.* TO 'user'@'server';
REVOKE INSERT ON stuff.* from 'user'@'server';

DROP USER 'user'@'server';

Managing Databases

CREATE DATABASE stuff;
SHOW DATABASES;
USE stuff;
DROP DATABASE stuff;

Managing Tables

CREATE TABLE music (
  id INT unsigned NOT NULL AUTO_INCREMENT,
  artist VARCHAR(50) NOT NULL,
  title VARCHAR(50) NOT NULL,
  PRIMARY KEY (id)
);
SHOW TABLES;
DESCRIBE music;

ALTER TABLE music ADD release_date DATETIME AFTER title;
ALTER TABLE music CHANGE COLUMN release_date rel_d DATE NOT NULL;
ALTER TABLE music DROP COLUMN rel_d, RENAME TO media;

DROP TABLE music;

Managing Records

INSERT INTO music (artist, title) VALUES
  ('Prefab Sprout', 'Steve McQueen'),
  ('Elbow', 'Asleep at the Back');

SELECT COUNT(*) AS total FROM music WHERE artist LIKE 'Elb%';
SELECT * FROM music WHERE release_date IS NULL ORDER BY artist;
SELECT * FROM stuff WHERE datetime > curdate() - INTERVAL 1 DAY;
SELECT * FROM temperatures.readings
  WHERE pressure=(SELECT min(pressure) FROM temperatures.readings);

UPDATE music SET title='Cast of Thousands' WHERE title='Asleep at the Back';
DELETE FROM music WHERE artist='Elbow';

Tuesday, July 03, 2018

systemd

Time to move on from using the init daemon to manage starting and stopping processes and services.

systemd is a system management daemon that replaces init.d and is available on most distributions.

Fedora documentation can be found here.

To create a new service, create a new configuration file /etc/systemd/system/foo.service (note that absolute path names are required, even for interpreters etc.):

[Unit]
Description=My service
Requires=network.target mysql.service

[Service]
Type=simple
ExecStart=/usr/bin/java -Dapp.properties=/home/pi/clock.properties -jar /home/pi/clock-1.1.jar

[Install]
WantedBy=multi-user.target

To start:
sudo systemctl start foo

To enable on startup:
sudo systemctl enable foo

To check if enabled
sudo systemctl is-enabled foo

Tuesday, December 26, 2017

Installing a Fedora Guest VM


  • Download Fedora LXDE Spin
  • Create a new VM and point it at the Fedora iso
  • Run the VM and install Fedora to the hard drive
  • Remove the iso from the VM
  • 'visudo' and add the following: 'paul   ALL=(ALL)   NOPASSWD: ALL'
  • 'sudo yum update'
  • Ensure there is an optical disk attached to the VM: create if not
  • Install Guest Additions Virtual Drive: Devices-> Install Guest Additions
  • 'yum install dkms gcc kernel-devel'
  • 'cd /run/media/paul/VBOXADDITIONS*'
  • 'sudo ./VBoxLiuxAdditions'
  • 'sudo usermod -g vboxsf paul'
  • 'sudo mount -t vboxsf Temp ~/share'

Sunday, December 10, 2017

Installing Lego Mindstorms NXT 1.0 on Windows 10

See here for excellent instructions.

  1. From the installation CD search for all msi files and install:
    1. Mindstorms.msi
    2. MinstormsEng.msi
  2. Ensure English resource files are installed in the correct location (could be installed on D:) and move to C:\Program Files (x86)\LEGO Software\ if not
  3. Install driver from here. Note that cab file may have to be manually moved from zip file to temp file during the installation process
  4. Install updated driver from here. (not sure step 3 is also required. However...)
  5. Run...

Sunday, October 08, 2017

Installing OpenVPN

Based on OpenVPN 2.4.0. On a Raspberry Pi...

   sudo apt-get install openvpn openssl easy-rsa
   sudo cp -r /usr/share/easy-rsa /etc/openvpn

Update the file /etc/openvpn/easy-rsa/vars:

   export EASY_RSA="/etc/openvpn/easy-rsa"
   export KEY_SIZE=2048

Create Certificates

These should be built into the /etc/openvpn/easy-rsa/keys directory

   cd /etc/openvpn/easy-rsa
   sudo su
   source vars
   ln -s openssl-1.0.0.cnf openssl.conf
   ./clean-all

   # build CA cert
   ./build-ca OpenVPN

   # build server key files
   ./build-key-server server

   # build client key files
   ./build-key client1
   ./build-key client2

   # build Diffie-Hellman key exchange
   ./build-dh

   # static Pre-Shared Key PSK
   openvpn --genkey --secret ta.key

Create Server Configuration File

A default version can be found in /usr/share/doc/openvpn/examples/sample-config-files. Should be named /etc/openvpn/openvpn.conf. However, this is all that is required:

   port 1194
   proto udp
   dev tun
   ca /etc/openvpn/easy-rsa/keysca.crt
   cert /etc/openvpn/easy-rsa/keysserver.crt
   key /etc/openvpn/easy-rsa/keysserver.key
   dh /etc/openvpn/easy-rsa/keysdh2048.pem
   server 10.8.0.0 255.255.255.0
   ifconfig-pool-persist ipp.txt
   keepalive 10 120
   cipher AES-128-CBC
   tls-auth /etc/openvpn/easy-rsa/keysta.key 0
   comp-lzo
   persist-key
   persist-tun
   status /var/log/openvpn-status.log
   log /var/log/openvpn
   verb 3
   explicit-exit-notify 1

Create Client Configuration File

   This is all that is required:
   client 
   dev tun 
   proto udp 
   remote 1194 
   resolv-retry infinite
   nobind
   persist-key
   persist-tun
   mute-replay-warnings
   ns-cert-type server
   key-direction 1
   cipher AES-128-CBC
   comp-lzo
   verb 1
   mute 20
   
   -----BEGIN CERTIFICATE-----
   ...
   -----END CERTIFICATE-----
   
   
   -----BEGIN CERTIFICATE-----
   ...
   -----END CERTIFICATE-----
   
   
   -----BEGIN PRIVATE KEY-----
   ...
   -----END PRIVATE KEY-----
   
   
   #
   # 2048 bit OpenVPN static key
   #
   -----BEGIN OpenVPN Static key V1-----
   ...
   -----END OpenVPN Static key V1-----
   

Routing all Client Traffic Through VPN

To route all traffic through the VPN, the following is added to /etc/openvpn/openvpn.conf:

push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
push "dhcp-option DNS 8.8.8.8"
user nobody
group nogroup

And the following firewall rule is required to ensure that all traffic initiated from clients will be masqueraded as traffic outgoing from wlan0 (change this to the appropriate interface!):

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o wlan0-j MASQUERADE

Installing iptables-persistent can make the firewall rules persistent:

sudo apt-get install iptables-persistent

They are configured with:

iptables-save >/etc/iptables/rules.v4

or better:

dpkg-reconfigure iptables-persistent

To list the NAT tables:

iptables -t nat -vL

It is also necessary to enable IP forwarding by un-commenting net.ipv4.ip_forwarpv4.ip_forward=1 in /etc/sysctl.conf and running sudo sysctl -p

Sunday, April 03, 2016

VirtualBox access to SD card through Windows Host

The problem is how to set the wireless details for a headless Raspberry Pi, and so the challenge is how to change a file on an SD card formatted as Ext4 for Linux when Windows doesn't recognise it.

As I'm already running a Centos 7 image of Linux in VirtualBox, one solution is to access the whole, raw SD card direct from that VM.


And this post details exactly how to do this, whilst the VirtualBox man pages can be found here.



1. Get the DeviceID for your SD card reader

As administrator, open a command prompt and type:

wmic diskdrive list brief



2. Create an image representing the SD card

As administrator, navigate create a link file to the SD card on the desktop:

cd c:\Program Files\Oracle\VirtualBox

VBoxManage.exe internalcommands createrawvmdk -filename "%USERPROFILE%/Documents/sdcard.vmdk" -rawdisk "\\.\PHYSICALDRIVE1"


3. Connect the VM to the SD card using the link

Open VirtualBox as administrator, and open the Settings for the VM. Go to Storage -> Controller: SATA -> (right click) Add Hard Disk -> Choose Existing Disk and open the file that was created in Documents (note VM must be powered off).
.

4. Access...

Start the VM and mount the card using the GUI. The card should now be accessible in native Ext4 format...