zoukankan      html  css  js  c++  java
  • TCL自动化之SSH交互式

    目前ssh工具很多,但是能够轻松运用到自动化脚本中,可以轻松适配任何环境,满足ssh交互式登录的tcl工具包很少

    下面是个人在tcl自动化过程中比较满意的一款自动化脚本

    通过使用管道方式分装plink.exe实现ssh命令交互

    plink.exe通过命令行方式登录linux服务器,tcl通过cmd方式调用plink

    plink.exe放在脚本当前目录

    ;#by enter

    #!/bin/sh
    # plink.tcl

    proc waitTime {time} {
    after $time {set a 0 }
    vwait a
    }

    proc openPipe {pipestr} {
    global exePath
    if {[info exists exePath]==0 } {
    set exePath "."
    }
    set currentPath [pwd]
    cd $exePath
    set channel [open "|plink.exe $pipestr" r+]
    cd $currentPath
    fconfigure $channel -block 0 -buffering none -buffersize 1 -encoding utf-8
    fileevent $channel readable [list getEcho $channel]
    return $channel
    }


    proc getEcho {channel} {
    variable ${channel}read
    if [catch {
    set s [read $channel]
    #puts -nonewline $s ;#输出到console
    append ${channel}read $s ;#保存到内存
    } errmsg ] {
    puts stderr "getEcho error := $errmsg"
    }
    }

    proc connect { protocol remoteip username password} {
    set url "-$protocol -l $username -pw $password $remoteip "
    set channel [openPipe $url]

    variable ${channel}read
    set ${channel}read ""
    set timeout 0
    while {1} {
    if {[regexp "#|>" [set ${channel}read]]>0} {
    break
    } elseif {$timeout > 12000} {
    puts stderr "connect $url : timeout "
    break
    }
    waitTime 100
    incr timeout 100

    }
    regsub -nocase -all {e[?d{1,4}h|e[d{1,2}(;d{1,2}){0,2}m|e(Be[m} [set ${channel}read] "" ${channel}read
    puts [set ${channel}read ]
    return $channel
    }


    proc sendCommand {channel str args} {
    variable ${channel}read
    puts -nonewline $channel $str
    puts -nonewline $channel " "
    set ${channel}read ""
    set readflush ""
    set timeout 0
    waitTime 100
    if {[llength $args]>0} {
    set m "$args|#|>"

    } else {
    set m "#|>"
    }
    while {1} {
    if {[regexp $m [set ${channel}read]] > 0} {
    break;
    } elseif {$timeout>10000} {
    if {[expr $timeout%3000]==0} { ;#发送时间超过10s,且3s内容无变化,退出循环
    if {$readflush==[set ${channel}read]} {
    puts "execute command : $str timeout"
    break
    }
    set readflush [set ${channel}read]
    }
    }
    waitTime 100
    incr timeout 100
    }
    #regsub -nocase -all "~#" [set ${channel}read] "->" ${channel}read ;#将~#输出变成->
    regsub -nocase -all {e[?d{1,4}h|e[d{1,2}(;d{1,2}){0,2}m|e(Be[m} [set ${channel}read] "" ${channel}read ;#去除linux显示的颜色乱码
    regsub {.*? } [set ${channel}read] "" ${channel}read ;#去掉回显第一行
    if {[llength $args]<1 && [regexp -all {(.*?)( )} [set ${channel}read]] >0 } { ;#去掉回显最后一行
    set i 1
    set c ""
    set a [split [set ${channel}read] " "]
    foreach x $a {
    if {$i<[llength $a]} {
    incr i
    append c $x " "
    }
    }
    set ${channel}read [string trimright $c " "]

    }
    # puts [set ${channel}read ] ;#打印命令回显
    return [set ${channel}read] ;#返回命令回显
    }


    proc tclclose {channel} {
    fileevent $channel readable {}
    if [catch {
    exec taskkill /F /IM plink.exe  ;#通过直接杀进程方式有点野蛮,可通过pid方式进行结束
    #close $channel
    } errmsg ] {
    puts "close plink error : $errmsg"
    return false;
    }

    return ture;
    }

    set ch [connect ssh 192.168.251.10 root 123]
    puts [sendCommand $ch "ls -ll"]
    tclclose $ch

  • 相关阅读:
    Java多线程学习(六)Lock锁的使用
    Java多线程学习(五)线程间通信知识点补充
    Java多线程学习(四)等待/通知(wait/notify)机制
    Java多线程学习(四)等待/通知(wait/notify)机制
    Java多线程学习(三)volatile关键字
    SLAM领域资源链接
    追踪tracking
    orbslam算法框架
    Covisibility Graph
    优化
  • 原文地址:https://www.cnblogs.com/hua198/p/5693665.html
Copyright © 2011-2022 走看看