Friday, 1 July 2016

Python3:Lesson 3 If statments

If you are new to python you can check out my other lessons to get up to speed.
  1. Lesson 1
  2. Lesson 2
An If statement is used to decision in the program depending on different variables, it allows selection to take place.

First off lets get some input from the user that we can use in the if statement. I will do it in the context of test grades.
score = int(input("Please enter your score out of 100: "))

We now have the user score stored in the variable "score" as an integer.

Now lets look at the operators for a if statement.
  • == Is used for equal to.
  • <= Is used for less than or equal to.
  • >= Is used for greater than or equal to.
  • != Is used for not equal to.
  • and can be used for 2 comparisions.
We now need to create our if/elif statement.
if ( score >= 90 ):
    print("A")
elif ( score >= 80 and score < 90 ):
    print("B")
elif ( score >= 70 and score < 80 ):
    print("C")
elif ( score < 70 ):
    print("Fail")

Now if we run this code with the score as 85 it should print out a "B" to the screen.

Wednesday, 29 June 2016

How to install android studio

Android studio is a development environment for creating android apps.

To install it you first will need to download it from here. After you have downloaded it you will have a file called something like "android-studio-ide-143.2915827-linux.zip". You need to unzip this file to be able to use it.

To do this run:
unzip android-studio-ide-143.2915827-linux.zip

You should now have a directory that looks something like this:
You now need to enter the "bin" folder and run the script  "studio.sh". If you are unsure how to run the script:
./studio.sh
Now complete the setup following the instructions it gives you, once you are done close the program down, we can now start installing the program.
Do this after you have completed all of the other steps as you will have to reconfigure it so it will waste your time otherwise.

Steps to install:
  1. Locate where your android studio bin folder is located.
  2. Add this to your PATH variable.
  3. Rename studio.sh to studio.
Step 1:
First you need to cd to the unzipped android studio folder as shown in the picture earlier on. You then need to cd to the bin folder. You can now rename studio.sh to studio.
mv studio.sh studio


You can now run the pwd command to get the full location of your directory.
pwd






You can now see that the directory is located at "/home/matty/Downloads/android-studio/bin"

We now need to add this path to your PATH variable. We do this so that you can type studio and open up the android studio from the terminal.

export PATH=$PATH:/"YOUR PATH TO THE DIRECTORY"

Note: YOUR PATH TO THE DIRECTORY is what we have just got from the pwd command, this should be added without the quotes for the above command.

You should now be able to type "android" into your terminal and it will open the android studio.


Are you vulnerable to the bash bug?

The Bash bug or shell shock as it is also known, is a bug in the bash shell. It allows code to be remotely executed on your server. This could lead to malicious code being installed to your server and it no longer being your server.(CVE-2014-6271)

How to test if you are vulnerable:
First you will need to open a terminal on your server/PC.
You now need to enter this command:
env x='() { :;}; echo this text should not be seen' bash -c 'echo This text should be seen'
If you get the message "this text should not be seen" then you are vulnerable.


How to patch this vulnerability:
To patch this vulnerability we need to update bash.
This is a simple task that can be solved in a few commands.

Debian systems patch:
Run these commands.
sudo apt-get update
sudo apt-get install bash


Fedora systems patch:
Run these commands.  
sudo yum update -y bash


You have now updated your bash shell and will no longer be vulnerable.

Sunday, 26 June 2016

Python 3: Lesson 2 Basics

This lesson will include:
  • Input
  • Output
  • Storing values in variables
  • A combination of the above.
Variables
Variables are used to hold data within the program. In python you do not need to assign it to a certain data type as this is done automatically.
Example Use:
a = 5                 #This would be defined as an integer
b = "Hello, World"    #This would be defined as a string
c = 5.000             #This would be defined as a float



Input
To allow the users to input data into the program we use the input() function. Most of the time this will be paired up with assigning it to a variable to store the data.
Example Use:

name = input("Please enter your name: ")

The "Please enter your name: " is the prompt that tells the user what the program is asking for.

Output
To allow the program to output data from the program.
Example Use:

print("Hello, World!")
print(name) In this case the contents of the variable "name" will be printed to the screen


Combination of all three
We will now create a program that uses variables input and output.
First off we will get the user to input some data.

name = input("Please enter your name: ")
age = input("How old are you?: ")

We have now got two things from them, their name(stored in the variable "name") and their age(stored in the variable "age").

We can now print this variables out with a print statement.
print("Hello",name,"you are",age, "years old")

You now have a program that will ask the user their name and age and print out the output of both of these in a sentence.







Saturday, 25 June 2016

Python3: Lesson 1 Installing Python

Python is a high level interpreted programming language. It's user friendly code allows anyone to start learning it at home.

Installing Python
Most Linux distributions come with python installed out of the box. Open up a terminal and type python3 to check. If you don't get any errors then you are ready to go.

Installing on Debian systems:
sudo apt-get install python3

Installing from source:
wget --no-check-certificate http://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
tar xvf Python-3.4.3.tgz
cd Python-3.4.3
./configure
make
sudo make install



Now when you type the command python3 It should open up a python terminal.

Sunday, 10 April 2016

Ceaser cipher in Python

A Ceaser cipher is a basic encryption originating from the 1st century, invented by Julius Ceaser. It works by moving each letter a fixed position down or up the alphabet.
Here it is better explained by a diagram:









Now for the code in python 2.7, Firstly we will create a function for encoding so open a new file:
def encode(text)


Create an array to hold the new characters:
new_text = []

Now we need to loop through each character in the input text the user will give us:
for char in text:

ord converts a character to its numbered value in ascii.
In this case we will shift all the letters by 3 forward in the ascii table however you can use whatever number you want as long as it lower than 127:
char = ord(char) + 3

To make sure that we stay in the range of ASCII values we need to implement an if statement
if(char > 127):
    char = char - 127


Now to convert the value back to a character:
char = chr(char)

Then add this character to the array:
new_text.append(char)


We have reached the end of this function and now have to return the array to the program:
return new_array

You should now have your first function, which put together look like this:
def encode(text):
        new_text = []
        for char in text:
                char = ord(char) + 3
                if(char > 127):
                        char = char - 127
                char = chr(char)
                new_text.append(char)
        return new_text

The decode function works the same you just need to the opposite to decode instead of encode. I have made the changes in bold below.
def decode(text):
        new_text = []
        for char in text:
                char = ord(char) - 3
                if(char < 0):
                        char = char + 127
                char = chr(char)
                new_text.append(char)
        return new_text


We now need to allow the user to input some text and decide to encode or decode.
This can be done with a simple while loop and if/elif statment:



choice = 0
while choice != 3:
        choice = raw_input("1:Encode, 2:Decode, 3:Exit ")
        if(choice == "1"):
                user_input = raw_input("Enter a word: ")
                new_text = encode(user_input)
                print ''.join(new_text)
        elif(choice == "2"):
                user_input = raw_input("Enter a word: ")
                new_text = decode(user_input)
                print ''.join(new_text) 
        elif(choice == "3"):
                print "Goodbye"
        else:
                print "Please enter a correct choice"



Now lets see if it works

Checking the encoding works:
Choose 1 for encode then enter "hello, world" it should give the output of "khoor/#zruog"

Checking the decoding works:
Choose 2 for decode then enter "khoor/#zruog" it should give the output of "hello, world"

Sunday, 21 February 2016

How to fix the "Firefox already running" problem on Linux

After creating a script which included closing a firefox tab and hiding the window I realised I could not open firefox and just got a "Firefox is already running" error every time after running this script once.

I tried running the command
sudo killall firefox
As there may have been some hidden windows however this didn't work.

Then I tried reinstalling firefox
sudo apt-get install --reinstall firefox
This didn't seem to work either.

Note this command will remove all personal data from firefox, run at your own risk.

After some research I found this
sudo rm -fr /home/"USERNAME"/.mozilla
This worked and I could open firefox again however I don't believe its the best way of solving this problem.

I'm open to any better ways so post in the comments if you have any.