s## tcl读写配置文件实现自动化远程交互
#!/usr/tcl/bin/expect
proc getConfig {configFile Key {Comment "#"} {Temp "["}} {
# 打开配置文件
set Value "0"
set err [catch {set fileid [open $configFile r]} errMsg]
if {$err == 1} {
puts "errMsg : $errMsg"
return $Value
}
set End "END"
set sign "1"
set rowid 0
while {[eof $fileid] != 1} {
incr rowid ;# 记录行数, 从一开始
gets $fileid line
set commentpos [string first $Comment $line]
if { $commentpos == 0 } {
} else {
if { $commentpos != -1 } {
set line [string range $line 0 [expr $commentpos-1]]
}
set line [string trim $line]
if { $line == "" } {
continue
} else {
set Keypos [string first $Key $line] ;# 得到Key的位置
set Endpos [string first $End $line] ;# 得到END的位置
set arr(0) $rowid
set sign "0"
}
if { $Endpos != -1 && $sign == "0"} {
set arr(1) $rowid
}
}
}
}
set Value "${arr(0)}_${arr(1)}"
close $fileid
return $Value
}
proc getValue {configFile Array {Temp "#"} {Equal "="}} {
set Temppos [string first "_" $Array]
set BeginPos [string range $Array 0 [expr $Temppos -1]]
set EndPos [string range $Array [expr $Temppos +1] end]
set Value "" ;# 记录过程返回的值
set End "END"
set err [catch {set fileid [open $configFile r]} errMsg]
if {$err == 1} {
puts "errMsg : $errMsg"
return $Value
}
set rowid 0
set num 0 ;#记录当前行数,程序调试时打印调试信息使用的
while {[eof $fileid] != 1} {
incr rowid ;# 记录行数, 从一开始
gets $fileid line ;# 读出一行
if { $rowid > $BeginPos && $rowid < $EndPos } {
set arr($num) $line
incr num
}
}
set num [array size arr]
for {set x 0} {$x<$num} {incr x} {
#puts $arr($x);
append Value $arr($x) "#"
}
close $fileid
return $Value
}
proc findPair {configFile {begin "["} {end "]"}} {
# 打开配置文件
set Value "0"
set err [catch {set fileid [open $configFile r]} errMsg]
if {$err == 1} {
puts "errMsg : $errMsg"
return $Value
}
set End "END"
set null ""
set sign "1"
set rowid 0
while {[eof $fileid] != 1} { ;# 读取文件内容
gets $fileid line
set commentpos [string first $configFile $line] ;# 得到注释符号的位置
if { $commentpos == 0 } {
} else {
if { $commentpos != -1 } { ;# 行中有注释符号,去掉注释
set line [string range $line 0 [expr $commentpos-1]]
}
set line [string trim $line] ;# 去掉两端的空格
if { $line == "" } {
continue
} else {
set Keypos [string first $begin $line] ;# 得到Key的位置
set Endpos [string first $end $line] ;# 得到END的位
set Value [string range $line [expr $Keypos + 1] [expr $Endpos - 1]]
if { $Value != $End && $Value != $null} {
set arr($rowid) $Value
incr rowid
}
}
}
}
append Value1 $rowid "#"
set num [array size arr]
for {set x 0} {$x<$num} {incr x} {
append Value1 $arr($x) "#"
}
close $fileid
return $Value1
}
proc start {Array {Temp "#"} {Equal "="}} { ;#这部分内容根据配置文件自行修改
set timeout 120
regexp -- {((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}} $Array ip
regexp -- {(?=username=).*?(?=#)} $Array usernameTemp
regexp -- {([^=]+)$} $usernameTemp username
regexp -- {(?=passwd=).*?(?=#)} $Array passwdTemp
regexp -- {([^=]+)$} $passwdTemp passwd
regexp -- {(?=ck_cxbhlist=).*?(?=#)} $Array ck_nameTemp
regexp -- {([^=]+)$} $ck_nameTemp ck_cxbhlist
#puts "$passwd"
#puts "$username"
#puts "$ip"
#puts "$ck_cxbhlist"
spawn ssh $username@$ip
expect {
"*yes/no" { send "yes\r" }
"*password:" { send "$passwd\r" }
}
expect "#*"
send "cd /home/webapp && ./test.sh\r"
send "ls\r"
expect eof
}
proc main { {Temp "#"} } { ;#遍历配置文件中所有的主对[111]xxx[END]
set val [findPair "test.ini"]
set Endpos [string first $Temp $val]
set num [string range $val 0 [expr $Endpos - 1]] ;#循环因子
set val [string range $val [expr $Endpos + 1] end]
for {set x 0} {$x<$num} {incr x} {
set Endpos [string first $Temp $val]
set Key [string range $val 0 [expr $Endpos - 1]]
set aaa [getConfig "test.ini" $Key]
set bbb [getValue "test.ini" $aaa]
set ccc [start $bbb]
set Endpos [string first $Temp $val]
set val [string range $val [expr $Endpos + 1] end]
}
}
set start [main]
exit
expect eof
下面配上配置文件内容
[111]
ip=122.113.2.xxx
username=webapp
passwd=12345678
ck_cxbhlist=711
sada
sadasaa=
ssssd =sss
scpdest=/home/webapp/test/
scpsrc=test.sh
[END]
[222]
ip=122.113.5.xxx
username=webapp
passwd=12345678
ck_cxbhlist=711
sada
sadasaa=
ssssd =sss
scpdest=/home/webapp/
scpsrc=test.sh
[END]
##############分割线##################
tcl自学的,可以使用,自行改良,共同进步