Labels

Arduino (1) Brainf**k (1) Bugs (3) C (1) HTML (1) Install (3) Linux (27) Maths (1) Python (12) Raspberry Pi (4)

Thursday, 31 December 2015

How to setup wifi on the Raspberry pi

Setting up wifi on the Raspberry pi on the command line requires a few things.
  1. The ssid i.e. TP-Link
  2. The router password

To get the ssid run :
iwlist wlan0 scan

Then find the ssid out of the data it gives you.
Now we need to login to the Pi.
Once your logged in open up the file /etc/network/interfaces, you will need to be root.
Find the line that says
allow-hotplug wlan0

Add the following lines below it
iface wlan0 inet dhcp
        wpa-ssid "YOURSSID"
        wpa-psk "YOURPASSWORD"

Now reboot your Pi and you should be connected to the wifi.

Wednesday, 23 December 2015

How to get the ASCII values of a string in python

ASCII values are the codes that correspond to printable characters. In total there are 255 but only 127 of them are commonly used.You can look at the ASCII table here
You can also run man ascii to get the ASCII table.
Note: All of the code needs to be run in the python3 console
To get a value of a character in python run:
ord('a')


This should give you the result of 97.
To get the character of a ASCII value in python run:
chr(97)


This should give you the result of 'a'

These built in functions only take 1 argument so if we want to get the ASCII representation of a string then a for loop needs to be implemented.
This would look something like this:
word = input("Please enter a word: ")
for char in word:
        print(ord(char))
 
If we run this with the word hello then we should get the result:
104
101
108
108
111

To store these values in a array this would be implemented by adding the following code.
word = input("Please enter a word: ")
char_array = []
for char in word:
        char_array.append(ord(char))
print(char_array)

If we run this code with the word hello we should get the result:
[104, 101, 108, 108, 111]


Thursday, 19 November 2015

Bubble sort in 5 lines using Python

The bubble sort is used to put the numbers in order from smallest to biggest. The current method is not the most efficient way of sorting the numbers into order as it compares each number to every other number.The amount of iterations it takes can be calculated by n^2 with n being the number of values in the array.
numbers = [9,1,2,3,45,4332,3232,29,-1]
for j in range(len(numbers)):
    for i in range(len(numbers)-1):
        if(numbers[i] > numbers[i+1]):
            numbers[i],numbers[i+1] = numbers[i+1],numbers[i]


First setup the array with numbers not in any order
numbers = [9,1,2,3,45,4332,3232,29,-1]
 


Now create a for loop that loops depending on the amount numbers - 1 as the array index starts at 0.
for j in range(len(numbers)):


Another for loop nested inside the for loop to do the same thing.
for i in range(len(numbers)-1):


Compare the value of the current number with the number that is next in array.
if(numbers[i] > numbers[i+1]):


If this is true then swap them around so that the smaller one is first.
numbers[i],numbers[i+1] = numbers[i+1],numbers[i]



The array before the iteration starts looks like this
[9, 1, 2, 3, 45, 4332, 3232, 29, -1]


After the code runs it is in order like this
[-1, 1, 2, 3, 9, 29, 45, 3232, 4332]

Tuesday, 6 October 2015

Creating a matrix with python and numpy.

To create a matrix in python we use the library numpy.
Install numpy from that link for the operating system you are running.
If you want to create a 50 by 50 matrix use the following code:


import numpy
matrix = numpy.zeros((50,50))


This creates a matrix that is 50 by 50 and full of zeros.
If you want to fill this random 0's or 1's use this for loop


import numpy
import random

matrix = numpy.zeros((50,50))   #Create a 50 by 50 matrix full of zeros
#Fill matrix with random data
for row in range(0,49):   #Loop used for the row
    for column in range(0,49):   #Loop used  for the column
        matrix[int(row),(column)] = random.randint(0,1)    

print(matrix)    #Print it out to check its worked



Monday, 14 September 2015

How to turn off ping replies.

The ping command can be useful to check if a machine is running however this can make your server more vulnerable to hacking.
If you want to turn the ping reply off use this command

sysctl -w net.ipv4.icmp_echo_ignore_all=1

To turn it back on again

sysctl -w net.ipv4.icmp_echo_ignore_all=0

Monday, 31 August 2015

How to chown a directory recursively.

Chown is used to change the user and group ownership of a file.
General use of it tends to be
chown user:group file
But if the directory has multiple directories inside of that it can take time doing it by hand.
We use the -R option which stands for recursive which does every thing inside of the directory.
chown -R user:group directory

Recalling previous linux commands you have entered

Being able to find commands you have run can be helpful if you cant remember what you have just done or need to run the command again.

While your in a terminal you can use the up arrow to view previous command and this is usually ok for the last few commands but if its any more than that it can become tedious to find it.

To find a command used recently run

 history | grep "PART OF COMMAND"

Make sure to put the space before the command as this stops that current command being added to the history

To run the command last run again

!! also the same as !-1

To run the last command starting with something i.e ifconfig

!ifconfig

To run the command second most recent

!-2

Friday, 31 July 2015

How to only allow specific users SSH access

This post shows you to set the users that are allow SSH access.
If you don't have SSH installed then run
On Ubuntu/Debian/Mint
sudo apt-get install openssh-client openssh-server
On RHEL/Fedora/Centos
sudo yum -y install openssh-clients openssh-server

To make things easier login with root using su - as most of the commands need root privileges.
Or you can just put sudo in front of all of these commands.
Firstly make a backup of the file we will be working with in case you want to return to your original setup.
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

The line we are going to add to the file is AllowUsers "Put users here separated by spaces"

So add the line and then after it a comment for future reference something like only allow SSH access with these users

Now we need to restart the sshd service
If you are running Ubuntu/Debian/Mint run
service ssh restart
If you are running RHEL/Fedora/Centos
service sshd restart

If you are currently logged in over ssh this will close the connection and a new one will be created.

Now if you try and login with a user not specified in the sshd config file
ssh "blocked user"@"ip of pc"
You will not be able to
But if you try to login with a user specified in the sshd config file
ssh "specified user"@"ip of pc"
You should be able to login as usual.

Friday, 24 July 2015

How to configure a static IP on linux

Normally your Linux setup will come with come with a DHCP setup which requests a IP address and gets given one. The Linux box has no control over the IP address that its given this is fine if you are just browsing the web but if you want to ssh into the box or set it up as a web server it can get quite annoying if the IP just changes. A static IP address means that it will always be the same which makes it easy to access.

First we need to find your gateway address this is the address your box goes through to get to the Internet.

route -n
You should get something that looks like this:
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      
0.0.0.0         192.168.0.1     0.0.0.0         UG    303   
192.168.0.0     0.0.0.0         255.255.255.0   U     303    

My gateway address is 192.168.0.1

Now we need to open /etc/network/interfaces but before we do that make a backup as from previous experiences if you mess this up its hard to fix.
sudo cp /etc/network/interfaces /etc/network/interfaces.backup
Now open the file with a editor of your choice.
sudo vi /etc/network/interfaces
And edit the line that looks like
iface eth0 inet dhcp
to
iface eth0 inet static
Now add these three lines underneath that
address 192.168.0.100 #This is the static ip
netmask 255.255.255.0 #This is the netmask which is a 24 bit netmask.
gateway 192.168.0.1 #This is the gateway address we found earlier


Edit: Better way instead of rebooting
ifconfig eth0 down;ifconfig eth0 up

Now we need to restart for the effects to take place.
sudo reboot

If it all works you may want to remove the backup file to save space.

Tuesday, 21 July 2015

How to find out the your WAN address using the command line.

A WAN address is the IP address that the router uses to connect to the internet and it is shared by all of the devices on that LAN network.

We are going to be using tnx.nl/ip to get our IP as this just sends the IP in plain format so its easy to read.

Now lets create a script to get our IP.

Line 1:
This tells the shell to use the sh interpreter.
#!/bin/sh


Line 2:
This gets you IP using wget and tnx.nl and stores it in the variable myIP
myIP=`wget -qO- tnx.nl/ip`


Line 3:
This prints out your IP address.
echo "Current IP address is: $myIP"



Sunday, 19 July 2015

How to make a LED flash with the Raspberry Pi.

To flash a LED we will use the GPIO pins(general purpose input/output) to control the LED. We will control these GPIO pins using python.
The GPIO layout on the Raspberry Pi model B.












We will be using GPIO 17 which is Pin 11.

Installing the library for python.
sudo apt-get install python-dev python-rpi.gpio

Now create a file called blink.py
vi blink.py
Add the following lines

import time
import RPi.GPIO as GPIO

This imports the two libraries, the time library provides the delay for the LED to flash and the RPi.GPIO allows us to control the GPIO pins.

pin = 17

This sets pin to 17


GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)



This sets the pin mode to BCM so we can reference the pins and sets up pin 17 for output

while True:
    GPIO.output(pin, True)
    time.sleep(0.5)
    GPIO.output(pin, False)
    time.sleep(0.5)
This is the Loop where the LED is turned on then turned off.

Now connect the positive leg of your LED to pin 17 and the negative to ground.
When you run the code you should see the LED flash.

Monday, 13 July 2015

How to use the ping command

Ping is a program that is used to test the reachability of a host. It also measures the time it takes to send the packet and receive one.

It does this by sending a ICMP echo request packet and waiting for a reply. Pretty much all Linux flavours will come with Ping pre-installed.


How to ping using a specific interface
ping -I wlan0 google.com
This will ping google using the wlan0 interface

How to send a certain amount of ping requests
ping -c 4 google.com
This will send 4 ICMP echo requests to google

How to change the timeout of a ping
ping -w 5 google.com
This changes the timeout of the ICMP request to 5 seconds

How to ping yourself to check your interface is working
ping 0
or
ping 127.0.0.1
or
ping localhost
They all essentially do the same thing which is ping yourself.

Most ping output should look something like this:

matty@matty-ThinkPad-T420 ~ $ ping google.com
PING google.com (216.58.210.14) 56(84) bytes of data.
64 bytes from lhr08s06-in-f14.1e100.net (216.58.210.14): icmp_req=1 ttl=55
64 bytes from lhr08s06-in-f14.1e100.net (216.58.210.14): icmp_req=2 ttl=55
64 bytes from lhr08s06-in-f14.1e100.net (216.58.210.14): icmp_req=3 ttl=55
64 bytes from lhr08s06-in-f14.1e100.net (216.58.210.14): icmp_req=4 ttl=55
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 26.039/32.973/37.669/4.941 ms




Sunday, 12 July 2015

How to change your MAC address on linux at boot

I already have a post about what a MAC address is and how to find it so if you don't know what these things are look here and have a look.

How to change your MAC address at boot?
We need to edit the /etc/network/interfaces to change the MAC address of the network card before it turns on.
We do this using the pre-up command.
What the pre-up command does is it runs the command before the network card turns on and connects, this means that you will always have that MAC address.

First of all we need to be root then run
vi /etc/network/interfaces Note: Advised to make backup i.e. cp interfaces interfaces.bak
You should get something like this

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp


We need to add
pre-up ifconfig eth0 hw ether 00:00:00:00:00:01
To the end of the file.
Now reboot and check your MAC address and it should be changed.






Saturday, 11 July 2015

How to change mac address on linux temporarily

Whats a MAC address?
A MAC address is a physical way of addressing your network card at a hardware level.

How to find out your MAC address?
Open up a terminal and run
ifconfig wlan0 | grep HWaddr 
Where I have wlan0 you would put your networking device.
My output:
wlan0     Link encap:Ethernet  HWaddr 8c:a9:82:6c:9b:da
 
As you can see my MAC address is 8c:a9:82:6c:9b:da.

How to change your MAC address temporarily?
Open up a terminal as root and run
ifconfig wlan0 down
ifconfig wlan0 hw ether 00:00:00:00:00:01
ifconfig wlan0 up

This will have changed your MAC address to 00:00:00:00:00:01.
To show this run ifconfig wlan0 | grep HWaddr
This change will not survive a reboot.

Tuesday, 7 July 2015

How to change the hostname on ubuntu server.

Your hostname is a label that is assigned to your computer so that it can be identified.

There are only a couple of steps:

Step 1:
Edit /etc/hosts
sudo vi /etc/hosts
You dont have to use vi you can open it with any text editor
Here you should see your current hostname, change it to the one you want.

Step 2:
Edit /etc/hostname
sudo vi /etc/hostname
Here you should see not the hostname you just entered into /etc/hosts but the old one.
Change it to your new one.

Step 3:
Restart the hostname service
sudo service hostname restart
or
sudo /etc/init.d/hostname restart

Saturday, 4 July 2015

How to burn an ISO to CD or DVD using Wodim

Wodim is a command line tool for burning ISO files to disks.

Step 1:
Locate you CD/DVD writer.
wodim --devices
matty@matty-ThinkPad-T420 ~ $ wodim --devices 
wodim: Overview of accessible drives (1 found) :
-------------------------------------------------------------------------
 0  dev='/dev/sg1' rwrw-- : 'MATSHITA' 'DVD-RAM UJ8A0A'
-------------------------------------------------------------------------

As you can see here my devices is /dev/sg1.

Step 2: 
If your CD/DVD is blank then skip this step but if its not then erase the disk.
wodim dev=/dev/sg1 blank=fast

Step 3:
Burn your ISO image to the CD/DVD.

wodim -v dev=/dev/sg1 speed=10 -eject "path-to-iso"

This could take a while so be patient.

Friday, 3 July 2015

Installing VMware Player on debian/ubuntu.

VMware is a free piece of software that allows Virtualization of a guest operating system inside of an operating system. This can be useful when developing or trying out new distros.

To install VMware player we first need to download it.
Lin


You then need to make it executable by running:
sudo chmod +x VMware-Player-7.1.2-2780323.x86_64.bundle

Now run the executable:
sudo ./VMware-Player-7.1.2-2780323.x86_64.bundle

Now follow the installer steps then you will be good to go.

If you get a error about gcc then you need to install dependencies:

apt-get install build-essential linux-headers-`uname -r`

Thursday, 2 July 2015

Basic script to ping all hosts online.

This shell script is a script that pings all the hosts between 192.168.0.1-254. It returns if they are online or not.

#!/bin/sh
for i in `seq 1 254`
do
  ping -c 1 -t 1 192.168.0.$i | grep "64 bytes" &
  sleep 0.1
done

This takes 26 seconds.

If you run the code like this it is significantly slower as the ping command runs in the foreground. To background it you need to add a & at the end of it.

#!/bin/sh
for i in `seq 1 254`
do
  ping -c 1 -t 1 192.168.0.$i | grep "64 bytes" amp;
  sleep 0.1
done
 

Monday, 29 June 2015

How to monitor your CPU on debian or ubuntu systems.

There are many reasons to want to monitor your CPU maybe if you are over-clocking(making sure your CPU hasn't caught fire after running Prime95),or you might just be interested to look at your CPU temps a, there is lots of different reasons and this is a post to show you how.


For both of these programs lm-sensors is required and can be installed with a
apt-get.

sudo apt-get install lm-sensors

If you get a run sudo apt-get update which will update the package lists.
You then need to run sudo sensors-detect and follow the instructions this command is finding all the monitoring devices that are available on your hardware.
If you follow the standard instructions you should be fine

Installing xsensors:
Install with a simple apt-get.
sudo apt-get install xsensors
Wait for it to install then run 
xsensors 
This should open up a program looking something like this:

 This shows the temperature of the hardware in your computer. It doesn't however have very much detail but you may like it if you are just looking for a simple program.








 If you want something with more detail I would recommend psensors. I will show you how to install it now.

Installing Psensor: 
Install with a simple apt-get.
sudo apt-get install psensor
Wait for it to install then run
psensor
This should open up a program looking something like this:
 I Prefer psensors as it has more information about the hardware.
It also has a nice graph which compares temperatures.

Average temperatures:
CPU idle temp about 40C
CPU normal temp 50C
CPU stress temp 65-75C(some may run hotter especially Haswell chips in the 80C-90C)
If your CPU is running hot you might want to check that the cooler is working properly i.e fan isn't clogged with dust and heatsink is attached properly.
You also could re apply your thermal paste.



Saturday, 20 June 2015

How to install Raspian on the raspberry pi.

There are lots of different ways of installing Raspian to a sd card like win32diskimager and usb image writer but a even easier way is with dd.

Things I am going to show you:
  1. How to locate your sd card in the command line.
  2. Where to download the image from.
  3. How to install the image to the sd card using dd.
1. Locating your sd card in the command line.

To do this first open up a terminal and type:
sudo fdisk -l
You should get something like that, now locate what your sd card is named. Mine here is called "/dev/mmcblk0"
yours might be something different but make sure to pick the right one as if you choose the wrong one you could corrupt your hard drive.
Try to look for the size of the sd card.
Take note of the name of your device.


2. Downloading the image.
To download the image, goto https://www.raspberrypi.org/downloads/ and find the most recent raspian image.
Download and extract the image file.






3. Installing
To install the image to the sd card first we need to unmount the sdcard.
sudo umount /dev/mmcblk0
You should use the name of your device.(Make sure you have the correct device or your hard drive could be damaged)
Then we need to be in the same folder as the extracted image.
Now to write the image to the sd card type
sudo dd bs=4M if=path/to/image of=your/device
After running this command(it may take a while depending on your sd card) you should be ready to go.

How to use apt-get on debian or ubuntu systems.

The advance package tool also known as APT is a tool that is used for the easy installation, removal and upgrading of software on Debian/Ubuntu systems.


Things that I will show you how to do:
  1. Search for packages using apt-get
  2. Update system package lists
  3. Upgrade software packages
  4. Install and remove specific software packages
  5. Check for broken dependencies

1. Search for packets that you would like to install using apt-get.
This command can be used to search for packets and find the specific name so that it can be installed. It also gives a short description on what each piece of software is.
apt-cache search
apt-cache search google-chrome  






2. Update system package lists.
This command downloads the package lists from the repositories to get info on the new packages available. 
sudo apt-get update

Note:
The "sudo" command is needed as you need to be root to update the packages as protected files are accessed.











3. Upgrade software files.
This searches for any updated software that is currently installed on your computer.
sudo apt-get upgrade 

Note:
"sudo apt-get update"
This command should be run before running this command to get a fresh package list.


4.  Install and remove specific packages.

Install:
sudo apt-get install <package name>












Remove:
sudo apt-get remove <package name>








This removes just the package.


Purge remove:
sudo apt-get purge <package name>
 






This removes the config files and the package.