홈페이지 : http://tclpcap.sourceforge.net/
tclpcap은 Pcap 패킷 캡처 라이브러리를 바인딩한 Tcl 확장(extension)입니다. 이 확장을 사용하면 프로그래머가 Tcl 스크립트에서 패킷을 캡처하고 처리할 수 있습니다. 패킷은 네트워크 인터페이스에서 직접 캡처할 수도 있고, tcpdump로부터 만들어진 캡처 파일에서 읽어올 수도 있습니다. 이 확장은 덤프 파일을 생성할 수 있기 때문에, 캡처 파일을 처리하는 다양한 도구를 Tcl만으로 순수하게 작성할 수 있습니다. 참고로, 이 확장은 디코딩 기능은 제공하지 않습니다. 단순히 네트워크나 캡처 파일에서 원시 데이터를 제공할 뿐입니다. 현재 이 패키지는 Linux에서만 테스트되었습니다. 약간의 이식 작업만 하면 대부분의 Unix 시스템에서도 동작할 것입니다. man 페이지의 html 버전은 여기에서 확인할 수 있습니다.
tclpcap is a Tcl extension that provides access to the Pcap packet capture library. This extension allows a programmer to capture and process packets from a Tcl script. Packets can be either captured from a network interface or read from a capture file from tcpdump. This extension allows dump files to be created so interesting tools to process capture files can be written purely with Tcl. Note that this extension provides no decoding capabilities. It simply provides the raw data from the network or capture file. At this time, this package has only been tested under Linux. It should work for any Unix system with only minor porting effort. An html version of the man page can be found here.
#!/usr/local/tcl8.4/bin/tclsh8.4
#
# watchHttp.tcl
# Copyright (c) 2005, Todd J Martin
#
# This is an example of how to capture and examine http packets
set dirname [file dirname [info script]]
set dirname [file join $dirname ..]
lappend auto_path $dirname
package require Pcap
puts "Pcap [package present Pcap]"
proc rawDump {str} {
set out ""
for {set cnt 0} {$cnt < [string length $str]} {incr cnt} {
binary scan [string index $str $cnt] c dec
set dec [expr ( $dec + 0x100 ) % 0x100]
append out [format "%x " $dec]
}
puts $out
}
proc getPacket {p} {
set packet [pcap::getPacket $p]
set data [lindex $packet 1]
set tcpPacket [string range $data 34 end]
binary scan [string index $tcpPacket 12] h2 tcpHeadLen
set tcpHeadLen "0x$tcpHeadLen"
set tcpHeadLen [expr {$tcpHeadLen * 4}]
if {$tcpHeadLen >= [string length $tcpPacket]} {
return
}
set tcpData [string range $tcpPacket $tcpHeadLen end]
if {![binary scan $tcpData A* httpText] || ![string length $httpText]} {
return
}
set httpCode [string range $httpText 0 [expr [string first " " $httpText] - 1]]
switch -glob -- $httpCode {
"GET" {
set i1 [expr [string first " " $httpText] + 1]
set i2 [expr [string first " " $httpText $i1] - 1]
set url [string range $httpText $i1 $i2]
puts "$httpCode $url"
}
"HTTP*" {
set i1 [expr [string first " " $httpText] + 1]
set i2 [expr [string first "\r" $httpText $i1] - 1]
set responseCode [string range $httpText $i1 $i2]
puts "$httpCode $responseCode"
}
default {
}
}
}
set p [pcap::pcap_open -filter "ip proto \\tcp and port 80" [pcap::lookupdev]]
if {[lindex [pcap::datalink $p] 1] != "Ethernet"} {
puts "Don't understand this datalink type"
close $p
exit
}
fconfigure $p -encoding binary -translation binary
fileevent $p readable "getPacket $p"
vwait forever
'Tcl & Tk > 확장 패키지 (Extension Package)' 카테고리의 다른 글
Swank 2.1.3 (0) | 2025.09.01 |
---|---|
hash 0.2a1 (0) | 2025.09.01 |
tclTelnet 2.0.1 (0) | 2025.09.01 |
TODL 0.1 (0) | 2025.09.01 |
Sugar 0.1 (0) | 2025.08.27 |