Monday 16 December 2013

Simple python lotto number picker

We had unusually big lotto the other day, so I wanted to play. I decided to write my own lucky number picker. This one picks 5 numbers out of 40 number pool, excludes last drawn number and returns your 5 number row in order.


#!/usr/bin/python
import random   
from random import choice
for k in range(10): # number of row to print
    pick = list(range(1,41))  # size of pool 40 +1 
    numbers = [] 
    i=1 
    for j in range(5): 
        num = random.choice(pick) 
        pick.remove(num) # remove selected number from pool
        numbers.append(num) 
        i=i+1
    print sorted(numbers)   # return sorted row
All ideas about improving this code are welcome.

--cheers
Tuesday 26 November 2013

Vim editor is frozen after CTRL+S

Many programs and clients use CTRL+S to save documents. In VI and VIM it's quite different. Even though I still press CTRL+S by accident every once in awhile.
To “unfreeze” the console simply hit CTRL+Q

Apparently CTRL+S actually does XOFF, which means the terminal will accept key strokes but won’t show the output of anything. Press CTRL+Q to turn flow-control on (XON)

To disable keyboard listening to XOFFs, add this to your .bashrc (man stty for more options)
stty ixany
stty ixoff -ixon
If you need to send CTRL+S and/or CTRL+Q you can just add
stty stop undef
stty start undef
Monday 18 November 2013

Capture images on webcam

I own a raspberry pi and have been thinking about getting the camera module, but still I haven't. One of the things I like to do is time lapse.
You can also do this with your web cam on your computer.

First check if your web cam is detected
$ ls -l /dev/video*
crw-rw----+ 1 root video 81, 0 nov 16 17:08 /dev/video0
Then install following programs
sudo apt-get install fswebcam
sudo apt-get install mencoder
Create your time lapse folder and use editor to create your tilme lapse file, the script.
mkdir timelapse
vim runtimelapse
Paste in the following:
#!/bin/bash
# Timelapse controller for USB webcam
frames=1440 # The numer of images to be taken
pause=10 # Dealy in seconds between images
DIR=~/timelapse
x=1
while [ $x -le $frames ]; do
filename=$(date -u +"%Y%m%d_%H%M-%S").jpg
fswebcam -d /dev/video0 -r 640x480 $DIR/$filename
x=$(( $x + 1 ))
sleep $pause;
done;
To make the script executable, use:
chmod 755 runtimelapse
Then run it using:
./runtimelapse
Now you have some number of images, time to create video
cd timelapse
ls *.jpg > list.txt
mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=640:480 -o timelapse.avi -mf type=jpeg:fps=24 mf://@list.txt
And now you have time lapse video.

Instructions on making timelapse using raspberry pi


Saturday 16 November 2013

Fix Ubuntu privacy settings

Ran into interesting thing few days ago.
If you're an Ubuntu user and you're using the default settings, each time you start typing in Dash (to open an application or search for a file on your computer), your search terms get sent to a variety of third parties, some of which advertise to you.

Open a terminal Ctrl+Alt+T. Paste code, press enter. and enjoy your privacy.


V=`/usr/bin/lsb_release -rs`; if [ $V \< 12.10 ]; then echo "Good news! Your version of Ubuntu doesn't invade your privacy."; else gsettings set com.canonical.Unity.Lenses remote-content-search none; if [ $V \< 13.10 ]; then sudo apt-get remove -y unity-lens-shopping; else gsettings set com.canonical.Unity.Lenses disabled-scopes "['more_suggestions-amazon.scope', 'more_suggestions-u1ms.scope', 'more_suggestions-populartracks.scope', 'music-musicstore.scope', 'more_suggestions-ebay.scope', 'more_suggestions-ubuntushop.scope', 'more_suggestions-skimlinks.scope']"; fi; if ! grep -q productsearch.ubuntu.com /etc/hosts; then echo -e "\n127.0.0.1 productsearch.ubuntu.com" | sudo tee -a /etc/hosts >/dev/null; fi; echo "All done. Enjoy your privacy."; fi
 Source https://fixubuntu.com/

Thursday 14 November 2013

Import csv to MySQL with php

I have small project at work where I need to import data from over 80 excel document and from more than one sheet from each into database. And I also needed to enter data from the web form to the database at the same time to identify each port with equipment and location. After converting sheet from .xls to .cvs I import the file by upload it trough the web form.
Not very hard task but I had some issues that I came a cross.
There is a simple method to import a file to database. From the command line I would do this.
mysql > LOAD DATA INFILE 'filename' INTO TABLE tablename FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' (field1,field2,field3);
Ok, this was easy but. In my case it seams that the file neaded to be in /var/lib/mysql/databasename for this command to work and I did not figure out how to get the file there when I uploaded it with php, guess it's permission issue and i'm not going to change them. Adding LOCAL to the command is the way to import the file from my directory, but it considered an administrative task and it returned error.

In the connection parameters I had to add "false,128" to get it to work.
$connect = mysql_connect("localhost","user","pass",false,128);

mysql_select_db("database",$connect);

 mysql_query("LOAD DATA LOCAL INFILE 'upload/$filename' INTO TABLE database FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' (field1,field2,field3) set field4='value1',field5='value2';") ;
Now it works like a charm.
--cheers

See also how to export MySQL data to csv file 

ssh login takes too long time before prompt



I had this problem where it took way too long time for the prompt to appear when using ssh to remote server. And b.t.w. I am using my public ssh key. The GSSAPI authentication was timing out, and I'm not not relying on it.
To turn it off edit the /etc/ssh/ssh_config file with your favourite editor, I use VIM
sudo vim /etc/ssh/ssh_config
Edit this line and change from yes to no 
GSSAPIAuthentication no

Side by side DIV

This is one of the things I just can't remember and I have to google it every time. But it is so simple. Side by side DIV.

Here is some css to go with the div
#leftcolumn { width: 300px; border: 1px solid #333333; float: left;}
#rightcolumn { width: 300px; border: 1px solid #333333; float: right;}
.clear { clear: both;}

And for the div's

<div id="leftcolumn"><p>some text here</p></div>
<div id="rightcolumn"> <p>more text here</p></div>
<div class"clear"></div>

Yes, quite simple and next time I know where I find this.

Friday 8 November 2013

Install and run a TFTP server


In some cases I need tftp server. The method to install the tftp server is very easy.

First install following packages.
$ sudo apt-get install xinetd tftpd tftp
Create /etc/xinetd.d/tftp and add code
service tftp
{
protocol        = udp
port            = 69
socket_type     = dgram
wait            = yes
user            = nobody
server          = /usr/sbin/in.tftpd
server_args     = /tftpboot
disable         = no
}
Create a folder  /tftpboot and modify privileges
$ sudo mkdir /tftpboot
$ sudo chmod -R 777 /tftpboot
$ sudo chown -R nobody /tftpboot
Restart the xinetd service.
$ sudo /etc/init.d/xinetd restart
Now the tftp server is up and running. For MAC OS X see: Run a TFTP Server on Mac OS X

Note:
My install was on Ubuntu 13.10

Edit:
Oct, 2014 installed on Mint 17


Wednesday 6 November 2013

RewriteRule; Moved Permanently

I was getting high numbers of 404 "file not found" on my server do to discontinued blog's I hosted long a go. To reduce those numbers I used .htaccess to redirect all requests to another page using the 301, "Moved Permanently" code.

Example:
RewriteRule ^blogfolder\/?(.*)$ "http\:\/\/newdomain\.com\/" [R=301,L]
Live example:
http://acme.to/blogfolder/foo.html
Monday 4 November 2013

Using history


I tend to forget commands, especially when they get longer or more complex. That's when "history" gets handy. You might want to search in history,
Example:
$ history | grep shutdown
1011  shutdown -h
1012  sudo shutdown -h now
This will give you all shutdown commands from your history. You can also check command history, without running it.
Example:
$ !yourcommand:p
Or if you need to execute multiple commands from history.
Example:
$ !219 ; !229 ; !221
The last example is how to list your most used commands.

$ history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head



Simple things in live

I love Linux, In this first post here I'm going to show you two simple ticks that you will love if you do not know them already, and if you do know them I guess that you still love them.

No permission running command. For example you like to reboot in 10 minutes

$ shutdown -r 10
shutdown: Need to be root
You might be the one who repeats the command starting with "sudo" for root privileges, that's ok for short commands, but you might even want to use the really short one.
$ sudo !!
This will repeat your latest command as root.

The second trick is for same purpose with in the Vim editor. How often have you opened file for edit and when you try to save you find out tha you have no permission to save.
There is a sulution.
:w !sudo tee %
Quite simple and it works.