Download IP address

Document related concepts

Deep packet inspection wikipedia , lookup

AppleTalk wikipedia , lookup

Computer network wikipedia , lookup

Network tap wikipedia , lookup

Distributed firewall wikipedia , lookup

Piggybacking (Internet access) wikipedia , lookup

List of wireless community networks by region wikipedia , lookup

Wake-on-LAN wikipedia , lookup

Airborne Networking wikipedia , lookup

Internet protocol suite wikipedia , lookup

Recursive InterNetwork Architecture (RINA) wikipedia , lookup

Cracking of wireless networks wikipedia , lookup

Zero-configuration networking wikipedia , lookup

Transcript
212 F
Network
Programming and Design TCP/IP
5 March 2009
Philip Tsang
New Learning Ecology
Innovation in LT&R: 3C + C Skills, YouTube and Podcasting
Critical Thinking
Contents
T&L
Collaborative
Communicative
Agenda for TCP/IP
•
•
•
•
Review of Quizzes of 25 /27 Feb
Cover Key concepts in Unit 3
TCP/IP architecture & Basic concepts
Internet Layer
 IP addressing, Subnet, IP Routing, Routing Table,
Routing protocols, CIDR, ICMP
• Address translation
 ARP : IP address -> MAC address
 RARP : MAC address -> IP address
• Transport Layer
 TCP (Transmission Control Protocol)
 UDP (User Datagram Protocol)
Quiz of 25 Feb 2008
•
•
•
•
•
Name:________________
Student #______________
Date: 25 Feb 2008
212F TCP/IP Self Assessment question Quiz
(1) Identify and describe two methods to handle the rapid depletion
of IP addresses. [2 marks]
• (2) Your boss asks you to create 124 sub-networks with the
assigned Class B address 150.150.0.0. Answer the following
questions:
–
–
–
–
–
–
What is the subnet mask? [1/2]
List the first three valid network numbers. [1/2]
List the range of host IP addresses on those three networks. [2]
List the last valid network and range of IP addresses. [2]
How many subnets does this solution allow? [2]
How many host addresses can be on each subnet? [1]
•
•
•
•
Answer
(a)To have 124 sub-networks, we need to borrow 7 bits from the
host field. So the subnet mask is 255.255.254.0.1
(b) 150.150.2.0, 150.150.4.0,
(c) 150.150.2.1.– 150.150.3.254; 150.150.4.1150.150.5.254;150.150.6.1-150.150.7.254
•
[See following IP Subset Calculator screen capture]2
•
•
d) Network: 150.150.252.0
Range of IP addresses: 150.150.252.1-150.150.253.2542
•
•
(e)In part
the 2^N-2 equation where N =10, 2^7-2=126. 126 subnets can be
allowed.2
(f) The subnet mask 255.255.254.0 has 9 bits set to 0.
Use the 2^N-2 equation where N =6, 2^9-2=512512 host addresses
can be on each subnet.
•
•
•
•
•
•
•
(1)
The first method is using the private IP for
the local network. Since private IP can use
in different local network repeatedly. So the
hosts no need to assign the public IP.
•
•
The second method is using more bits to
represent the IP address. So the number of
IP address will increase.
•
•
•
•
•
•
•
•
•
•
•
•
•
(2)
a) The subnet mask used to determine what
subnet an IP address belongs to.
b) The first three valid network numbers are
150.150.1.0, 150.150.2.0, 150.150.3.0
c) On 150.150.1.0, the range of host IP
addresses is 150.150.1.1 - 150.150.1.254
On 150.150.2.0, the range of host IP
addresses is 150.150.2.1 - 150.150.2.254
On 150.150.3.0, the range of host IP
addresses is 150.150.3.1 - 150.150.3.254
d) The last valid network is 150.150.254.0
Its range of host IP addresses is
150.150.254.1 – 150.150.254.254
e) This solution allows 128 subnets.
f) Addresses can be on each subnet are 510.
• Subnet mask consist
the number between
0~255,it allows you to
identify which part of
an IP address is
reserved for the
network, and which
part is available for
host use. Also it
required setting in the
TCP/IP control panel.
List the first three valid network
numbers. [1/2]
•
•
•
Class A :
Network 8 bits;
(0=Reserved ;127=Loopback address) (e.g. US
government , IBM)
Class B :
Network 16 bits; Range=128-191
Class C :
Network 24 bits. Range=192-223
c. List the range of host IP
addresses on those three
networks. [2]
• Class A: Host address is denoted by last
24 bits or last 3 octets.
• Host IP configuration for class A can be
shown as • network.host.host.host
• Class B:
Host address is denoted by
last 16 bits or last 2 octets.
• Host IP configuration for class B can be
shown as • network.network.host.host
• Class C : Host address address is denoted
by last 8 bits or last octet.
• Host IP configuration for class C can be
shown as • network.network.network.host
Quiz of 27 Feb 2008
• Repeat your answers of Quiz of 25 Feb
through a multimedia presentation
But How?
Objective of Unit 3
• Describe the concept of the Internet architecture
• Identify the different roles of IP, TCP and UDP in
Internet communication
• Describe the features of the IP
• Discuss IP naming and address translation
mechanisms
• Demonstrate an understanding of IP routing
• Compare connection-oriented and connectionless
communication
• Discuss TCP protocol mechanism
• Discuss UDP protocol mechanism
• Analyze the benefits & limitations of TCP and UDP
5 questions for communication between 2 s/w
(eg. Client & server) on 2 different machines
1. How to identify a machine ??
2. How to identity a s/w running in a machine ??
3. How to transfer data from one machine to
another ??
4. How to ensure all data are correctly received
by destination s/w ??
5. What to do if there is network congestion
due to :
•
•
Transmitting host sends out data too fast??
receiving capability of destination machine is not enough
(eg. Less processing power, memory, NIC speed, etc) ??
~~Architecture & basic concept~~
INTERNET (TCP/IP) architecture
Basic concepts (1)
• Encapsulation & de-capsulation
• De-multiplexing
• IP address
 uniquely identify a interface/host/subnet/network in
INTERNET (more discussion in later section)
• TCP port & UDP port are different
 Each application within a host is uniquely identified by a
port number; 16 bit;
 reserved well-known port : 1 ~ 1023
• Socket
 one end of each communication connection
 Uniquely identify which host, which application
 IP address + port number + protocol type (tcp or udp)
Basic concepts (2)
• Connection
identified by a pair of sockets on both ends
of connection. (protocol type, source IP
address, source port number, destination
IP address, destination port number)
• MTU (Maximum Transmission Unit)
Upper limit on size of data portion of
network access layer, excluding header
and trailer
Different Network access (link) layer
technologies have different MTU.
Encapsulation & De-capsulation
De-multiplexing
5 questions for communication between 2 s/w
(eg. client & server) on 2 different machines
1. How to identify a machine  IP address
2. How to identity a s/w running in a machine  (IP
address + transport layer protocol type + port number )
3. How to transfer data from one machine to another??
4. How to ensure all data are correctly received by
destination s/w ??
5. What to do if there is network congestion due to :
 Transmitting host sends out data too fast;
 receiving capability of destination machine is not
enough (eg. Less processing power, memory, NIC
speed, etc) ??
~~~~~~~~ Internet Layer
~~~~~~~~~
•IP address
•IP subnet & CIDR
•IP routing
•IP fragmentation & reassembly
•ICMP & ARP
IP packet format
Functions of internet layer
•
Functions of internet layer :
1. Provide unique identification (i.e. logical
IP address) to an interface of INTERNET
node (eg. Host, router) or network/subnet
 Classful
 Classless (CIDR)
2. Packet forwarding (or routing)
3. Fragmentation
4. Reassembly
IP Address (1)
• IP-v4 classful address :
 32 bit long, four 8-bit fields, separated by period
 2 formats :
Decimal : 192.123.44.23 (note each field
between period is 0 ~ 255), more convenient
Binary :
11000000.01111011.00110100.00010111
 Composed of 2 portions – network portion, host
portion
 All hosts of same network must have same value
on network portion
 Based on first 4 bits (MSB) of IP address, it is
classified into 5 classes : class A through E. Only
Class A through C can be assigned to a
machine/host.
IP Address (2)
Class attendance quiz
• Illustrate with steps to identify/show the
max value of the first decimal number of
Class C address is 223.
•
Send email to: [email protected]
• Subject: 212f Quiz> Your name + Your SN
IP Address (3)
• Network mask is used to specify which part of
an IP address is network-portion.
• Consecutive bit of value “1” specify
corresponding bit as network portion
Class
(Default) network mask
A
255.0.0.0
B
255.255.0.0
C
255.255.255.0
IP Address (4)
Class
Network
portion
Host
portion
number of
usable
network
number of
usable hosts per
network
A
0~127
0.0.1~
255.255.25
4
126
224-2
B
128.0~
191.255
192.0.0~
223.255.25
5
0.1~
255.254
1~254
214
216-2
221
28-2
224.0.0.0~23
9.255.255.25
5
N/A
multicast
N/A
C
D
IP Subnet (1)
• What is sub-netting?
 Divide a large network into a number of smaller subnets
 Each subnet is also a network, a broadcast domain. Subnets are
connected by router (not hub/LAN switch)
• How to sub-net?
 Network mask changes
 extends continuous ones towards right to host portion. The extension
part is called subnet portion. The network mask is now called subnet
mask.
• Each IP address now consists of 3 portions
 Network, subnet, host portion
• Why sub-netting :
 Better utilization of IP addressing space
 Reduces network congestion by reducing broadcast domain
 Reduce number of hosts per subnet => better performance in
Ethernet
IP Subnet (2)
IP Subnet (3)
IP Subnet (4)
• Scenario 1 :
• Class A network : 12.0.0.0
default network mask = 255.0.0.0
Number of networks = 1
Number of hosts per network = 2 24 -2
subnet mask=255.255.0.0
Number of subnets = 256 –2 = 254
Number of hosts per network = 2 16 –2
Effect : one class A network increases from
1 to 254 subnets but the number of hosts
per subnet decreases from (2 24 –2) to (2
16 –2)
IP Subnet (5)
• Scenario 2 :
• Class B network : 158.182.0.0
default network mask = 255.255.0.0
Number of network = 1
Number of hosts per network = 2 16 -2
subnet mask=255.255.240.0
Number of network = 24 – 2 = 14
Number of hosts per network = 2 12 – 2 = 4094
Effect : one class B network increases from
1 to 14 subnets but the number of hosts
per subnet decreases from (2 16 –2) to
4094
IP Subnet (5)
• Scenario 3 :
• Class C network : 192.182.0.0(?)
default network mask = 255.255.255.0
Number of network = 1
Number of hosts per network = 2 8 -2
subnet mask=255.255.255.240
Number of network = 24 – 2 = 14
Number of hosts per network = 2 12 – 2 = 4094
Effect : one class B network increases from
1 to 14 subnets but the number of hosts
per subnet decreases from (2 16 –2) to
4094
IP Subnet Quiz
• Scenario 3 :
• Class C network :
default network mask = XXX
Number of network = X
Number of hosts per network = XXX
subnet mask=XXXXXX
Number of network = XXXX
Number of hosts per network = XXX
Effect : one class B network increases from M to
subnets but the number of hosts per subnet
decreases from N
Send answer to: [email protected]
Subject 212F Subnet Quiz>Your Name +SN
Online IP Calculator
http://www.subnet-calculator.com/
IP Routing principle
• Unreliable; hop-by-hop routing
 Each hop (router) only provide IP address of next best hop (router)
to destination network/subnet
• Routing decision is made based on destination IP
address of incoming packet to be routed and
metrics for the path
• Each router/host has a routing table, from which
routing decision is made
• Contents of routing table can be :
 Dynamic, learned from routing protocols (eg. RIP, OSPF, IGRP,
EIGRP)
 Default route by hard-coded
 Static by hard-coded
 Directly connected network/subnet
IP Routing table
• Each entry of routing table must contain at least
following columns :
 IP address of destination network/subnet
 IP address of next hop (router) to destination
Note next hop is sometimes called gateway
 metric for that route
 Outgoing interface to next hop
• Default gateway :
 Definition : When there is no entry in routing table
matching destination IP address, entry of default
gateway will be used. Destination is represented by
0.0.0.0/0.0.0.0
 Host with single interface : simplify by using default
gateway entry
 One more usage of default gateway. What is it?
IP default gateway
ARP (Address Resolution Protocol)
• Function : Get MAC address of other TCP/IP host,
on the same segment, from IP address of that
host
(i.e. IP address -> MAC address)
IP V6
•
•
Previous discussion is for IPv4.
IPv6 is compatible with IPv4 with following
enhancement:
1. IP address : 32 bits -> 128 bits => more IP available
2. Support QoS for timing critical application such as
Voice over IP, video conference, etc.
3. Support security extension for authenticating source,
providing integrity & encryption.
4. Header processing is speed up to increase
performance
5. Addition of anycast address, which allows messages to
be sent to any of several possible service providers or
to any of a related group of remote destinations, with
the idea that any one of them can manage the
forwarding of the packet to others.
•
More : http://www.ipv6.org
IP - CIDR (1)
• Classless InterDomain Routing (CIDR)
• No more class boundary or subnet on IP
address
• Combine multiple continuous smaller
subnets (eg. Class C) into a larger supernet
or CIDR block
• Representation of CIDR network : prefix +
length
Eg. 158.182.4.40 /12
ICMP
• ICMP (Internet Control Message Protocol) :
• Network control and error reporting
 test node reachability across an internetwork with
echo and reply messages (eg. Ping)
 stimulate more efficient routing with redirect
messages (eg. ICMP redirect)
 “time exceeded” messages to inform sources that a
datagram has exceeded its allocated time to exist
within the internetwork (eg. Traceroute, tracert)
 Router advertisement and router solicitation
messages
Labs and discussion of Quizzes
• Remember to submit your Quiz to
[email protected]
•
So far only half of your have submitted it.
5 questions for communication between 2 s/w
(eg. Client & server) on 2 different machines
1.How to identify a machine  IP address
2.How to identity a s/w running in a machine  IP
address + transport layer protocol type + port number
3.How to transfer data from one machine to another
 routing table + IP layer
4.How to ensure all data are correctly received by
destination s/w ??
5.What to do if there is network congestion due to :
 Transmitting host sends out data too fast;
 receiving capability of destination machine is not
enough (eg. Less processing power, memory, NIC
speed, etc) ??
212 F
Network
Programming and Design
TCP/IP of 5-10 March 08
Philip Tsang
~~~~~ Transport Layer ~~~~~
TCP : connection-oriented
UDP : connectionless
TCP segment format
TCP – basics (1)
• Connection-oriented; full-duplex
• stream data transfer - segmenting data from
application
• Data transfer involves 3 steps :
 Connection establishment (3-way handshake)
 Actual data transfer
 Connection termination (4-way)
• Some terms:
 sequence number – identify each byte of segment data
 acknowledge number – sequence number of next byte of
data sender expects to receive
 delayed acknowledge – acknowledgement does not
respond immediately for each received segment, wait
until multiple segments are received => save bandwidth
and processing power
TCP – basics (2)
• Reliability is provided by :
 Sequence number, Ack. number, delayed ack., Timeout
& Retransmission, discard duplicate
 Sender starts a timer for sending each packet.
 Receiver sends +ve acknowledge (ACK) some delays
after receiving packet
 If sender does not receive ACK for a packet when timer
expires (i.e. timeout), sender retransmits that packet
– Retransmission may lead to duplicate packet. Duplicated
packet is discarded by receiver.
• Flow Control – sliding windows buffering. Buffer
size is integral multiple of segment size. Sender
transmits data continuously until number of
segments sent without acknowledge equals length
of window.
UDP datagram format
UDP
• User Data Protocol (UDP)
Connectionless
Discrete datagrams
Unreliable (NO Sequence & Ack.,
retransmission)
High-speed, minimal overhead
Eg. Multimedia application, TFTP, SNMP
5 questions for communication between 2 s/w (eg.
Client & server) on 2 different machines
1.How to identify a machine  IP address
2.How to identity a s/w running in a machine  IP
address + transport layer protocol type + port number
3.How to transfer data from one machine to another
 routing table + IP layer
4.How to ensure all data are correctly received by
destination s/w -> TCP reliability
5.What to do if there is network congestion due to :
 Transmitting host sends out data too fast;
 receiving capability of destination machine is not
enough (eg. Less processing power, memory, NIC
speed, etc) -> TCP flow control
~~~~ Application Layer ~~~~~
•DNS
•Trouble-shooting commands
•BOOTP & DHCP
•Telnet
•FTP
•SMTP
DNS (1)
• Function of DNS
Resolve domain name (eg. www.ouhk.edu.hk) into
IP address
• Why DNS ?
For human being, it is easier to remember domain
name of a host rather than its IP address
• Distributed and hierarchical domain name server &
database
• Delegation of domain authority
• Each DNS server can server its own domain or
lower-level domains
• Each domain must be served by at least 2 DNS
servers. Why?
DNS (2)
• Structure of domain spacing:
Hierarchical tree structure
Multiple levels :
Root domain : .
Top level domains :
o By functional : com, net, org, mil, edu, gov, …
o By geographical : hk, ca, jp, ….
Lower levels : eg. ibm.com, novell.com,
cisco.com,…
DNS (3)
BOOTP (BOOTstrap protocol)
• BOOTP server maintains and provides
auto-configuration of clients
Client’s IP address, IP address of default
router; server’s IP address, server’s host
name
DHCP (Dynamic Host Configuration Protocol)
• Similar to BOOTP with 2 major differences:
More configuration options
Client’s IP address, subnet mask, IP
address of default router, IP address of
DNS server, other options
Lease time for IP assignment can be finite.
Allows release and reallocation of
configuration information => dynamic
Note that permanent allocation is still
possible
Telnet
•
What is a Telnet Client? In client/server computing, you need to
be able to send data to and receive data from a host. To do this, a
general terminal or a terminal emulator is normally used. Naturally,
the terminal or the PC that is running a terminal emulator must be
connected to the server or mainframe computer, because otherwise
data transmission would not be possible. To connect the terminal to
the server, there are various different methods that can be used.
Among these methods are serial cables, modems and existing
networks. For a long time, the most common network connection
method was Telnet, a standard protocol for connecting two different
computers. With Telnet, you can connect to the server, input
commands, and run programs. Nevertheless, Telnet has one major
drawback: all data is exchanged without any form of encryption.
• In a corporate environment where LAN cables and infrastructure are
controlled by the owners, Telnet may well be sufficient. However, it
can present a serious security risk when you�re transmitting over a
wide area or using public lines. When you're using Telnet, anybody
with some network knowledge and tools can monitor the data
exchange, even gaining access to details like usernames and
passwords. For this reason, SSH (Secure Shell) is usually a better
alternative nowadays.
• http://www.emtec.com/zoc/telnet-client.html
Telnet
• - What is Telnet?
Telnet is a user command and an underlying
TCP/IP protocol for accessing remote computers.
Through Telnet, an administrator or another user
can access someone else's computer remotely.
On the Web, HTTP and FTP protocols allow you
to request specific files from remote computers,
but not to actually be logged on as a user of that
computer. With Telnet, you log on as a regular
user with whatever privileges you may have
been granted to the specific application and data
on that computer.
telnet
• RFC 854 (RFC854)
• http://www.faqs.org/rfcs/rfc854.html
Additional information
• IANA - oversee allocation of port
number of Internet Protocol;
• ICANN (APNIC, RIPE, ARIN) –
allocation of IP address in Asia Pacific,
Europe & America respectively
• INTERNIC – domain name registration
exercises
• Select five .hk site and ‘telnet them’ to see
the results.
• Write a C/shell/script to automatically
telnet hosts on a file.
FTP
•
File FTP is an acronym for File Transfer Protocol.
•
- File Transfer Protocol (FTP), a standard Internet protocol, is the simplest way to
exchange files between computers on the Internet. Like the Hypertext Transfer
Protocol (HTTP), which transfers displayable Web pages and related files, and the
Simple Mail Transfer Protocol (SMTP), which transfers e-mail, FTP is an application
protocol that uses the Internet's TCP/IP protocols. FTP is commonly used to transfer
Web page files from their creator to the computer that acts as their server for
everyone on the Internet. It's also commonly used to download programs and other
files to your computer from other servers.
As a user, you can use FTP with a simple command line interface (for example, from
the Windows MS-DOS Prompt window) or with a commercial program that offers a
graphical user interface. Your Web browser can also make FTP requests to download
programs you select from a Web page. Using FTP, you can also update (delete,
rename, move, and copy) files at a server. You need to logon to an FTP server.
However, publicly available files are easily accessed using anonymous FTP.
Basic FTP support is usually provided as part of a suite of programs that come with
TCP/IP. However, any FTP client program with a graphical user interface usually
must be downloaded
from the company that makes it.
http://www.kb.indiana.edu/data/aerg.html
•
•
•
•
http://www.w3.org/Protocols/rfc959/
commands
•
•
•
•
•
•
•
•
Get
Put
Del
Binary
Cd
Lcd
Dir
quite
SMTP
•
•
•
•
Simple mail Transfer Protocol
pronounced as separate letters) Short for Simple Mail Transfer Protocol, a
protocol for sending e-mail messages between servers. Most e-mail
systems that send mail over the Internet use SMTP to send messages from
one server to another; the messages can then be retrieved with an e-mail
client using either POP or IMAP. In addition, SMTP is generally used to
send messages from a mail client to a mail server. This is why you need to
specify both the POP or IMAP server and the SMTP server when you
configure your e-mail application.
[pronounced as separate letters) Short for Simple Mail Transfer Protocol, a
protocol for sending e-mail messages between servers. Most e-mail
systems that send mail over the Internet use SMTP to send messages from
one server to another; the messages can then be retrieved with an e-mail
client using either POP or IMAP. In addition, SMTP is generally used to
send messages from a mail client to a mail server. This is why you need to
specify both the POP or IMAP server and the SMTP server when you
configure your e-mail application. ]
[See also http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol]
SMTP exercises
• Select five com.hk sites and five .com and
‘telnet to their SMTP port’ to see the
results.
• Write a C/shell/script to automatically
telnet hosts on a file.
TCP/IP Administration Tools
• Netstat
• Ifconfig
• Ping
212 (April 2007)
Network
Programming and Design
Tutorial 4 for group 1
(Internet application development on the Unix
Platform)
Course Coordinator : Dr. Philip Tsang
Tutor : Mr. Joe Lee
URL of my teaching website :
http://ucourse.ouhk.edu.hk/~t441051/ct212
Agenda for Tutorial 4
•
•
•
•
•
•
•
•
•
•
•
•
Review for Unit 3
Objectives of Unit 4
Your Linux/UNIX accounts provided by OUHK
Linux/UNIX architecture
About shell
Linux/UNIX file and directory
Linux/UNIX permission right
Hint for Linux/UNIX
Linux/UNIX editor
File transfer (ftp, wsftp)
Shell programming
Compiling C program in Linux/UNIX
Objective of Unit 4
• Identify the key features of the UNIX
programming environment
• Discuss the concept of UNIX shells
• Discuss syntax of UNIX commands
• Describe the UNIX file and directory systems
• Introduce a pico text editor in UNIX
• Introduce some basic concepts in Shell
programming
• Describe the compilation tool available in
UNIX
Linux/UNIX architecture
kernel
shell
application
Linux/UNIX architecture
•
3 parts :
1. Utility, command or script : eg. ls, mkdir, sort,
named, ..
2. Shell (command interpreter)
•
•
•
Interpret command or script for kernel execution
Provide working environment for your login session
Provides script language
3. Kernel



Driver code
Actual Interface with memory, I/O
Execution of command
About Shell (1)
•
In using Linux/UNIX, you are always facing
shell:
1. When you login, you will be provided with a
default shell (working shell), which provides a
working environment for you.
2. When you run a shell script or command, a shell
will be invoked to interpret your shell script or
command.
–
Shell type of this invoked shell can be of same or
different from your working environment shell.
– This is controlled by first line of your shell script (eg.
#!/bin/sh)
About Shell (2)
•
Different shell type provides:

Different working environment, which can be
customized by your startup script
Different startup script
Different syntax of shell script statement


•
•
Default shell of Red Hat Linux is k-shell (ksh)
How to know my current working shell
–
•
Echo
$SHELL
How to change your default shell
1. login to the system
2. Type “chsh”, then input pathname of your desired
shell

Pathname of all shells are /bin/*sh
About Shell (3) – startup script
• Function : setup initial environment variables
when you login
shell
Bourne (sh)
C (csh)
System-wide
/etc/profile
/etc/csh.login
User-wide
~/.profile
~/.cshrc , then ~/.login
Korn (ksh)
/etc/profile
~/.profile
Bash (bash)
Tcsh (tcsh)
~/.bash_profile
Directory & file (1) – file type
First column represents file type :
normal file (-)
directory (d)
device file (b or c)
link file (l)
Directory & file (2) - hierarchy
Directory & file (3) – file reference

You can reference file/directory by relative or absolute pathname
1. Absolute pathname
Current directory (shown by pwd)
(begins with “/”)
2. Relative pathname (relative to current directory)
Begins with either “.” (current directory) or “..” (parent directory)
Directory & file (4) - wildcard
• To reference a group of files/directories with
common in filename, we use “wildcards”:
– * matches any (zero or more) characters except
that first character cannot be “.”
– ? matches any single character except “.”
– [a-z,A-Z,0-9] matches any single character which
is alphanumeric
– [!a-z,A-Z,0-9] matches any single character which
is not alphanumeric
Directory & file (5) - Command
•
•
•
•
To display current directory : pwd
Change current directory : cd new_dir_pathname
Create a directory : mkdir dir_name
Delete a directory : rmdir dir_name (dir. must be
empty)
or rm -R dir_name (even dir. is not empty)
• List content of a directory : ls –l dir_pathname
• Delete file(s) : rm filename
• Display content of a file :
cat
filename
or more filename
• Copy or overwrite file :
cp
src_filename
dest_filename
• Rename file : mv old_filename new_filename
• Move file : mv src_filepathname new_filepathname
File permission right (1)
To display permission right of file
File owner
Group owner
Filename
These 9 columns represents the permission rights for 3 types of
users – 3 columns for file owner, 3 columns for group owner, 3
columns for others.
Only file owner and root can change the permission right.
File permission right (2) –File owner
To display permission right of file
These 3 columns represents the permission rights
for file owner. File owner has read ( r ) , write (w) ,
execute (x) right for file AA2. Numerically, his
right is 700 (111 000 000)
File permission right (3) – group owner
To display permission right of file
These 3 columns represents the permission rights
for group owner. Group owner has read ( r ) , no
write (-) , execute (x) right for file BB. Numerically,
his right is 750 (111 101 000)
File permission right (4) – others
To display permission right of file
These 3 columns represents the permission rights
for others. Others has read ( r ) , no write (-) ,
execute (x) right for file CC. Numerically, his right
is 705 (111 000 101)
File permission right (5) – command
• How to change permission right
chmod is the command
700 is New permission right
AA is the filename
Directory permission right (1)
To display permission right of directory
Still classify users into 3 types – file owner, group owner, others
Meaning of r, w, x is different from that for file.
r : allow listing content of directory (i.e. ls /dir1/*)
x : allows enter (i.e. cd ) into the directory
w + x : allow creating, deleting, renaming files within the
directory
Numeric permission is similar to that for file – 700 (111 000 000)
Directory permission right (2)
To display permission right of directory
Setting “dir2 with this permission right (xx1)” + “read right (xx4)
for html files under dir2”
 html files under dir2 can be read by everyone (others) via
browser
Numeric permission is 701 (111 000 001)
Directory permission right (3)
To display permission right of directory
Setting “dir3 with this permission right (xx5)” + “read right (xx4)
for html files under dir3”
 html files under dir3 can be “read by everyone (others) via
browser” + “everyone (others) can list filenames of files under dir3
via browser”
Numeric permission is 705 (111 000 101)
Directory permission right (4)
• How to change permission right for a directory
chmod is the command
704 is New permission right
dir1 is directory name
To change permission right of “dir2, all files and
subdirectory under dir2” recursively, use option “-R”
Useful hints for Linux/UNIX
•
•
•
•
•
•
•
•
•
•
all input to UNIX is case-sensitive
command syntax :
command_name [-options] [arguments]
You can get online help of a command by “man
command_name”
types of variables – environment, shell variable
check your environment variable : env
default path : echo $PATH
your home directory : $HOME (or ~)
goto your home directory : cd
How to change your environment variable (eg. $TERM) :
export TERM=vt100
change your password : passwd
(follow the prompt message to change your password)
Fast Linux shell commands intro
• http://www.youtube.com/watch?v=LssPpKt
b2oQ&feature=related
UNIX editors (1)
• 2 most common editors :
– pico
• more user-friendly,
• Easy to learn,
• more suitable for normal user
– vi
•
•
•
•
more powerful,
hard to learn,
more suitable for administrator
learn by yourself through Joe’s Website ->
reference page and news page
File transfer
• 4 tools : ftp or wsftp or winscp, filezilla
– ftp
• already exist on every unix machine or
win9x/NT/2000
• Command mode
• Commands : open, quit, help, ascii, binary, put,
mput, get, mget, prompt
– wsftp
• Freeware, downloadable from Joe’s Website ->
News page
• GUI mode, user-friendly
• Installed by yourself
• run on win9x/XP
Shell programming (1) – script basics
• How does UNIX know this is shell script?
– It check the first line of script file for #!/bin/ksh (or
#!/bin/sh or #!/bin/csh)
• How to make shell script executable by yourself?
– In unix prompt, chmod 700 script_filename
• How to run shell script?
– For ksh or sh script,
• Pathname_of_script (eg. ./test.ksh or
/data1/home1/tutors/t441051/test.ksh)
– For csh script,
• source Pathname_of_script (eg. source
~/.test.csh)
ASCII Animation
• http://www.youtube.com/watch?v=jnlpbQ5I
xHk&feature=related
Shell programming (2) – script basics
• Contents of a script can be:
–
–
–
–
Comment prefixed by #
Shell built-in commands (mkdir, cd, echo, eval, etc.)
UNIX external utilities (cp, tar, grep, awk, etc.)
Shell variable assignment statement (Var_name=“test”;
variable is declared and assigned at the same time)
– Arithmetic or relational statement
– Control flow statement :
•
•
•
•
Conditional expression (if, then, else, elseif)
Looping expression
Case statement
Test statement
– File I/O statement (read for input or echo for output)
Shell programming (3) – shell variable
• Declaration and assignment within one statement
• Content is manipulated as string
• Content can be
– single, double quoted for string
– backslash quoted for single character with special meaning , eg. $ ? & ‘ ’
“”() [] | <>`
•
•
•
•
•
•
Accessed by ${VAR_NAME} or $VAR_NAME
Eg. VAR1=abc
Eg. VAR2=‘this is a variable’
Eg. VAR3=“today is `date` and VAR1 is $VAR1”
Eg. VAR4=“\$VAR1”
Read and run file var.ksh (downloadable from Joe’s web site ->
tutorial page)
var.ksh
4 “echo VARn” statements not
displayed
Shell programming (4) – arithmetic
• Two methods : make use of either utility
expr or bc
• expr
– support arithmetic of 2 integers only
–+ - * / %
• bc
– Support floating point arithmetic of expression
– Not include %
• Read and run file arith.ksh (downloadable
from Joe’s web site ->tutorial page)
Arithmetic of integers only
Arithmetic of real numbers
Echo is used to print “…..” to
monitor
Shell programming (5) – relational
•
[
expr1
-eq
expr2
]
-ne
expr2
]
-lt
expr2
-le
expr2
]
-gt
expr2
]
-ge
expr2
]
– Is equal to
•
[
expr1
– Is not equal to
•
[
expr1
]
– Is less than
•
[
expr1
– Is less than or equal to
•
[
expr1
– Is greater than
•
[
expr1
– Is greater than or equal to
At least one or
more space
Shell programming (6) – conditional
if condition1
then
command_block1
elif condition2
command_block2
else
command_block3
fi
Read is used to input from
keyboard and assigned value
to variable “INPUT_VAR”
These are all logical
conditions for flow control
statements
Shell programming (7) –looping
• syntax :
while condition
do
command_block
done
• Read and run file loop.ksh (downloadable from
Joe’s web site -> tutorial page)
Read input from keyboard
and assigned value to
variable “VAR”
This is logical condition for
while loop
Echo is used to print “…..” to
monitor
Shell programming (8) – case
• syntax :
case string in
pattern1) command_block1
pattern2) command_block2
patterni) command_blocki
*) command_block
esac
;;
;;
;;
;;
• Read and run file case.ksh (downloadable from
Joe’s web site ->tutorial page)
If value $VAR is one of these
values, corresponding
command(s) will be executed
If value $VAR is NOT one of
those listed values,
command(s) in *) will be
executed
C programming (1) – compilation
• Function of c compilation :
– check for syntax error in source code
– source code (*.c) -> object code (*.o) ->
executable machine code
• C compilation model :
– 4 components – preprocessor, compiler,
assembler, linker (for detail, refer to
http://www.cs.cf.ac.uk/Dave/C/section2_4_4.
html)
• Available c compiler :
– gcc (better, more ansi-compatible)
C programming (2) – compilation
• How to compile :
• 1. For single source file :
– compile to executable code :
• gcc *.c (a.out is default filename for
executable code)
– compile up to object code file but no linking :
• gcc –c *.c (*.o will be output)
– compile to executable code but specify filename for
executable code :
• gcc –o out_exe *.c (out_exe will be
executable code)
C programming (2) – compilation
• 2. For multiple source files :
– make utility + makefile (or Makefile) file
– makefile file syntax :
• Maintarget: requirements1
instructions1
target1: requirements2
instructions2
target2: requirements3
instructions3
• Try make with makefile and c programs
from the hand out of today’s lecture
References
• Ken Steube
•
UNIX Bourne Shell Scripting
http://www.esscc.uq.edu.au/~ksteube/Bshell/
• Norman Matloff ,
•
Unix Shell Scripts http://heather.cs.ucdavis.edu/~matloff/UnixAndC/Unix/CShellII.html
• Mendel Cooper
– Advanced Bash-Scripting Guide
– http://tldp.org/LDP/abs/html/
• Phil
– Internet Radio to Podcast with Shell Tools" in the September, 2005 issue of
LINUX JOURNAL, http://www.linuxjournal.com/article/8171