212 F Network Programming and Design TCP/IP 5 March 2009 - PowerPoint PPT Presentation

Loading...

PPT – 212 F Network Programming and Design TCP/IP 5 March 2009 PowerPoint presentation | free to download - id: c76a6-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

212 F Network Programming and Design TCP/IP 5 March 2009

Description:

... bits (MSB) of IP address, it is classified into 5 classes : class A through E. ... BOOTP server maintains and provides auto-configuration of clients ... – PowerPoint PPT presentation

Number of Views:147
Avg rating:3.0/5.0
Slides: 125
Provided by: ABin3
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: 212 F Network Programming and Design TCP/IP 5 March 2009


1
212 F Network Programming and Design TCP/IP 5
March 2009
  • Philip Tsang

2
New Learning Ecology Innovation in LTR 3C C
Skills, YouTube and Podcasting
Contents
Critical Thinking
TL
Collaborative
Communicative
3
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 -gt MAC address
  • RARP MAC address -gt IP address
  • Transport Layer
  • TCP (Transmission Control Protocol)
  • UDP (User Datagram Protocol)

4
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

5
  • 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.1-150.150.5.254150.150.6.1-150.150.7.25
    4
  • See following IP Subset Calculator screen
    capture2
  • d) Network 150.150.252.0
  • Range of IP addresses 150.150.252.1-150.150.253.2
    542
  • (e)In part
  • the 2N-2 equation where N 10, 27-2126. 126
    subnets can be allowed.2
  • (f) The subnet mask 255.255.254.0 has 9 bits set
    to 0.
  • Use the 2N-2 equation where N 6, 29-2512512
    host addresses can be on each subnet.

6
  • (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.

7
  • Subnet mask consist the number between 0255,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.

8
List the first three valid network numbers. 1/2
  • Class A Network 8 bits (0Reserved
    127Loopback address) (e.g. US government , IBM)
  • Class B Network 16 bits Range128-191
  • Class C Network 24 bits. Range192-223

9
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

10
  • 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

11
  • 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

12
Quiz of 27 Feb 2008
  • Repeat your answers of Quiz of 25 Feb through a
    multimedia presentation

13
(No Transcript)
14
But How?
15
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

16
  • 5 questions for communication between 2 s/w (eg.
    Client server) on 2 different machines
  • How to identify a machine ??
  • How to identity a s/w running in a machine ??
  • How to transfer data from one machine to another
    ??
  • How to ensure all data are correctly received by
    destination s/w ??
  • 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) ??

17
  • Architecture basic concept

18
INTERNET (TCP/IP) architecture
19
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)

20
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.

21
Encapsulation De-capsulation
22
De-multiplexing
23
  • 5 questions for communication between 2 s/w (eg.
    client server) on 2 different machines
  • How to identify a machine ? IP address
  • How to identity a s/w running in a machine ? (IP
    address transport layer protocol type port
    number )
  • How to transfer data from one machine to
    another??
  • How to ensure all data are correctly received by
    destination s/w ??
  • 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) ??

24
  • Internet Layer
  • IP address
  • IP subnet CIDR
  • IP routing
  • IP fragmentation reassembly
  • ICMP ARP

25
IP packet format
26
Functions of internet layer
  • Functions of internet layer
  • Provide unique identification (i.e. logical IP
    address) to an interface of INTERNET node (eg.
    Host, router) or network/subnet
  • Classful
  • Classless (CIDR)
  • Packet forwarding (or routing)
  • Fragmentation
  • Reassembly

27
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.

28
IP Address (2)
29
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 drphiliptsang_at_gamil.com
  • Subject 212f Quizgt Your name Your SN

30
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
31
IP Address (4)
Class Network portion Host portion number of usable network number of usable hosts per network
A 0127 0.0.1 255.255.254 126 224-2
B 128.0 191.255 0.1 255.254 214 216-2
C 192.0.0 223.255.255 1254 221 28-2
D 224.0.0.0239.255.255.255 N/A multicast N/A
E 240.0.0.0255.255.255.255 N/A experiment N/A
32
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 gt better
    performance in Ethernet

33
IP Subnet (2)
34
IP Subnet (3)
35
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 mask255.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)

36
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 mask255.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

37
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 mask255.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

38
IP Subnet Quiz
  • Scenario 3
  • Class C network
  • default network mask XXX
  • Number of network X
  • Number of hosts per network XXX
  • subnet maskXXXXXX
  • Number of network XXXX
  • Number of hosts per network XXX
  • Effect one class B network increases from M to
    n subnets but the number of hosts per subnet
    decreases from N
  • Send answer to drphiliptsang_at_gmail.com
  • Subject 212F Subnet QuizgtYour Name SN

39
Online IP Calculator http//www.subnet-calculator.
com/
40
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

41
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?

42
IP default gateway
43
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 -gt MAC address)

44
IP V6
  • Previous discussion is for IPv4.
  • IPv6 is compatible with IPv4 with following
    enhancement
  • IP address 32 bits -gt 128 bits gt more IP
    available
  • Support QoS for timing critical application such
    as Voice over IP, video conference, etc.
  • Support security extension for authenticating
    source, providing integrity encryption.
  • Header processing is speed up to increase
    performance
  • 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

45
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

46
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

47
Labs and discussion of Quizzes
  • Remember to submit your Quiz to
    drphiliptsang_at_gmail.com
  • So far only half of your have submitted it.

48
  • 5 questions for communication between 2 s/w (eg.
    Client server) on 2 different machines
  • How to identify a machine ? IP address
  • How to identity a s/w running in a machine ? IP
    address transport layer protocol type port
    number
  • How to transfer data from one machine to another
    ? routing table IP layer
  • How to ensure all data are correctly received by
    destination s/w ??
  • 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) ??

49
212 F Network Programming and Design TCP/IP of
5-10 March 08
  • Philip Tsang

50
  • Transport Layer
  • TCP connection-oriented
  • UDP connectionless

51
TCP segment format
52
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 gt save
    bandwidth and processing power

53
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.

54
UDP datagram format
55
UDP
  • User Data Protocol (UDP)
  • Connectionless
  • Discrete datagrams
  • Unreliable (NO Sequence Ack., retransmission)
  • High-speed, minimal overhead
  • Eg. Multimedia application, TFTP, SNMP

56
  • 5 questions for communication between 2 s/w (eg.
    Client server) on 2 different machines
  • How to identify a machine ? IP address
  • How to identity a s/w running in a machine ? IP
    address transport layer protocol type port
    number
  • How to transfer data from one machine to another
    ? routing table IP layer
  • How to ensure all data are correctly received by
    destination s/w -gt TCP reliability
  • 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) -gt TCP flow control

57
  • Application Layer
  • DNS
  • Trouble-shooting commands
  • BOOTP DHCP
  • Telnet
  • FTP
  • SMTP

58
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?

59
DNS (2)
  • Structure of domain spacing
  • Hierarchical tree structure
  • Multiple levels
  • Root domain .
  • Top level domains
  • By functional com, net, org, mil, edu, gov,
  • By geographical hk, ca, jp, .
  • Lower levels eg. ibm.com, novell.com,
    cisco.com,

60
DNS (3)
61
BOOTP (BOOTstrap protocol)
  • BOOTP server maintains and provides
    auto-configuration of clients
  • Clients IP address, IP address of default
    router servers IP address, servers host name

62
DHCP (Dynamic Host Configuration Protocol)
  • Similar to BOOTP with 2 major differences
  • More configuration options
  • Clients 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 gt dynamic
  • Note that permanent allocation is still possible

63
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

64
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.

65
(No Transcript)
66
telnet
  • RFC 854 (RFC854)
  • http//www.faqs.org/rfcs/rfc854.html

67
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

68
exercises
  • Select five .hk site and telnet them to see
    the results.
  • Write a C/shell/script to automatically telnet
    hosts on a file.

69
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

70
http//www.w3.org/Protocols/rfc959/
71
commands
  • Get
  • Put
  • Del
  • Binary
  • Cd
  • Lcd
  • Dir
  • quite

72
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

73
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.

74
TCP/IP Administration Tools
  • Netstat
  • Ifconfig
  • Ping

75
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.e
    du.hk/t441051/ct212

76
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

77
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

78
Linux/UNIX architecture
kernel
shell
application
79
Linux/UNIX architecture
  • 3 parts
  • Utility, command or script eg. ls, mkdir,
    sort, named, ..
  • Shell (command interpreter)
  • Interpret command or script for kernel execution
  • Provide working environment for your login
    session
  • Provides script language
  • Kernel
  • Driver code
  • Actual Interface with memory, I/O
  • Execution of command

80
About Shell (1)
  • In using Linux/UNIX, you are always facing shell
  • When you login, you will be provided with a
    default shell (working shell), which provides a
    working environment for you.
  • 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)

81
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
  • login to the system
  • Type chsh, then input pathname of your desired
    shell
  • Pathname of all shells are /bin/sh

82
About Shell (3) startup script
  • Function setup initial environment variables
    when you login

shell System-wide User-wide
Bourne (sh) /etc/profile /.profile
C (csh) /etc/csh.login /.cshrc , then /.login
Korn (ksh) /etc/profile /.profile
Bash (bash) /.bash_profile
Tcsh (tcsh)
83
Directory file (1) file type
  • First column represents file type
  • normal file (-)
  • directory (d)
  • device file (b or c)
  • link file (l)

84
Directory file (2) - hierarchy
85
Directory file (3) file reference
  • You can reference file/directory by relative or
    absolute pathname

1. Absolute pathname (begins with /)
Current directory (shown by pwd)
2. Relative pathname (relative to current
directory) Begins with either . (current
directory) or .. (parent directory)
86
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

87
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

88
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.
89
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)
90
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)
91
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)
92
File permission right (5) command
  • How to change permission right

chmod is the command
700 is New permission right
AA is the filename
93
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)
94
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)
95
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)
96
Directory permission right (4)
  • How to change permission right for a directory

chmod is the command
dir1 is directory name
704 is New permission right
To change permission right of dir2, all files
and subdirectory under dir2 recursively, use
option -R
97
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 TERMvt100
  • change your password passwd
  • (follow the prompt message to change your
    password)

98
Fast Linux shell commands intro
  • http//www.youtube.com/watch?vLssPpKtb2oQfeature
    related

99
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 Joes Website -gt
    reference page and news page

100
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 Joes Website -gt News
    page
  • GUI mode, user-friendly
  • Installed by yourself
  • run on win9x/XP

101
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)

102
ASCII Animation
  • http//www.youtube.com/watch?vjnlpbQ5IxHkfeature
    related

103
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_nametest 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)

104
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. ? ( )
    lt gt
  • Accessed by VAR_NAME or VAR_NAME
  • Eg. VAR1abc
  • Eg. VAR2this is a variable
  • Eg. VAR3today is date and VAR1 is VAR1
  • Eg. VAR4\VAR1
  • Read and run file var.ksh (downloadable from
    Joes web site -gt tutorial page)

105
var.ksh
4 echo VARn statements not displayed
106
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
    Joes web site -gttutorial page)

107
Arithmetic of integers only
Arithmetic of real numbers
Echo is used to print .. to monitor
108
(No Transcript)
109
Shell programming (5) relational
  • expr1 -eq expr2
  • Is equal to
  • expr1 -ne expr2
  • Is not equal to
  • expr1 -lt expr2
  • Is less than
  • expr1 -le expr2
  • Is less than or equal to
  • expr1 -gt expr2
  • Is greater than
  • expr1 -ge expr2
  • Is greater than or equal to

At least one or more space
110
Shell programming (6) conditional
  • if condition1
  • then
  • command_block1
  • elif condition2
  • command_block2
  • else
  • command_block3
  • fi

111
Read is used to input from keyboard and assigned
value to variable INPUT_VAR
These are all logical conditions for flow control
statements
112
(No Transcript)
113
Shell programming (7) looping
  • syntax
  • while condition
  • do
  • command_block
  • done
  • Read and run file loop.ksh (downloadable from
    Joes web site -gt tutorial page)

114
Read input from keyboard and assigned value to
variable VAR
This is logical condition for while loop
Echo is used to print .. to monitor
115
(No Transcript)
116
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
    Joes web site -gttutorial page)

117
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
118
(No Transcript)
119
C programming (1) compilation
  • Function of c compilation
  • check for syntax error in source code
  • source code (.c) -gt object code (.o) -gt
    executable machine code
  • C compilation model
  • 4 components preprocessor, compiler, assembler,
    linker (for detail, refer to http//www.cs.cf.ac.u
    k/Dave/C/section2_4_4.html)
  • Available c compiler
  • gcc (better, more ansi-compatible)

120
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)

121
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 todays lecture

122
(No Transcript)
123
(No Transcript)
124
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
About PowerShow.com