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\(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} { ;#发送时间超过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\(B\e\[m} [set ${channel}read] "" ${channel}read ;#去除linux显示的颜色乱码
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  ;#通过直接杀进程方式有点野蛮,可通过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

 

转载于:https://www.cnblogs.com/hua198/p/5693665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值