commonlib.tcl

来自「tcl例子脚本」· TCL 代码 · 共 109 行

TCL
109
字号
#************************************************ 
# get a line from file, skip blank lines and
# comment lines, return the reading line in
# parameter 'line'.
#
# @PARAMS
# fd     - file fd
# line   - var used to return the line
# 
# @RETURN
# return 1 if read successfully, otherwise 0
#************************************************ 
proc getLine {fd line} {
    upvar $line ln

    # read a line from fd
    while {[set lineLen [gets $fd ln]] >= 0} {
        # blank line
        if { $lineLen == 0 } continue
 
        # trim whitespace
        set ln [string trim $ln]
        if { [string length $ln] == 0 } continue

        # skip comment
        if { [string index $ln 0] == "#" } continue

        # success
        return 1
    }

    return 0
}


#************************************************ 
# debug output routine
#
# @PARAMS
# arg - variable length arguments
#************************************************ 
proc dbgLog arg {
    global g_dbgFlag

    if {$g_dbgFlag} {
        puts $arg
    }
}

#************************************************ 
# error output routine
#
# @PARAMS
# arg - variable length arguments
#************************************************ 
proc errLog arg {
    global g_bLogFd
    global g_dbgFlag

    if {$g_dbgFlag} {
        puts $arg
    }

    if { $g_bLogFd != 0 } {
        puts $g_bLogFd $arg
    }
}

#************************************************ 
# telnet login routine
#
# @PARAMS
# ipaddr - remote device ip address
# user   - user name to login in
# passwd - login password
# 
# @RETURN
# spawn_id if login success, otherwise 0
#************************************************ 
proc login {ipaddr user passwd} {
    global g_prompt g_usrPrompt g_pwdPrompt prompt

    spawn telnet $ipaddr
    expect {
        "$g_usrPrompt" {
            exp_send "$user\r"
            exp_continue
        }
        "$g_pwdPrompt" {
            exp_send "$passwd\r"
            exp_continue
        }
        "$g_prompt" {
            #dbgLog "Login Successful\n"
            exp_send "co\r"
            exp_continue
        }
        "$prompt" {
            dbgLog "登陆成功,进入配置模式\n"
            return $spawn_id
        }
        timeout {
            send_user "timeout\n"
            return 0
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?