怎么样用tcl和putty通过ssh或者telnet访问设备

本文介绍了一种基于Tcl编程语言的库TclPutty,用于通过Putty实现SSH和Telnet连接,提供API接口进行命令执行、数据读取和匹配,简化测试开发中的设备访问过程。
摘要由CSDN通过智能技术生成

问题由来

做测试开发,经常需要通过ssh或者telnet访问设备,来获取信息或者执行命令。目前tcl没有专门的api实现这个功能,以前windows版本的expect能做到。但是近期发现expect不能用了,于是就有开发一个相关api的打算。

解决方案

写代码,越简单越好,这里采用最简单的方案,不容易出错。该方案基于tcl调用putty来实现。核心代码是下面的两行。

set file_id [open "|$program -telnet -x  -t -sanitise-stderr -sanitise-stdout $ip" r+]
fconfigure $file_id -blocking 0 -buffering none

就是通过管道打开一个进程,然后通过这个管道跟进程通讯,记住必须配置 -blocking 0 -buffering none,否则会有卡死现象。

目前我已经在github上创建了一个lib,可以拿来就用。github的地址是:
https://github.com/smshi/TclPutty
也可以在我的百度网盘下载:
链接:https://pan.baidu.com/s/133yljc4ZH4Nds_NIrW18DA?pwd=oiqh
提取码:oiqh

下面是我在github创建的使用说明。

TclPutty

TclPutty is tcl lib to connect server by ssh or telnet via putty. It has tested on windows system. I think it could work on Linux too after replace the putty binary with Linux version.

Exmaple

Telnet

package require TclPutty

set DutIp       10.0.11.123
set DutUsername admin
set DutPassword admin123
set log_file_id [open log.txt w]

#Create object named telnet_obj using telnet protocol, the log is recorded into log.txt
TclPutty telnet_obj $DutIp $DutUsername $DutPassword telnet $log_file_id

#Connect to the server
telnet_obj login

#Input command in the connection
telnet_obj exec_cmd "sys"

#Get response from the connection
set output [telnet_obj get_response]
puts $output

telnet_obj exec_cmd "display cur config"

#Get all the response until regexp match the pattern, if not match, it will timeout in 20 seconds in default.
#Following example is get the output from session until match regexp .*[HUAWEI].*
set output [telnet_obj read_until {.*\[HUAWEI\].*}]
puts $output

#Input command in the connection and get all the response until regexp match.
set output [telnet_obj exec_until "display cur config" {.*\[HUAWEI\].*}]
puts $output

#Input command in the connection and get all the response until first regexp match, 
#check if all the response match another regexp, match return 1, not match return 0
if { [telnet_obj exec_until_match "display cur config" .+ ".*interface 8.*"] } {
	puts "Interface 8 is in the running config."
} else {
	puts "Not match"
}

delete object telnet_obj

close $log_file_id

SSH

package require TclPutty

set DutIp       10.0.11.123
set DutUsername admin
set DutPassword admin123
set log_file_id [open log.txt w]

#Create object named telnet_obj using telnet protocol, the log is recorded into log.txt
TclPutty telnet_obj $DutIp $DutUsername $DutPassword ssh $log_file_id

#Connect to the server
telnet_obj login

#Input command in the connection
telnet_obj exec_cmd "sys"

#Get response from the connection
set output [telnet_obj get_response]
puts $output

telnet_obj exec_cmd "display cur config"

#Get all the response until regexp match the pattern, if not match, it will timeout in 20 seconds in default.
#Following example is get the output from session until match regexp .*[HUAWEI].*
set output [telnet_obj read_until {.*\[HUAWEI\].*}]
puts $output

#Input command in the connection and get all the response until regexp match.
set output [telnet_obj exec_until "display cur config" {.*\[HUAWEI\].*}]
puts $output

#Input command in the connection and get all the response until first regexp match, 
#check if all the response match another regexp, match return 1, not match return 0
if { [telnet_obj exec_until_match "display cur config" .+ ".*interface 8.*"] } {
	puts "Interface 8 is in the running config."
} else {
	puts "Not match"
}

delete object telnet_obj

close $log_file_id

API Induction

Constructor

constructor { mngip usr pwd {prtcl telnet} {fileid ""}}

You can construct object like this, default using telnet protocol, and the log is only output to the stdout:

TclPutty objectname 192.168.1.1 root mypass

You can construct object like this too, using ssh protocol, and the log is output to the stdout and the file specified:

TclPutty objectname 192.168.1.1 root mypass ssh fileid

Object attributes

Attribute Default Induction
usrprompt Username: When login by telnet, when the putty output is usrprompt, it will input username automatically.
pwdprompt Password: When login by telnet, when the putty output is pwdprompt, it will input password automatically.
login_flag #|> It is a regexpr pattern. When the output is matched with the pattern, it will think login successfully.
command_end \r The paramter will be appended to command when exec_cmd is called.
reponse_delay 100 The interval to read data from the putty proccess channel
timeout 20000 When the api with pattern parameter is called, if not match, it will wait until the timeout is reached.

We can use the basic itcl grammar to change attribute into what you want.
For example:

TclPutty objectname 192.168.1.1 root mypass
#This change the login_flag into >
objectname configure -login_flag >

Member functions

login

After construct object, you should login.

objectname login

exec_cmd cmd

It will input cmd to the putty channel.
Following example is to input dir command to the putty channel.

objectname exec_cmd "dir"

read_until { {expr_pat .+} {delay default}}

It will read the data from the putty channel until match expr_pat. In default, it will match any character.
Usage examples.

objectname read_until ".*localhost.*" --------- read all the data from putty channel until localhost appears
objectname read_until --------------------------read all the data from putty channel in condition that there is response data unread
objectname read_until ".*localhost.*" 30000 ----read all the data from putty channel until localhost appears, but the timeout is changed into 30000 milliseconds.

exec_until {cmd {expr_pat .+} }

It is combination of exec_cmd and read_until.
Usage exmaple:

objectname exec_until "show running config" ".*localhost.*"
objectname exec_until "show running config"

exec_until_match {cmd {expr_pat .+} {match .*} }

It is combination of exec_cmd and read_until. Additionally, it will match the return data to check if it is matched with regexp in match variable.
Usage exmaple:

objectname exec_until_match "show running config" ".+" ".*interface 8.*"

upload_via_scp filename

It will upload the local file into the remote server by ssh.
Usage exmaple:

objectname upload_via_scp filename
  • 21
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
随着Linux在服务器端应用的普及,Linux系统管理越来越依赖于远程。在各种远程登录工具中,Putty是   出色的工具之一。   Putty是一个免费的、Windows 32平台下的telnet、rlogin和ssh客户端,但是功能丝毫不逊色于商业的   telnet类工具。用它来远程管理Linux十分好用,其主要优点如下:   ◆ 完全免费;   ◆ 在Windows 9x/NT/2000下运行的都非常好;   ◆ 全面支持ssh1和ssh2;   ◆ 绿色软件,无需安装,下载后在桌面建个快捷方式即可使用;   ◆ 体积很小,仅364KB (0.54 beta版本);   ◆ 操作简单,所有的操作都在一个控制面板中实现。   使用简介:   把Putty下载到机器上,双击putty.exe,就出现如图1的配置界面。选择“Session”,在“Host Name   (or IP address)”输入框中输入欲访问的主机名或IP,比如server1或192.168.9.4。端口号(Port)根   据使用的协议有所区别,ssh默认使用22,telnet默认使用23,rlogin默认使用513。在“Protocol”单   选栏中选择使用的协议,一般是telnetssh,这取决于服务器提供的服务。在“Saved Session”输入   栏中输入任务的名字,单击“Save”按钮,就可以把任务配置保存起来了。   配置完成后单击“Open”按钮,出现如图2的登录界面,就可以使用Putty连接Linux主机了。   关于putty的一些使用中常见的问题   一、在Windows中使用putty登录FreeBsd系统时,能够输入用户名,但是输入密码后按回车键半天无反应   。   答:设置过程如下:   1. 在/etc/ssh/sshd_config中添加如下内容,使普通用户可以通过SSH登录:   AllowUsers 普通用户帐号   UseDNS no   2. 将希望使用su命令的用户加入到wheel组中,在/etc/group中作如下修改   wheel:*:0:root,普通用户帐号   二、在login as: 后输入用户名,J04222035@cs8.xmu.edu.cn's password: 光标不动。即使输入正确的   密码也是没有反应?   答:这是因为putty不像telent那样会直接显示密码,而是不显示密码,只要你输入的密码是正确的,   即使不显示只要输完密码后直接回车即可进入,不用在乎光标不动。   当输入密码错误敲下回车时会出现 denice 以提示用户密码错误
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值