Thursday, July 23, 2015

C Programming


The situation is so much better for programmers today - a cheap used PC, a linux CD, and an internet account, and you have all the tools necessary to work your way to any level of programming skill you want to shoot for."


I've got a cheap computer with Linux on it and "A Book on C" by Al Kelley and Ira Pohl. It's time to shoot high ... 

1. Program Output

Program Output

Previous | Home | Next


Gather

Read Chapter 1, section 'Program Output (p. 6-10)

Reflect 

In file sea.c (omit line numbers, empty lines are only put for clarity of output)
1:  #include <stdio.h>  
2:    
3:  int main(void) {  
4:    
5:    printf("from sea to shining C\n");  
6:    
7:    return 0;  
8:  }  
download code here.

Linux compiling:
 $ gcc -o sea sea.c  

Running program:
 ./sea  

Line 1:
Directive for compiler pre-processor which will include the content of the file stdio.h which is found in the usual place (known to the compiler). This file will allow me to use function printf and others

Line 3:
Every program starts with function main(). Function will return an int (integer) to the OS that started program (Linux here).

Line 5:
Function printf(), which is loaded with line 1 directive, will display on the screen the string (sequence of characters enclosed between quotation marks. The string will end with 'new line' character (\n).

Line 7:
Program before finishing its job, is going to return number 0 (return 0) to the OS (which is code for 'all went well').

Lines 3-8:
Body of function main() is between curly braces {}. It is the code that will be run sequentialy (top-to-bottom).

Create

Introducing some errors in the code to see how the compiler is going to react.

Error 1
1:  #include <stdio.h>  
2:    
3:  int main(void)   
4:    
5:    printf("from sea to shining C\n");  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:5:5: error: expected declaration specifiers before ‘printf’  
    printf("from sea to shining C\n");  
    ^  
 sea.c:7:5: error: expected declaration specifiers before ‘return’  
    return 0;  
    ^  
 sea.c:8:1: error: expected declaration specifiers before ‘}’ token  
  }  
  ^  
 sea.c:8:1: error: expected ‘{’ at end of input  
   

What is the problem?

Error 2
1:  #include <stdio.h>  
2:    
3:  int main(void){   
4:    
5:    printf("from sea to shining C\n")  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:7:5: error: expected ‘;’ before ‘return’  
    return 0;  
    ^  

What is the problem?

Error 3
1:  #include <stdio.h>  
2:    
3:  int main(void){   
4:    
5:    printf("from sea to shining C\n);  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:5:12: warning: missing terminating " character [enabled by default]  
    printf("from sea to shining C\n);  
       ^  
 sea.c:5:5: error: missing terminating " character  
    printf("from sea to shining C\n);  
    ^  
 sea.c:7:5: error: expected expression before ‘return’  
    return 0;  
    ^  
 sea.c:8:1: error: expected ‘;’ before ‘}’ token  
  }  
  ^  

What is the problem?

Test

Write a program that displays the following:

first.c
first.c



Previous | Home | Next

Wednesday, July 22, 2015

CLI Explorations



The list of Raspberry Pi Explorations

Day 1: Installation
Day 2: Making Myself At Home
Day 3: VIM - Powerful Text Editor
Day 4: Linux As a Problem Solving Tool
Day 5: The Linux Command Line Ch1-3 Notes
Day 6: The Linux Command Line Ch4 Notes - Manipulating Files and Directories
Day 7: The Linux Command Line Ch5 Notes - Working With Commands
Day 8: The Linux Command Line Ch6 Notes - Redirection
Day 9: The Linux Command Line Ch7 Notes - Seeing The World As The Shell Sees It
Day 10: The Linux Command Line Ch8 Notes - Advanced Keyboard Tricks
Day 11: The Linux Command Line Ch9 Notes - Permissions
Day 12: The Linux Command Line Ch10 Notes - Processes
Day 13: The Linux Command Line Ch10 Notes -  Processes Continued
Day 14: The Linux Command Line Ch11 Notes - The Environment
Day 15: The Linux Command Line Ch12 Notes - Gentle Introduction To VIM
Day 16: The Linux Command Line Ch13 Notes - Customizing The Prompt
Day 17: The Linux Command Line Ch14 Notes - Package Management
Day 18: Installation of Raspbian Step By Step (instead of ch15)
Day 19: The Linux Command Line Ch16 Notes - (ping, traceroute etc.)
Day 20: The Linux Command Line Ch16 Notes - Continued (FTP, wget, ssh)
Day 21: The Linux Command Line Ch17 Notes - Finding Files and Directories
Day 22: The Linux Command Line Ch18 Notes - Archive and Backup
Day 23: The Linux Command Line Ch19 Notes - Regular Expressions Part 1

Day 23: The Linux Command Line Ch19 Notes

Previous | Home | Terminology | Next

Chapter 19 - Regular Expressions

What are regular expressions?

Let's quote the author of "The Linux Command Line" book William E. Shotts, Jr

"Simply put, regular expressions are symbolic notations used to identify patterns in text. In some ways, they resemble the shell’s wildcard method of matching file and pathnames but on a much grander scale. Regular expressions are supported by many command-line tools and by most programming languages to facilitate the solution of text manipulation problems. However,
to further confuse things, not all regular expressions are the same; they vary slightly from tool to tool and from programming language to language. For our discussion, we will limit ourselves to regular expressions as described in the POSIX standard (which will cover most of the command-line tools), as opposed to many programming languages (most notably Perl ), which use slightly larger and richer sets of notations."

List of Regular Expressions Metacharacters
(can be escaped and treated literally with backslash)


^ $ [ ] { } - ( ) | \

Note:

As we can see, many of the regular-expression metacharacters are also characters that have meaning to the shell when expansion is performed. When we pass regular expressions containing metacharacters on the command line, it is vital that they be enclosed in quotes to prevent the shell from attempting to expand them.

Little explanation:

. matches any single character.

* matches zero or more of characters (including a character specified by a regular expression) that immediately precedes it.

? matches zero or one occurrences of the preceding regular expression.

+ matches one or more occurrences of the preceding regular expression.

^ matches the first character of regular expression, matches the beginning of the line. 

$ matches the last character of regular expression, matches the end of the line.

[] matches any one of the class of characters enclosed between the brackets. A circumflex (^) as first character inside brackets reverses the match to all characters except newline and those listed in the class. A hyphen (-) is used to indicate a range of characters. The close bracket (]) as the first character in class is a member of the class. All other metacharacters lose their meaning when specified as members of a class.

[^bg]zip matches zip NOT preceded by bg.

{n} matches the preceding element if it occurs exactly n times.

{n,m} matches the preceding element if it occurs at least n times, but no more than m times.
{n,} matches the preceding element if it occurs n or more times.

{,m} matches the preceding element if it occurs no more than m times.

- is used to denote a range of characters (e.g. [a-z] a through z.

() matches an expression.

| acts as logical or (alternation).

\ treat following metacharacter as literal and NOT metacharacter.


The lab in next post!


Previous | Home | Terminology | Next

Tuesday, July 21, 2015

Day 22: The Linux Command Line Ch18 Notes


Chapter 18 - Archive and Backup

Commands:

Compressing Programs:

gzip - Compress or expand files.
bzip2 - A block sorting file compressor.

the archiving programs:

tar - Tape-archiving utility.
zip - Package and compress files.

and the file synchronization program:

rsync - Remote file and directory synchronization.


Lab 12

  1. List the /etc directory content and redirect output to foo.txt file. Check the size of the file.
  2. Compress foo.txt file and check its size after compression. Use gzip. Then decompress the file.
  3. Compress the file using bzip2 utility and check the file size.
  4. Decompress the file.
  5. Use foo.txt to create an archive that is going to be compressed at the same time (foo.tar.gz). Use: Verbose option.
  6. You are about to send a file to Window user. How would you compress/decompres it?
  7. There is an external disk mounted in /media/jaro directory. Its name is Seagate Expansion Drive. Crate a 'backup' directory on it and store a directory on it (my case 'Books').

Lab 12 Solutions

List the /etc directory content and redirect output to foo.txt file. Check the size of the file.

pi@raspberrypi ~ $ ls /etc > foo.txt; ls -l foo*
-rw-r--r-- 1 pi pi 1798 Jul 21 09:22 foo.txt
pi@raspberrypi ~ $ 

Compress foo.txt file and check its size after compression. Use gzip. Then decompress the file.

pi@raspberrypi ~ $ gzip foo.txt; ls -l foo*
-rw-r--r-- 1 pi pi 910 Jul 21 09:22 foo.txt.gz

pi@raspberrypi ~ $

pi@raspberrypi ~ $ gunzip foo.txt.gz

Compress the file using bzip2 utility and check the file size.

Decompress the file.

pi@raspberrypi ~ $ bzip2 foo.txt ; ls -l foo*
-rw-r--r-- 1 pi pi 1001 Jul 21 09:22 foo.txt.bz2

pi@raspberrypi ~ $

pi@raspberrypi ~ $ bunzip2 foo.txt.bz2 

pi@raspberrypi ~ $ 

Use foo.txt to create an archive that is going to be compressed at the same time (foo.tar.gz). Use: Verbose option.

pi@raspberrypi ~ $ tar -czvf foo.tar.gz foo.txt; ls -l foo*.gz
foo.txt
-rw-r--r-- 1 pi pi 1025 Jul 21 09:38 foo.tar.gz

pi@raspberrypi ~ $

You are about to send a file to Window user. How would you compress/decompress it?

$ zip file/unzip file
$ zip -r directory/unzip directory

There is an external disk mounted in /media/jaro directory. Its name is Seagate Expansion Drive. Crate a 'backup' directory on it and store a directory on it (my case 'Books').

Notice!
The name of a drive contains spaces. You can use quotes or backslashes to enter that disk.

cd /media/jaro/Seagate\ Expansion\ Drive/
rsync -av Books/ /media/jaro/Seagate\ Expansion\ Drive/Backup



Monday, July 20, 2015

Day 21: The Linux Command Line Ch17 Notes


Chapter 17 - Searching for Files

Commands:

locate  - Find files by name.
find - Search for files in a directory hierarchy.

xargs - Build and execute command lines from standard input.
touch - Create/Change file name.
stat - Display file/file system status

Locate utility finds files based on the database stored at:
/var/lib/mlocate/mlocate.db

This database is updated using: 'updatedb' utility.

Find searches a given directory and its subdirectories in order to find the file name. It also supports type of the file (-type f = file, -type -d = directory). For instance:

$ find ~ -type d | wc -l

will find all directories (-type d) in our home directory (~) and will list their number (| wc -l). Find is very powerful. Check for all options in the "The Linux Command Line" (p. 222-224).



Lab 11

  1. Update database locate uses and find all files with 'passwd' name.
  2. Using 'locate' display how many times 'passwd' was found.
  3. In your home directory find all files with ".avi" extenstion and that are 10MB or larger in size.
  4. Find files whose content or attributes were changed in the last 20 minutes.
  5. Find files whose content were changed in the last 2 or less minutes.
  6. Repeat the step 4 and 5 using only one search (hint: group both queries in two logical expressions with an -or)
  7. Which command can follow 'find' with user defined action?
  8. As a bonus follow the instructions as per 'A Return to the Playground' section in the book. It's awesome!
Update database locate uses and find all files with 'passwd' name.
(raspberry pi in my version did not have locate utility installed)

$ sudo apt-get install locate
$ sudo updatedb

Using 'locate' display how many times 'passwd' was found.

$ locate passwd

In your home directory find all files with ".avi" extenstion and that are 10MB or larger in size.

find ~ -type f -name '*.avi' -size +10M

Find files whose content or attributes were changed in the last 2 or less minutes.

$ find ~ -type f -cmin -2

Find files whose content were changed in the last 2 or less minutes.

find ~ -type f -mmin -2

Repeat the step 4 and 5 using only one search (hint: group both queries in two logical expressions with an -or).

find ~ \( -type f -cmin -2 \) -or \( -type f -mmin -2 \)

Which command can follow 'find' with user defined action?

-exec command '{}' ';'
or 
$  | xargs command

for instance:

$ find ~ -name '*.avi' | xargs --null ls -l
Notice!
--null option allows to interpret embedded spaces in file names to be treated as such rather than as space + another command/option

As a bonus follow the instructions as per 'A Return to the Playground' section in the book. It's awesome!


Create 100 directories as per example:

$ cd playground
$ mkdir dir-{00{1..9},0{10..99},100}

Create 100 files in each directory as per example:

dir-{00{1..9},0{10..99},100}/file-{A..Z}

Confirm that 100 file-A have been created (if you are in 'playground directory type the following):

find . -name 'file-A' | wc -l

Create file 'timestamp' for our reference:

$ cd ~
$ touch playground/timestamp
$ stat playground/timestamp

Change times on the file timestamp

$ touch playground/timestamp
$ stat playground/timestamp

Let's update some of the files:

find playground -type f -name 'file-B' -exec touch '{}' ';'

and find newer than timestamp:

find playground -type f -newer playground/timestamp

Finally, let’s go back to the bad permissions test we performed earlier and apply it to playground:

$ find playground \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)


Friday, July 17, 2015

Day 20: The Linux Command Line Ch16 Notes Cont


Chapter 16 - Networking Continued

Commands:

ftp - Internet file transfer program
wget - Non-interactive network downloader
ssh - OpenSSH SSH client (remote login program)


Lab 8 - FTP
  1. Change directory (you can use your 'playground').
  2. Ensure that you are in the local directory 'playground'
  3. Using CLI, create a connection to any known public ftp server and download any file.
  4. Make sure that you see the download progress.
Lab 8 - Solution


pi@raspberrypi ~ $ ftp ftp.microsoft.com
-bash: ftp: command not found
pi@raspberrypi ~ $ 

Ooops! FTP client is not installed. Let's install it.

pi@raspberrypi ~ $ sudo apt-get install ftp
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  ftp
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 60.6 kB of archives.
After this operation, 140 kB of additional disk space will be used.
Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main ftp armhf 0.17-27 [60.6 kB]
Fetched 60.6 kB in 0s (77.1 kB/s)
Selecting previously unselected package ftp.
(Reading database ... 79596 files and directories currently installed.)
Unpacking ftp (from .../archives/ftp_0.17-27_armhf.deb) ...
Processing triggers for man-db ...
Setting up ftp (0.17-27) ...
update-alternatives: using /usr/bin/netkit-ftp to provide /usr/bin/ftp (ftp) in auto mode
pi@raspberrypi ~ $ 

Now, let's try again! I am going to use microsoft ftp server for this lab (user: ftp, password: anything).

pi@raspberrypi ~ $ ftp ftp.microsoft.com
Connected to ftp.microsoft.com.
220 Microsoft FTP Service
Name (ftp.microsoft.com:pi): ftp
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230-Welcome to FTP.MICROSOFT.COM. Also visit http://www.microsoft.com/downloads.
230 User logged in.
Remote system type is Windows_NT.

Now let's display the content of directory on the ftp server:

ftp> ls
200 PORT command successful.
125 Data connection already open; Transfer starting.
04-28-10  07:21PM       <DIR>          bussys
04-28-10  10:17PM       <DIR>          deskapps
04-28-10  11:14PM       <DIR>          developr
04-28-10  11:15PM       <DIR>          KBHelp
04-28-10  11:15PM       <DIR>          MISC
04-29-10  06:54AM       <DIR>          MISC1
04-29-10  08:47AM       <DIR>          peropsys
04-29-10  05:10PM       <DIR>          Products
04-29-10  05:13PM       <DIR>          PSS
04-29-10  05:22PM       <DIR>          ResKit
04-29-10  07:51PM       <DIR>          Services
04-30-10  08:37AM       <DIR>          Softlib
226 Transfer complete.
ftp> 

Let's snoop around in ... ResKit directory.

ftp> cd ResKit
250 CWD command successful.
ftp> ls
200 PORT command successful.
125 Data connection already open; Transfer starting.
04-29-10  05:21PM       <DIR>          bork
04-29-10  05:21PM       <DIR>          IIS4
04-29-10  05:21PM       <DIR>          mspress
04-29-10  05:21PM       <DIR>          nt4
04-29-10  05:22PM       <DIR>          win2000
04-29-10  05:22PM       <DIR>          win98
04-29-10  05:22PM       <DIR>          y2kfix
226 Transfer complete.
ftp> cd y2kfix
250 CWD command successful.
ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection.
04-29-10  05:22PM       <DIR>          alpha
02-03-00  06:24PM                 5115 readme.txt
04-29-10  05:22PM       <DIR>          x86


Check what's my local directory:

ftp> lcd
Local directory now /home/pi
ftp> 

I need to change my local directory to 'playground'

ftp> lcd playground
Local directory now /home/pi/playground
ftp> 

In order to display download progress I need to use keyword: hash

ftp> hash
Hash mark printing on (1024 bytes/hash mark).

And finally, let's download 'readme.txt' file'

ftp> get readme.txt
local: readme.txt remote: readme.txt
200 PORT command successful.
125 Data connection already open; Transfer starting.
#####
226 Transfer complete.
5115 bytes received in 0.19 secs (26.6 kB/s)
ftp> bye
221 Thank you for using Microsoft products.

The '#####' indicate download progress. With large files you will see a lot of them flying through the screen.



Lab 9 - WGET

  1. Connect to the same server and download the same file using wget.
  2. Use wget to download the file using the following url:

In case you were interested this link has all the chapters:



Lab 9 - Solution




Lab 10 - SSH

Create ssh connection to a computer using CLI.

Lab 10 - Solution


Wednesday, April 22, 2015

Day 19: The Linux Command Line Ch16 Notes


Chapter 16 - Networking

Commands:
ping - Send an ICMP ECHO_REQUEST to network hosts
traceroute - Print the route packets trace to a network host
netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
host - DNS lookup utility
ftp - Internet file transfer program
wget - Non-interactive network downloader
ssh - OpenSSH SSH client (remote login program)

First a little terminology (explanation for 13 year old son):

IP Address: An address that identifies a device in the network.
Just like most people have home addresses, computers and other devices in the network (printers, routers, switches, smart TVs, etc.). In order to send messages between those devices computer-to-computer talk, they must have some form of unique identifiers. They are those four bytes that look like:
192.168.1.1

Host: computer in the network
Computer can have many applications installed on them. The ones that want to communicate accross the network. For instance, your game, your web browser etc. We say that computers are hosts for this applications.

Domain Name: Name that represents IP address
Since applications require IP addresses of the servers (server is a bigger computer that sends to your application what it requested), with which it communicate, it would be next to impossible for you to remember those four-byte thingies. Instead, you use names rather than addresses, but there is a special service on the Internet that translates them to actual IP addresses that your computer can use. For instance, in your web browser you say:
http://www.raspberrypi.org, rather than, http://93.93.130.39. 

In this post I would like to help you get the hang of networking basics.

On the command line type:

$ ping 8.8.8.8

After few seconds press CTRL-c to stop it.

Here's my output of this test:

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=23.8 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=57 time=24.5 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=57 time=24.6 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=57 time=33.3 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=57 time=25.7 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=57 time=25.4 ms
^C
--- 8.8.8.8 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5009ms
rtt min/avg/max/mdev = 23.858/26.278/33.336/3.218 ms

What just happened?
Your computer sent a number of special packets called: ICMP echo-request (will talk more on ICMP some time in the future). Those messages were delivered by so called routers (special devices finding the best path to destinations and forwarding those packets to them), to a computer with IP address 8.8.8.8. That happens to be Google public DNS server (the one that can translate any domain name to actual IP address).

Since the computer 8.8.8.8 responded with ICMP echo-reply message (those line you see above starting with 64 bytes from 8.8.8.8), it shows you that your computer has access to the Internet. Also, you can learn that the average time it took to send this packet and receive reply from 8.8.8.8 host is 26.278 millisecond). Pow Wow! That's fast right? (millisecond is one millionth of a second!).

If you want to send a specified number of packets (say 4 packets only), try to use the following command:

$ ping -c 4 8.8.8.8

Next, very useful tool to check what routers (those magical boxes delivering our packets to the right host in different network than ours), are between us and the final destination. Try this (if you don't have this command available on your RP, do this: $ sudo apt-get update; sudo apt-get install traceroute):

$ traceroute 8.8.8.8

Today, I am in the hotel in town called Tralee, Ireland. This is what I got:

traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  10.16.0.254 (10.16.0.254)  7.131 ms  7.100 ms  7.084 ms
 2  86-45-112-3-dynamic.agg9.chf.chf-qkr.eircom.net (86.45.112.3)  113.396 ms  117.233 ms  117.241 ms
 3  lag-24.pe1.rtd.rsl-rtd.eircom.net (86.43.13.69)  27.189 ms  27.196 ms  27.182 ms
 4  * * *
 5  lag-50.br1.6cr.border.eircom.net (86.47.63.112)  33.020 ms  33.010 ms  32.999 ms
 6  inex.google.com (193.242.111.57)  32.986 ms  22.912 ms  22.873 ms
 7  209.85.250.213 (209.85.250.213)  22.829 ms 66.249.95.91 (66.249.95.91)  22.646 ms 209.85.250.215 (209.85.250.215)  22.578 ms
 8  google-public-dns-a.google.com (8.8.8.8)  24.808 ms  24.779 ms  24.738 ms

The number 4 (asterisks) did not respond to my request, but all other did. It seems like there are 7 routers between my laptop and host 8.8.8.8 which is the last responder. There IP addresses or names are displayed. If you want only IP addresses, disable domain name resolution doing this:

$ traceroute -n 8.8.8.8

Here's my output:

traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  10.16.0.254  5.435 ms  5.381 ms  5.330 ms
 2  86.45.112.3  20.936 ms  23.475 ms  23.442 ms
 3  86.43.13.69  20.793 ms  20.741 ms  23.283 ms
 4  86.43.253.9  26.518 ms  26.531 ms  26.483 ms
 5  * * *
 6  193.242.111.57  26.267 ms  24.507 ms  24.423 ms
 7  209.85.250.213  27.151 ms 66.249.95.135  23.204 ms 209.85.250.213  23.177 ms
 8  8.8.8.8  23.154 ms  23.527 ms  23.479 ms

This time it is the 5th router that did not respond in timely manner. Cool!

One last networking command I'd like you to play with today (more on networking tomorrow), is netstat.

Do this:

$ netstat -r

Here's my output (Hotel in Tralee)

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         10.16.0.254     0.0.0.0         UG        0 0          0 wlan0
10.16.0.0       *               255.255.255.0   U         0 0          0 wlan0

I can find out that my default gateway (local router that will be helping my packets find the destinations) is 10.16.0.254.

If you want to see some statistics of your interface try this:

$ netstat -ie

It displays statistics of all network interfaces of your computer (again my laptop in the hotel shows something like this):

wlan0     Link encap:Ethernet  HWaddr 00:24:d6:75:7e:40  
          inet addr:10.16.0.137  Bcast:10.16.0.255  Mask:255.255.255.0
          inet6 addr: fe80::224:d6ff:fe75:7e40/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:22154 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16542 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 

          RX bytes:17330515 (17.3 MB)  TX bytes:3320213 (3.3 MB)

Received 17.3 MB so far. Transmitted 3.3 MB, no errors etc.

These tools have a way more to offer than I showed you. One thing at a time ;).

Before we call it a day, I am going to add one more tool so you can find out if your domain name service is working. This service is the most important service we have on the Internet. Remember that you use domain names in your applications rather than IP addresses? Who would remember all those weird numbers. It is so much easier to remember: minecraft.net rather than its current IP address.

Check this out:

$ host minecraft.net

Here's my output:

minecraft.net has address 54.243.72.162

BEAUTIFUL!

Mess around with those commands. Try to see man pages of the ones we used today. Some of them have a huge number of options, so don't try to remember them. Instead, read as much as you can to have an idea of their capabilities. You can always come back to those specifics.

More on networking in the next post. Meanwhile have fun with our today's tools.

What do you think this command do?

$ host minecraft.net 8.8.8.8

It also resolves name-to-ip but instead of using your local DNS server, it will ask Google public DNS server for help to resolve the name!



PS. Some workers cut the phone line to my house I'm told. Next post in few days when Eircom fixes it. So far they did not say when ..., that's just great. No Internet for God knows how many days! 

Tuesday, April 21, 2015

Day 18: Installation of Raspbian Step By Step (instead of ch15)


Chapter 15 - Storage Media


DON'T READ CHAPTER 15 (13 year old using RP doesn't need this skill yet). Instead, learn how to download, check integrity and install Raspbian on your Raspberry PI.


Commands:
mount – Mount a file system
umount – Unmount a file system
fsck – Check and repair a file system
fdisk – Partition table manipulator
mkfs – Create a file system
fdformat – Format a floppy disk
dd – Write block oriented data directly to a device
genisoimage (mkisofs) – Create an ISO 9660 image file
wodim (cdrecord) – Write data to optical storage media

md5sum – Calculate an MD5 checksum

Here are the step-by-step instructions:


  1. Download the zip file containing the image from a mirror or torrent
  2. Verify if the the hash key of the zip file is the same as shown on the downloads page (optional). Assuming that you put the zip file in your home directory (~/), in the terminal run:
    • sha1sum ~/2012-12-16-wheezy-raspbian.zip
    • This will print out a long hex number which should match the "SHA-1" line for the SD image you have downloaded
  3. Extract the image, with
    • unzip ~/2012-12-16-wheezy-raspbian.zip
  4. Run df -h to see what devices are currently mounted
  5. If your computer has a slot for SD cards, insert the card. If not, insert the card into an SD card reader, then connect the reader to your computer.
  6. Run df -h again. The device that wasn't there last time is your SD card. The left column gives the device name of your SD card. It will be listed as something like "/dev/mmcblk0p1" or "/dev/sdd1". The last part ("p1" or "1" respectively) is the partition number, but you want to write to the whole SD card, not just one partition, so you need to remove that part from the name (getting for example "/dev/mmcblk0" or "/dev/sdd") as the device for the whole SD card. Note that the SD card can show up more than once in the output of df: in fact it will if you have previously written a Raspberry Pi image to this SD card, because the Raspberry Pi SD images have more than one partition.
  7. Now that you've noted what the device name is, you need to unmount it so that files can't be read or written to the SD card while you are copying over the SD image. So run the command below, replacing "/dev/sdd1" with whatever your SD card's device name is (including the partition number)
    • umount /dev/sdd1
    • If your SD card shows up more than once in the output of df due to having multiple partitions on the SD card, you should unmount all of these partitions.
  8. In the terminal write the image to the card with this command, making sure you replace the input file if= argument with the path to your .img file, and the "/dev/sdd" in the output file of= argument with the right device name (this is very important: you will lose all data on the hard drive on your computer if you get the wrong device name). Make sure the device name is the name of the whole SD card as described above, not just a partition of it (for example, sdd, not sdds1 or sddp1, or mmcblk0 not mmcblk0p1)
    • dd bs=4M if=~/2012-12-16-wheezy-raspbian.img of=/dev/sdd
      • Please note that block size set to 4M will work most of the time, if not, please try 1M, although 1M will take considerably longer.
    • Note that if you are not logged in as root you will need to prefix this with sudo
    • The dd command does not give any information of its progress and so may appear to have frozen. It could take more than five minutes to finish writing to the card. If your card reader has an LED it may blink during the write process. To see the progress of the copy operation you can run pkill -USR1 -n -x dd in another terminal (prefixed with sudo if you are not logged in as root). The progress will be displayed (perhaps not immediately, due to buffering) in the original window, not the window with the pkill command.
  9. Instead of dd you can use dcfldd; it will give a progress report about how much has been written.
  10. You can check what's written to the SD card by dd-ing from the card back to your harddisk to another image, and then running diff (or md5sum) on those two images. There should be no difference.
  11. As root run the command sync or if a normal user run sudo sync (this will ensure the write cache is flushed and that it is safe to unmount your SD card)
  12. Remove SD card from card reader, insert it in the Raspberry Pi, and have fun