스크립트 작성 후 실행권한을 주려면.. Chmod
755 DiskUsageCheck.sh 쉘 스크립트의 첫번째 라인에는 대부분 #!/bin/sh 라는 문장이 있는데, 이는 어떤
쉘을 이용하여 이 프로그램을 실행시킬 것인지를 지정하는 것이다. 즉,스크립트가 실행되면 /bin/sh 프로그램 하에서 실행됨을 의미한다. 변수 그냥 변수명을 적으면 된다. 변수의 타입이나 형을 명시하지 않는다. 변수에 값을 할당하지 않아도 특별한
문제는 발생하지 않으며, NULL로 초기화하고 나중에 값을 할당하려고 한다면 다음과 같이 한다. name= 변수에 값을 할당할 때는 ‘=’ 기호를 사용하는데 = 사이에 공백을 넣으면 안된다. name=JUNGJH # 맞다. name =
JUNGJH # 틀리다. 만일 입력하려는 이름에 공백이 있다면
다음처럼 한다. name="JUNG
J H" 만일 인수로 입력되는 값이 있을 때
이를 변수로 받는 방법은 ‘$숫자’ 이다. 즉, $1은 첫번째 인수이고$2는 두번째 인수이다. 변수의 내용을 출력하려면 ‘echo’를 사용한다. 이때 변수임을
알리기 위해서 $를 넣는다. echo
$name 변수에 와일드 문자를 넣어서 사용할
수도 있다. filename=*.sh echo
$filename # 확장자가 sh로 되어 있는 파일들의 목록이 나온다. 변수를 이용하여 스트링의 일부를 변경할
수도 있다. 예를 들어 option이라는 변수에 ‘l’를 입력한
후ls –“$option” 을 사용하면 ls –l 과 동일한
문장이 된다. <예. ShellTest2.sh> 스크립트 파일 내부에서 개발자가 unset 키워드로 변수를 제거할 수 있다. name="Jung
JH" unset
name newname=$name #
newname= 과 같다. 쉘 스크립트에서도 상수를 만들 수 있다. 이때 사용되는 키워드는 ‘readonly’이다. readonly만 쓰면 읽기전용으로 된 상수들을 모두 화면에 표시한다. Name="Jung
JH" Readonly
name # 이 후로는 값을 바꿀 수 없다. 쉘 스크립트에서는 쉘 전체가 사용하는
환경변수도 일반 변수와 똑 같은 방법으로 사용할 수 있다. 변수에 값을 할당할 때 입력줄에서 입력받은
내용을 할당하는 방법이 있다. 즉, 스크립트가
실행되는 중간에 변수에 할당할 내용을 사용자로부터 입력받은 후 그 내용을 할당하는 것이다. 이때 사용하는 키워드가 ‘read’인데 이 키워드와 변수를 함께 적으면 그 라인이 실행될 때 사용자로부터 입력할
것을 요청하게 된다. <Grep.sh> #!/bin/sh echo
"grep에 사용될 옵션 입력,
-l 또는 -s를 입력해주세요." read
option1 echo
"검색하고자 하는 문장을 입력해주세요" read
option2 grep
$option1 $option2 * 쉘에서 사용되는 특정 키워드는 다음과
같다. $# - 명령입력 라인에서 입력된 인수의 개수를 가지고 있다. $* - 명령입력 라인에서 입력된 전체의 내용을 포함한다. 즉, $1 ~ $n을 모두 합친것과 같다. $$ - 쉘 프로그램이 실행되면서 사용된 프로세스의 ID 값을 가진다. $! - 쉘 프로그램이 실행시킨 백그라운드 프로세스의 ID 값을 가진다. <예. shellTest.sh> 위의 키워드를 사용한 다양한 응용을
한다. ‘set’ 명령어를 이용하면 현재 쉘에서 사용되고 있는 변수들의 리스트를 확인할 수 있다. <예. shellTest4.sh> 쉘의 환경변수를 몇가지 확인한다. 쉘에서 변수를 사용할 때 변수에 값을
할당된 경우에는 변수가 가진 값을 사용하고, 그렇지 않으면 디폴트의 다른값을 활용하는
방법이 있다. 더 나아가 지정한 내용을 값이 할당되지
않은 변수속에 할당하는 방법도 있다. ${X:-Y} # X에 값이 있으면 그대로, 그렇지 않으면 Y를 사용, X값 변동없음 ${X:=Y} # Y가 사용되고 난 후 X에 Y값 할당 ${X:?에러메시지} # X에 값이 없으면 에러메시지와 함께 종료 <예. shellTest4.sh> 위의 예를 활용한다. 제어문(조건문) 쉘에서 명령어들을 순차적으로 실행할
때 ‘||’ 연산자와 ‘&&’ 연산자를
이용하여 연산자 바로 뒤에 위치한 명령어가 조건에 따라 실행되도록 만들 수 있다. '||’ 연산자는 앞의 명령어가 실패했을 때만 뒤에 위치한 프로그램이 실행되도록 한다. ‘&&’ 연산자는 반대로 앞의 명령어가 성공적으로 수행되었을 때만 뒤의 프로그램이 실행된다. 다음은 명령어 실행이 실패했을 때 이
내용을 로그로 남기는 작업이다. Run
Command1 || echo 첫번째 명령 실행 실패 >> log.txt Run
Command2 || echo 두번째 명령 실행 실패 >> log.txt 또한 다음은 실패가 예상되는 명령에
대한 보완 실행이다. ShellTest5.sh
|| chmod 755 ShellTest5.sh && ShellTest5.sh If-else 구문의 형식은
다음과 같다. if [
conditons ] then 실행하고자 하는 문장 fi ######### if [
conditons ] then 조건만족시 실행하고자 하는 문장 else 조건 불만족시 실행하고자 하는 문장 fi ########## if [
conditons ] then 조건만족시 실행하고자 하는 문장 elif [
conditons ] 두번째 조건 만족시 실행 문장 else 조건 불만족시 실행하고자 하는 문장 fi <예. IfTest1.sh> if 조건문에서
사용되는 연산자들에는 다음과 같은 것들이 있다. l 문자열체크 [
stringName ] - 문자열이 널(NULL)인지 체크, Null이 아니면 참 [ -n
stringName ] - 문자열의 사이즈가 0 이상인지 체크,
0 이상이면 참 [ -z
stringName ] - 문자열의 사이즈가 0 인지 체크, 0이면 참 [
stringNameA = stringNameB ] - A문자열과 B문자열이 같은지 체크, 같으면 참 [
stringNameA != stringNameB ] - A문자열과 B문자열이 다른지 체크, 다르면 참 l 숫자 대소
관계 체크 [
intA -ge 100 ] - 숫자 A가 100보다 크거나 같은지 체크, 100 이상이면 참 [
intA -gt 100 ] - 숫자 A가 100보다 큰지 체크, 100이 넘으면 참 [
intA -le 100 ] - 숫자 A가 100보다 작거나 같은지 체크, 100 이하이면 참 [
intA -lt 100 ] - 숫자 A가 100보자 작은지 체크, 100 미만이면 참 l 파일 체크 [ -r
filename ] - 해당 파일이 읽기 가능한지 체크 [ -w
filename ] - 해당 파일이 쓰기 가능한지 체크 [ -x
filename ] - 해당 파일이 실행 가능한지 체크 [ -s
filename ] - 해당 파일의 사이즈가 제로 이상인지 체크 [ -d
filename ] - 해당 파일이 디렉토리 파일인지 체크 [ -f
filename ] - 해당 파일이 보통 파일인지 체크 [ -h
filename ] - 해당 파일이 링크 파일인지 체크 l 조건문의
결합 [ 조건문A -a 조건문B ] - 조건문 A와 B가 모두 참인지 체크, -a는 AND [ 조건문A -o 조건문B ] - 조건문 A와 B중 참이 하나라도 있는지 체크, -o는 OR <예. IfTest2.sh> case문도 제공을 하는데 형식은 다음과 같다. case $변수명 in 문장1) 첫번째 명령어;; 문장2) 두번째 명령어;; 문장3) 세번째 명령어;; *) Default 명령어;; esac <예. caseTest.sh> 제어문(반복문) while문의 형식은 다음과 같다. while [ 조건 ] do 명령어 구문 done <예. whileTest.sh> #!/bin/sh while [
$# -gt 0 ] echo "< $1 명령 실행 > " $1 Shift done 위의 예제에서 shift 명령어를 사용하는데. Shift는 인수로 들어온 내용을 하나씩 옮겨가는 기능을 한다. 즉, shift가 한번 실행되면 $1은 $2이 되고 $2는 $3이 된다.(???) until 구문과 비교해보라.. #!/bin/sh until [ $# -le 0 ] echo "< $1 명령 실행 > " $1 Shift done for 구문의 형식은
다음과 같다. for 변수명 in value1 value2
... do 반복 실행 문장 done while/until 에서 사용한
예제를 for로 변경하면 다음과 같다. #!/bin/sh for
variable in
$* # $* : 인수로 들어온 모든 문자열 do echo “<
$variable 명령
실행>” $variable done for 키워드 뒤에
변수명(variable)이 오고 in 키워드 뒤에
값들이 나열된다. 이것은 in 뒤에 있는
값이 하나씩 variable에 할당이 되면서 값의 할당이 끝날때까지 do~done를 반복한다. <예. breakContinue.sh> <예. runCmd> 여러 개의 쉘 명령어를 한꺼번에 실행하도록
만들어 주는 예제 쉘 명령어가 옵션이 있는지 여부를 체크하고, 옵션이 있으면 옵션과함께 명령어를 실행하고 옵션이 없으면 명령어만 실행하는데, 이를 위해 while문과 다중 if문, case문을 사용한다. 함수 작성 함수를 사용하려면 스크립트의 시작부에
정의가 되어 있어야 한다. 함수의 형식은 다음과 같다. 함수명() { 함수 내용 } 함수 호출은 인자가 없는 함수는 함수명만
호출하면 되고, 인자가 있는 함수는 함수명 뒤에 인자를 나열해주면 된다.( 괄호는 쓰지 않는다.) 함수 내부에서 넘겨받은 인자를 쓰는
방법은 쉘 프로그램이 인자를 쓰는 방법과 같다.($1, $2, …)
'Unix'에 해당되는 글 2건
- 2008/12/11 리눅스 - 쉘 스크립트 기초 문법
- 2008/12/10 보안(원문) - SECURING YOUR UNIX SYSTEMS
SECURING YOUR UNIX SYSTEMS
- White Papers
- UNIX Password Security
- Password Security: A Case History
- Securing RedHat Linux | Solaris
- A Comaparison of the Security of Windows NT and UNIX
- Linux Security Quick Reference Guide
- Improving the security of your UNIX system
- Securing and Optimizing Red Hat Linux
- Solaris Security: Recommendations from SANS Step by Step
- Site Security Handbook - rfc1244 | rfc2196
- Guidelines for the Secure Operation of the Internet
- Solaris Operating Environment Security
- Rapid Recovery Techniques for Solaris Operating Environment
- Disaster Recovery Analysis Form
- Solaris Operating Environment Network Setting for Security
- UNIX System Security Checklist
- X Window Security
- UNIX Configuration Guidelines - from http://www.cert.org
- Tools
- John the Ripper
- John the Ripper is a password cracker, currently available for UNIX, DOS, Win32. Its primary purpose is to detect weak UNIX passwords.
- L0phtCrack
- Password Auditing and Recovery Application
- exec.c
- exec.c 1.0.4 is a kernel module which logs all the commands executed on the system. Extremely powerful stealth logging made easy! Changes: This release fixes a memory allocation problem. Please update to the current version if you use the module. This module should work on 2.2.* kernels. By Pat Szuta
- Virtual FTPD
- Virtual FTPD v6.4 is a secure FTP daemon which is derived from the OpenBSD ftp daemon and can allows virtual FTP accounts which do not have an /etc/passwd entry. For more information, here.
- Snoopy
- Snoopy is designed to log all commands executed by providing a transparent wrapper around calls to execve() via LD_PRELOAD. Logging is done via syslogd and written to authpriv, allowing secure offsite logging of activity. Changes: Integrity checking, a new method of logging, and faster logging.
- FPF
- FPF is a lkm for Linux which changes the TCP/IP stack in order to emulate other OS's TCP fingerprint. The package contains the lkm and a parser for the nmap file that let you choose directly the os you want. For more information, here.
- Imsafe
- Imsafe is a host-based intrusion detection tool for Linux which does anomaly detection at the process level and tries to detect various type of attacks. Since Imsafe doesn't know anything about specific attacks, it can detect unknown and unpublished attacks or any other form of malicious use of the monitored application. Created for Linux systems but works on almost every UNIX flavor by watching strace outputs. Screenshots available here. Warning: Still in alpha. For more information, here.
- IPtrap
- IPtrap listens to several TCP ports to simulate fake services (X11, Netbios, DNS, etc) . When a remote client connects to one of these ports, his IP address gets immediately firewalled and an alert is logged. It runs with iptables and ipchains, but any external script can also be launched. IPv6 is supported. Changes: Logging the scanned port, and no more iptables/ipchains zombies. For more information, here.
- LOMAC
- LOMAC is a security enhancement for Linux that uses Low Water-Mark Mandatory Access Control to protect the integrity of processes and data from viruses, Trojan horses, malicious remote users, and compromised root daemons. LOMAC is implemented as a loadable kernel module - no kernel recompilations or changes to existing applications are required. Although not all the planned features are currently implemented, it presently provides sufficient protection to thwart script-kiddies, and is stable enough for everyday use. Whitepaper available here. Manual available here. Changes: Added mediation of directory modification operations, improving protection. here.
- Maxty
- Maxty is a small kernel-space tty sniffer. It is a LKM which will attach to read/write syscalls and save incoming/outgoing requests to opened tty devices into separate log files. It provides a way keeping a track what is happening on virtual consoles similar to a keystroke recorder.
- John the Ripper
- Links

