#################################################
# 在Tcl下执行系统命令时,如果只有一个单词可以写成
# set cmd "ifconfig"
# exec $cmd
# 如果命令行带参数,也就是说由多个单词构成的时候
# set cmd "ifconfig eth0"
# exec $cmd
# 这时就是报错。命令不可执行。
# 如果在Tclsh环境下,需要执行系统外部命令。则很简单。
# [ifconfig eth0] 即可。不过,写到程序里就不好用了。
#
# 为了能避免这样的情况。研究发现。可以使用 eval 命令。
#
# 在使用eval 命令时,必须加 “” (双引号)
#
# eval "exec $cmd"
# 这样就可以执行了。
#
#################################################
set cmd "ipconfig -all"
eval "exec $cmd"
#################################################
#
# Reader 使用管道的方式读取文件
#
# printProc 可打印出程序的源代码
#
#################################################
proc Reader { pipe } {
global done
if {[eof $pipe]} {
catch {close $pipe}
set done 1
return
}
gets $pipe line
puts $line
# Process the line here...
}
set pipe [open "tcltest.txt"]
fileevent $pipe readable [list Reader $pipe]
vwait done
proc printProc {procName} {
set result [list proc $procName]
set formals {}
foreach var [info args $procName] {
if {[info default $procName $var def]} {
lappend formals [list $var $def]
} else {
# Still need the list-quoting because variable
# names may properly contain spaces.
lappend formals [list $var]
}
}
puts [lappend result $formals [info body $procName]]
}
#################################################
#
# ifconfig cmd find IP address and MAC address
# with Linux system
#
#################################################
set result "eth1 Link encap:Ethernet HWaddr 00:15:17:37:49:0E inet addr:172.31.4.33 Bcast:172.31.255.255 Mask:255.255.0.0"
puts $result
set patip {(inet addr:)([^/s]+)/s+(Bcast:.*)}
regexp $patip $result a1 b1 ip
puts "ip is :$ip"
set patmac {(HWaddr )([^/s]+)/s+(inet.*)}
set retun [regexp $patmac $result aa bb mac]
puts $retun
puts "mac is : $mac"
puts "aa is : $aa"
puts "bb is : $bb"
##########################################################
#
# 提取当前时间
############################################################
set Time_now [clock format [clock seconds] -format day(%m-%d-20%y)_time(%H-%M-%S)]
puts "$Time_now "
#########################################################
# 可以起到控制时间的作用。当时间到达,自动从循环中退出
##########################################################
set i 0
set done 0
after 1000 set done 1
while {!$done} {
# A very silly example!
incr i
puts $i
# Test to see if our time-limit has been hit. This would
# also give a chance for serving network sockets and, if
# the Tk package is loaded, updating a user interface.
update
}
puts "i Total = $i"