[제15장] Tcl ; 도구 명령 언어
=================================================================
* Subject : [제15장] Tcl ; 도구 명령 언어
* Writer: w0rm9 (research.hackerschool.org)
* Date: 2004/02/23
=================================================================
/* 스터디에 사용했던 예제들을 중심으로 정리했습니다.
* 좀더 자세한 내용이나 명령어들은 챕터15를 참고하시길- -;
* 그냥 이런 언어도 있구나 정도의 정리입니다.
*/
0x01. Tcl의 개요
Tcl은 인터프리터 방식의 언어이다.
[w0rm9@work TCL]$ cat test1.tcl
#!/usr/bin/tclsh
set str "Wise guys"
puts $str
[w0rm9@work TCL]$ chmod u+x test1.tcl
[w0rm9@work TCL]$ ./test1.tcl
Wise guys
tclsh을 실해하면 Tcl 명령을 실행할 수 있다.
[w0rm9@work TCL]$ tclsh
% set str "Wise guys"
Wise guys
% put $str
Wise guys
% exit
0x02. 제어 구조
■ if
[w0rm9@work TCL]$ cat test2.tcl
#!/usr/bin/tclsh
set str "wise guys"
if {$str == "wise guys"} {
puts "oh yes~~"
} else {
puts "oh no~~"
}
[w0rm9@work TCL]$ chmod u+x test2.tcl
[w0rm9@work TCL]$ ./test2.tcl
oh yes~~
■ switch
[w0rm9@work TCL]$ cat test3.tcl
#!/usr/bin/tclsh
set str "wise guys"
switch $str {
"wise guys" {puts "oh yes~~"}
default {puts "oh no~~"}
}
[w0rm9@work TCL]$ chmod u+x test3.tcl
[w0rm9@work TCL]$ ./test3.tcl
oh yes~~
■ for
[w0rm9@work TCL]$ cat test4.tcl
#!/usr/bin/tclsh
for {set i 0} {$i<10} {incr i 1} {
puts $i
}
[w0rm9@work TCL]$ chmod u+x test4.tcl
[w0rm9@work TCL]$ ./test4.tcl
0
1
2
3
4
5
6
7
8
9
■ while
[w0rm9@work TCL]$ cat test5.tcl
#!/usr/bin/tclsh
while {1} {
puts "wiseguys forever"
}
[w0rm9@work TCL]$ chmod u+x test5.tcl
[w0rm9@work TCL]$ ./test5.tcl
wiseguys forever
wiseguys forever
wiseguys forever
wiseguys forever
wiseguys forever
wiseguys forever
wiseguys forever
0x03. 스트링 연산
[w0rm9@work TCL]$ cat test6.tcl
#!/usr/bin/tclsh
set i [string compare "HI" "HH"] // "HI"와 "HH"를 비교하여 같은면 0, 다르면 1
puts $i
[w0rm9@work TCL]$ chmod u+x test6.tcl
[w0rm9@work TCL]$ ./test6.tcl
1
[w0rm9@work TCL]$ cat test7.tcl
#!/usr/bin/tclsh
puts [string first "abc" "123abc546"] // "123abc456"에서 "abc"를 찾고, 일치하는 내용의 시작부분을 반환. last는 그 반대
[w0rm9@work TCL]$ chmod u+x test7.tcl
[w0rm9@work TCL]$ ./test7.tcl
3
[w0rm9@work TCL]$ cat test8.tcl
#!/usr/bin/tclsh
puts [string index "mirable" 3] // "mirable"에서 3(0부터 시작)째 문자를 반환
[w0rm9@work TCL]$ chmod u+x test8.tcl
[w0rm9@work TCL]$ ./test8.tcl
a
[w0rm9@work TCL]$ cat test9.tcl
#!/usr/bin/tclsh
puts [string toupper "sFnddFIEN"] // toupper은 대문자로, tolower은 소문자로
[w0rm9@work TCL]$ chmod u+x test9.tcl
[w0rm9@work TCL]$ ./test9.tcl
SFNDDFIEN
[w0rm9@work TCL]$ cat test10.tcl
#!/usr/bin/tclsh
set a "wise guys "
set b "forever~ hehe"
puts [append a $b] // a에 b를 추가함
[w0rm9@work TCL]$ chmod u+x test10.tcl
[w0rm9@work TCL]$ ./test10.tcl
wise guys forever~ hehe
0x03. 배열
[w0rm9@work TCL]$ cat test11.tcl
#!/usr/bin/tclsh
set str(0) "wiseguys"
set str(babo) "w0rm9"
set str(gosu) "mongii"
puts [array exists str] // str이 배열이면 1, 아니면 0 반환
puts [array size str] // 배열의 크기 반환
puts [array names str] // 배열의 이름 목록 반환
puts [array get str] // 배열색인과 요소의 쌍을 목록으로 반환
[w0rm9@work TCL]$ chmod u+x test11.tcl
[w0rm9@work TCL]$ ./test11.tcl
1
3
babo 0 gosu
babo w0rm9 0 wiseguys gosu mongii
0x04. 프로시저
[w0rm9@work TCL]$ cat test12.tcl
#!/usr/bin/tclsh
proc my_func {str} { // 반드시 proc를 사용하여 정의
set a "wise guys"
append a $str
puts $a
return 777
}
puts [my_func " forever"]
[w0rm9@work TCL]$ chmod u+x test12.tcl
[w0rm9@work TCL]$ ./test12.tcl
wise guys forever
777
0x05. 입력과 출력
[w0rm9@work TCL]$ cat test13.tcl
#!/usr/bin/tclsh
set fd [open "/etc/passwd" r] // 파일이나 명명 파이프 라인에 대한 접속을 설정
set str [read $fd] // $fd의 모든 문자를 읽어들임
puts $str
close $fd
[w0rm9@work TCL]$ chmod u+x test13.tcl
[w0rm9@work TCL]$ ./test13.tcl
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
rpm:x:37:37::/var/lib/rpm:/bin/bash
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/html/usage:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
wiseguys:x:500:500::/home/wiseguys:/bin/bash
wantstar:x:501:501::/home/wantstar:/bin/bash
y2family:x:503:503:Ju Yeong Yang,Ajou Univ.:/home/y2family:/bin/bash
naska21:x:507:507::/home/naska21:/bin/bash
prosager:x:508:508::/home/prosager:/bin/bash
mirr:x:509:509::/home/mirr:/bin/bash
sjh21a:x:511:511::/home/sjh21a:/bin/bash
mysql:x:512:512::/home/mysql:/bin/bash
bnc:x:514:514::/home/bnc:/bin/bash
maniabox:x:515:515::/home/maniabox:/bin/bash
smallcity:x:516:516::/home/smallcity:/bin/bash
hkpco:x:517:517::/home/hkpco:/bin/bash
muzen2540:x:518:518::/home/muzen2540:/bin/bash
w0rm9:x:519:519::/home/w0rm9:/bin/bash
quiz:x:520:520::/home/quiz:/bin/bash
new_maniabox:x:521:521::/home/new_maniabox:/bin/bash
0x06. 네트워크 지원
[w0rm9@work TCL]$ cat test15.tcl
#!/usr/bin/tclsh
set sock_fd [socket hackerschool.org 25] // hackerschool.org 의 25번 포트에 대한 SOCK_STREAM 접속을 연다.
gets $sock_fd banner
puts $banner
[w0rm9@work TCL]$ chmod u+x test15.tcl
[w0rm9@work TCL]$ ./test15.tcl
220 localhost.localdomain ESMTP Sendmail 8.12.8/8.11.6; Tue, 24 Feb 2004 20:46:17 +0900
_eof_
