30
CSC 382: Computer Security Slide #1 CSC 382: Computer Security Introduction to UNIX Programming

CSC 382: Computer SecuritySlide #1 CSC 382: Computer Security Introduction to UNIX Programming

Embed Size (px)

Citation preview

CSC 382: Computer Security Slide #1

CSC 382: Computer Security

Introduction to UNIX Programming

CSC 382: Computer Security Slide #2

Topics

1. What is UNIX?

2. Logging on.

3. Basic Commands.

4. The UNIX Shell

5. Compiling on UNIX

CSC 382: Computer Security Slide #3

What is UNIX?

UNIX:: /yoo'niks/ [In the authors' words, "A weak pun on Multics"] n. (also `Unix') An interactive time-sharing system originally invented in 1969 by Ken Thompson after Bell Labs left the Multics project, originally so he could play games on his scavenged PDP-7. Dennis Ritchie, the inventor of C, is considered a co-author of the system.

CSC 382: Computer Security Slide #4

Logging on to UNIX

General Categories– Console– Network

For the first assignment, we will log on tozappa.nku.edu

CSC 382: Computer Security Slide #5

1. Console

login: your_username <Enter>password: your_password <Enter>Last login: Sun Aug 28 19:35:32 2005 from

foo.com.You have new mail.Terminal type? [vt100] <Enter>Sun Microsystems Inc. SunOS 5.9 Generic May

2002

NOTICE: April 19, 2005 – The upgrade to Java JDK1.5.2 has been completed.

$

CSC 382: Computer Security Slide #6

2. Network Login using Putty on MS Windows client

CSC 382: Computer Security Slide #7

CSC 382: Computer Security Slide #8

3. Insecure Network Login via Telnet f/ Win client

CSC 382: Computer Security Slide #9

CSC 382: Computer Security Slide #10

Structure of a UNIX command

#command [[ - ] option(s)] [option argument(s)] [command argument(s)]

Examples:

• $ ls• $ ls -la• $ ls -la m*• $ lpr -Pspr -n 3 proposal.ps

CSC 382: Computer Security Slide #11

File Maintenance Commands

Creating, Deleting and Managing Files– cp, mv, rm, ls# cp myfile myfile2# mv myfile2 renamed_file# mv “latest revisions october.txt” laterevs.txt

# rm renamed_file# lsDesktopMailmyfile myfile2# ls –al

CSC 382: Computer Security Slide #12

File Maintenance Commands

Viewing the Contents of Files– cat, more, less

# cat > myfile

This is an example of how to use the cat command to add plain text to a file

<Ctrl-D>

# more myfile

This is an example of how to use the cat command to add plain text to a file

CSC 382: Computer Security Slide #13

File Maintenance Commands

Creating, Deleting and Managing Directories–mkdir, cd, pwd, rmdir# mkdir first

# cd first

# pwd

/home7/smithj/first

# cd

# pwd

/home7/smithj

# cp myfile myfile2

# ls my*

myfile myfile2

# rmdir first

rmdir: first: Directory not empty

CSC 382: Computer Security Slide #14

Obtaining Help with man

CSC 382: Computer Security Slide #15

Obtaining Help with man

man [options][-s section] command-list

# man lsUser Commands ls(1)

NAME

ls - list contents of directory

SYNOPSIS

/usr/bin/ls [-aAbcCdfFghilLmnopqrRstux1@] [file...]

/usr/xpg4/bin/ls [-aAbcCdfFghilLmnopqrRstux1@] [file...]

DESCRIPTION

For each file that is a directory, ls lists the contents of

the directory. For each file that is an ordinary file, ls

repeats its name and any other information requested. The

output is sorted alphabetically by default. When no argument

is given, the current directory is listed. …

CSC 382: Computer Security Slide #16

• whatis# whatis login setenvlogin login (1) - sign on to the systemsetenv set (1) - shell built-in functions to

determine the characteristics for environmental variables of the current shell and its descendents

• apropos# apropos webneon neon (3) - HTTP and WebDAV client libinstaller installer (1m) - Solaris Web Start installersmcwebserver smcwebserver (1m) - start the Sun consolewbem wbem (5) - Web-Based Enterprise Mgmt

Other Forms of Help

CSC 382: Computer Security Slide #17

What is a shell?

A command interpreter.– Runs external commands like cp and rm.

– Built-in commands change shell environment:• cd – change directory

• VAR=value

– I/O redirection.• cat /etc/shells >shells

– Ease of use• Command line editing, tab completion, history.

– Programming• Conditionals, loops, etc.

CSC 382: Computer Security Slide #18

Environment Variables

CSC 382: Computer Security Slide #19

Shell Initialization Files

• Configure shell settings at login.– Create aliases.– Set environment variables.

• bash initialization files– /etc/profile System-wide for sh and bash.– /etc/bashrc System-wide for bash.– ~/.bashrc User startup file.

CSC 382: Computer Security Slide #20

Globbing

• ?  Matches any one character.

• *  Matches zero or more characters.

• []  Matches list of characters inside brackets.

CSC 382: Computer Security Slide #21

Globbing

> ls *htmlannounce.html guidelines.html readings.html sites.htmlassignments.html index.html schedule.html> cd assignments> ls a[2-3]?htmla2.html a3.html

CSC 382: Computer Security Slide #22

Command History

Up-arrow Previous command

Down-arrow Next command

history List old commands

!! Previous command

!# Command #

!$ Last arg of previous command

CSC 382: Computer Security Slide #23

Command line editing

Ctrl-a Beginning of line

Ctrl-e End of line

Left-arrow Move back one character

Right-arrow Move forward one character

Ctrl-u Erase line

CSC 382: Computer Security Slide #24

Filename completion

TAB Completes filename

TAB-TAB Show list of possible

completions.

CSC 382: Computer Security Slide #25

UNIX C Programming

$ cat >hello.c

#include <stdio.h>

int main(int argc, char *argv[]) {

printf("Hello, world!\n");

return 0;

}

CSC 382: Computer Security Slide #26

UNIX C Programming

$ gcc –o hello hello.c

$ ./hello

Hello, world!

CSC 382: Computer Security Slide #27

gcc Flags

-ansi Use ANSI C 99 standard.

-pedantic Disallow C extensions.

-Wall Print all warnings.

-o file Name output file.

-g Include debugging info.

-ggdb Add extra GDB debug info.

CSC 382: Computer Security Slide #28

Command Line Arguments

argc – integer number of arguments

argv – array of character string arguments

CSC 382: Computer Security Slide #29

printargs.c

$ cat >printargs.c

#include <stdio.h>

int main(int argc, char *argv[]) {

int i;

for(i=0; i<argc; i++)

printf("arg[%d] = %s\n", i, argv[i]);

return 0;

}

CSC 382: Computer Security Slide #30

printargs

$ gcc –ansi –pedantic –Wall –o printargs printargs.c

$ ./printargs a b c 1 2 3

arg[0] = ./printargs

arg[1] = a

arg[2] = b

arg[3] = c

arg[4] = 1

arg[5] = 2

arg[6] = 3