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
TCL自动化之SSH交互式的更多相关文章
- 20181225-Linux Shell Bash环境下自动化创建ssh互信脚本
20181225-Linux Shell Bash环境下自动化创建ssh互信脚本 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba/ 个人网站 http: ...
- ModelSim之tcl自动化仿真
摘要: ModelSim的tcl最大的优势就在于它可以让整个仿真自动运行,免除每次进行各种用户界面控制操作的麻烦.用tcl就可以自动完成建库.映射库到物理目录.编译源代码.启动仿真器.运行仿真等一系列 ...
- SSH交互式脚本StrictHostKeyChecking选项 benchmode=yes
SSH 公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击.但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就需要一种手段能够绕过 SSH 的公钥检查. 什 ...
- ansible api 调用出现ssh交互式输入
发现在删掉 ~/.ssh/know_hosts 之后运行 ansible api 会出现以下提示 The authenticity of host '10.1.*.* (10.1.*.*)' can' ...
- Centos下 自动化配置SSH免密码登陆
hosts文件,存储要部署的节点IP地址,其中以#开头表示注释掉 192.168.101.52 192.168.101.53 192.168.101.54 192.168.101.55 192.168 ...
- TCL/Expect交互式自动化测试概要 - - ITeye技术网站
TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行"可程序化"会话的脚本语言,是一种可以提供"分支和嵌 ...
- linux - 怎么自动填写有交互的shell脚本 - SegmentFault
linux - 怎么自动填写有交互的shell脚本 - SegmentFault TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行 ...
- shell脚本 字串截取 正则表达式
字串处理 子串截取方法一:使用${}表达式格式:echo ${x:起始位置:长度}(起始位置编号从0开始,可省略) 方法二:使用expr substr格式:expr substr "$x&q ...
- 【Xamarin挖墙脚系列:Xamarin4.0的重大变更】
原文:[Xamarin挖墙脚系列:Xamarin4.0的重大变更] Windows下的变更不大,主要还是bug 的修复,性能的优化,API的扩展实现. 变化最大的是在Mac上的那个Xamarin.iO ...
随机推荐
- MYSQL学习笔记——连接以及存储过程
连接 当我们需要从多个表查询数据时,我们就需要使用到连接操作,mysql支持内连接,左连接以及右连接三种连接方式. 数据库准备 首先我们创建两个表t1, t2: create table t1(i1 ...
- maven编译问题之 -The POM for XXX is invalid, transitive dependencies (if any) will not be available
问题一: 把父工程tao-parent install 到maven本地仓后,接着install tao-common工程,然后报错 报错信息如下: [WARNING] The POM for com ...
- 状压DP常用操作
1. 判断一个数字x二进制下第i位是不是等于1. 方法:if ( ( ( 1 << ( i - 1 ) ) & x ) > 0) 将1左移i-1位,相当于制造了一个只有第i位 ...
- .net core跨平台
https://www.cnblogs.com/artech/p/7812811.html .net简介:https://baike.baidu.com/item/.NET/156737?fr=ala ...
- 树TreeView控件与DataTable交互添加节点(最高效的方法)
#region "读取树结点从Datatable" /// <summary> /// 读取树结点从Datatable" /// </summary&g ...
- (1)消灭初级程序员常用的多层if-else嵌套--------------【表驱动法】
表驱动法 1.相信很多刚从事工作的程序员或一些初级程序员在写代码的时候会出现对一些逻辑判断写成多层if-else嵌套的经历,这种方式在一些简单的层次中运用起来确实可行,但对于一些大型项目逻辑判断比较多 ...
- 冲刺CSP-S集训考试反思+其它乱写(密码私信)
RT.开坑. 10.1 开门黑23333. 放假回来稍困,而且感冒似乎愈加严重,导致我正常考试基本睁不开眼.一个小时勉强把题读懂,神志恍惚如斯. 看T2觉得估计又是各种推柿子堆定理的数学大题,写了个暴 ...
- git错误处理
1.今天 当我 执行 git add somefile 的时候,出现 如下 错误: If no other git process is currently running, this prob ...
- Vue通信、传值的多种方式,详解(都是干货)
Vue通信.传值的多种方式,详解(都是干货) 可参考博客: https://blog.csdn.net/qq_35430000/article/details/79291287
- Vagrant 手册之 box - 概述
原文地址 box 是 Vagrant 环境中使用的包格式.box 可以在 Vagrant 支持的所有平台上被任何人使用,从而提供相同的工作环境. vagrant box 工具提供了管理 box 的所有 ...