tcl脚本读写配置文件

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自学的,可以使用,自行改良,共同进步

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值