目前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

TCL自动化之SSH交互式的更多相关文章

  1. 20181225-Linux Shell Bash环境下自动化创建ssh互信脚本

    20181225-Linux Shell Bash环境下自动化创建ssh互信脚本 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba/ 个人网站 http: ...

  2. ModelSim之tcl自动化仿真

    摘要: ModelSim的tcl最大的优势就在于它可以让整个仿真自动运行,免除每次进行各种用户界面控制操作的麻烦.用tcl就可以自动完成建库.映射库到物理目录.编译源代码.启动仿真器.运行仿真等一系列 ...

  3. SSH交互式脚本StrictHostKeyChecking选项 benchmode=yes

    SSH 公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击.但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就需要一种手段能够绕过 SSH 的公钥检查. 什 ...

  4. ansible api 调用出现ssh交互式输入

    发现在删掉 ~/.ssh/know_hosts 之后运行 ansible api 会出现以下提示 The authenticity of host '10.1.*.* (10.1.*.*)' can' ...

  5. Centos下 自动化配置SSH免密码登陆

    hosts文件,存储要部署的节点IP地址,其中以#开头表示注释掉 192.168.101.52 192.168.101.53 192.168.101.54 192.168.101.55 192.168 ...

  6. TCL/Expect交互式自动化测试概要 - - ITeye技术网站

    TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行"可程序化"会话的脚本语言,是一种可以提供"分支和嵌 ...

  7. linux - 怎么自动填写有交互的shell脚本 - SegmentFault

    linux - 怎么自动填写有交互的shell脚本 - SegmentFault TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行 ...

  8. shell脚本 字串截取 正则表达式

    字串处理 子串截取方法一:使用${}表达式格式:echo ${x:起始位置:长度}(起始位置编号从0开始,可省略) 方法二:使用expr substr格式:expr substr "$x&q ...

  9. 【Xamarin挖墙脚系列:Xamarin4.0的重大变更】

    原文:[Xamarin挖墙脚系列:Xamarin4.0的重大变更] Windows下的变更不大,主要还是bug 的修复,性能的优化,API的扩展实现. 变化最大的是在Mac上的那个Xamarin.iO ...

随机推荐

  1. C# 获取系统环境数据

    using System; using System.Data; using System.Text.RegularExpressions; using System.Threading; names ...

  2. iBatis.Net 语句 获取Identity ID

    <insert id="Table_Operate_Id" parameterClass="Entity" resultClass="Syste ...

  3. hdu 4082 Hou Yi's secret(暴力枚举)

    Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 修改图片尺寸网站https://www.yasuotu.com/

    修改图片尺寸网站https://www.yasuotu.com/

  5. Java第二阶段之常用类

    包装类缓存-128到127之间的数字(系统初始的时候就创建了,当我们调用ValueOf时,首先检查是否在范围内,在则直接取用)integer in1 = integer.valueOf(-128):i ...

  6. yield与gen.coroutine

    def d(): for i in range(2): yield i def b(): yield d() print("b") yield "bb" def ...

  7. vue项目中router路由配置

    介绍 路由:控制组件之间的跳转,不会实现请求.不用页面刷新,直接跳转-切换组件>>> 安装 本地环境安装路由插件vue-router:    cnpm install vue-rou ...

  8. 演示Git使用

    对于新建的repository,第一次提交,完整过程: 13643@DESKTOP-K6CS6SE MINGW64 ~/Desktop/555 $ ls Readme.md run.py 13643@ ...

  9. sts创建maven项目 引入spring,报错

    症状: Missing artifact org.springframework:spring-core:jar:5.0.0.RC3 原因: 在引入之前没有设置spring版本号 和spring ur ...

  10. Arithmetic Sequence

    Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...