plink

JM 10/13/2018 - plink : a command-line interface to the PuTTY back ends

I found the following code for another Poor Man's Expect for windows which runs by driving plink.exe:
from:
https://www.cnblogs.com/hua198/p/5693665.html

  
  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
    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\(B\e\[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 "\r"
  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} {
      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\(B\e\[m} [set ${channel}read] "" ${channel}read
  regsub {.*?\n} [set ${channel}read] "" ${channel}read ;#???????
  if {[llength $args]<1 && [regexp -all {(.*?)(\n)} [set ${channel}read]] >0 } {
    set i 1
    set c ""
    set a [split [set ${channel}read] "\n"]
    foreach x $a {
      if {$i<[llength $a]} {
        incr i
        append c $x "\n"
      }
    }
    set ${channel}read [string trimright $c "\n"]
    
  }
  # puts [set ${channel}read ]
  return [set ${channel}read]
  }
  
  
  proc tclclose {channel} {
  fileevent $channel readable {}
  if [catch {
    exec taskkill /F /IM plink.exe
    #close $channel
  } errmsg ] {
    puts "close plink error : $errmsg"
    return false;
  }
  
  return true;
  }
  
  set ch [connect ssh 192.168.251.10 root 123]
  puts [sendCommand $ch "ls -ll"]
  tclclose $ch