zoukankan      html  css  js  c++  java
  • tera term通过ttl脚本 自动连接服务器

      在现在的这个公司一直使用tera term来远程连接服务器,感觉很方便,特别是它的ttl脚本配置的自动连接。有时候我们可能无法直接连接到目标服务器,需要通过ssh经过多个中间服务器才能连接到目标服务器。如果此时让我们在终端下一行一行命令的敲,实在是很烦的一件事情,我遇到过的最多的是中间经过三次服务器的ssh才连接到了目标服务器。虽然都是Ctrl+c 、Ctrl+v 。但是每天一上班就开好几个终端每次到这么一个一个敲很麻烦,而tera term的ttl脚本就帮助我们解决了这个问题。

      Tera Term

      这是一个类似于putty这样的一个远程连接的终端软件,通常就是那些ssh、ftp、sftp、scp的协议使用的浏览linux系统的远程连接软件(本人自己的体会,哈哈、、、)

      这是一个日本人开发的软甲,小鬼子做的还是很不错的。网上找了找资料发现资料比较少,那共享一下我的理解。也用过一段时间的putty,但是没有发现putty有类似的功能。每次都要输入用户名,密码,然后ssh命令,然后又是密码,真是很烦!tera term的ttl脚本就帮助我们解决了问题

      相关资料:

        tera term 英文官网:http://www.teraterm.net/manual/en/

        ttl 命令手册:http://www.teraterm.net/manual/en/macro/command/index.html

      这里在贴一片博文(http://my.oschina.net/u/183789/blog/37469)

        

    介绍几个常用的命令

    a. 通过Tera Term连接PFC所在的机器以及断开连接

    通过TTL的connect/disconnect命令实现

    Connect:

    connect '192.168.137.27 /ssh /auth=password /user=username /passwd= passwd'

    注:其中username是用户名,passwd是密码

    Disconnect:

      

    注:disconnect后面指定参数,这样就不弹出确认框了。

    b.    通过log记录全程操作过程,最终通过比对log确认测试结果。

    通过logopen/logclose命令来写log 

    logopen:

    logopen filename 0 1 1

    注:filename最好设成命令名,不通ttl脚本不要重复。也可以带执行的时间,

    例如:Show_candidate_config_ 20110426-130939.log

        方法如下:

    gettime timestr "%Y%m%d-%H%M%S"

    getdir mdir

    logclose :

               logclose

        

    c.     测试项作成时可能用到的命令

    Pause:(暂停)

     pause <time>Remarks:Pauses for <time> seconds. 

    Sendln:(发送命令并换行)

    sendln <data1> <data2>....

    Remarks:Causes Tera Term to send characters followed by a new-line character to the host.             

    Send:(发送命令)

    send <data1> <data2>....

    Remarks:Causes Tera Term to send characters to the host.

    If <data> is a string, the string is sent to the host. 

    If <data> is an integer, its lowest-order byte (0-255) is regarded as an ASCII code of the character, and the character is sent to the host.        例如:按Tab键的命令是send  #9           

    wait:(等待匹配的字符串出现)  

    wait <string1> [<string2> ...]

    Remarks:Pauses until one of the character strings is received from the host, or until the timeout occurs. Maximum number of the strings is 10. 

         注:使用这个命令是需要设置timeout时间,命令的返回结果保存在resault变量中,当resault为时,则为超时。Timeout 设置命令如下:

              timeout=1  /*等号后面的值为整数,设为负则是无限等待*

    waitln:(等待整行匹配的字符串出现)         

    waitln <string1> [<string2> ...]

    Remarks:Pauses until a line which contains one of the character strings is received from the host, or until the timeout occurs. Maximum number of the strings is 10.     

    注意点同上     其他命令例如if,then,elseif,else,endif,goto等请参考help文件。 

    脚本作成的注意点

    a.    不同的ttl脚本内指定的Log文件名不能重复。

    b.    脚本的最后部分请清空测试环境,以便下一个ttl脚本执行。

    c.     在执行比较缓慢的地方,例如连接机器时,请追加pause命令

    d.    脚本尽量写得简洁短小,以便式样发生变更时易于更改。

    e.     必要的时候可以追加注释

    使用例子:

    1,在window下创建bat文件,

    "C:Program Files eraterm tpmacro.exe" "D:My KownhowTTL est.ttl"
    exit

    2,生产ttl脚本文件test.ttl

    ;###connect host
    connect '172.28.92.23 /ssh /auth=password /user=root /passwd=password'

    pause  1

    ;###create log

    gettime logstr "log-%Y%m%d-%H%M%S.txt"
    getdir curdir
    sprintf '%s\%s' curdir logstr
    filename = inputstr
    logopen filename 0 1 1
    logwrite 'Log start'#13#10

    looptimes = 1 ; 
    while looptimes < 11
     ;###run cmd 

     sendln "ls -l"
     wait "#"
    looptimes = looptimes + 1 
    endwhile

    ;###closelog
    Logclose

    ;###disconnect
    disconnect 
    closett

    执行bat文件就可以运行ttl脚本了.

      

      然后来看看我的ttl配置文件

    ;++++++++++++++++++++++++++++++++++++++++++++
    ; tera term 连接脚本
    ; 下面的是定义变量的方式
    ; 这里我们设置 主机名Host
    ;              用户user1
    ;              密码password1
    ;++++++++++++++++++++++++++++++++++++++++++++
    Host = '192.***.***.***'
    User1 = '*******'
    Password1 = '******'
    Ini = 'TERATERM.INI'
    Prompt1 = '$'
    PromptPW = 'suDyzVs83y'
    Df = 'df'
    
    ; 设置一个字符Cmd为一个空字符串
    ; strconcat 是拼接字符串,我们可以看到下面进行了一系列的拼接,
    ; Cmd 最终的效果像这样:'192.***.***.*** /ssh /auth=password /user=username /passwd= passwd'
    Cmd = ''
    strconcat Cmd Host
    strconcat Cmd ' /ssh /1 /auth=password'
    strconcat Cmd ' /user='
    strconcat Cmd User1
    strconcat Cmd ' /passwd='
    strconcat Cmd Password1
    strconcat Cmd ' /f='
    strconcat Cmd Ini
    
    ; connect 连接命令 (图 1)
    ; 也就是: connect '192.***.***.*** /ssh /auth=password /user=username /passwd= passwd'
    connect Cmd
    
    ; wait (等待匹配的字符串出现)  图 2
    ; sendln (发送一行字符后换行)
    ; 等待bash出现的$符号
    wait '$'
    ; 通过ssh连接
    sendln 'ssh wangxb@***.***.***.***'
    ; 这里是等待要求输入密码的 ':' 
    wait ':'
    ; 输入密码
    sendln '****'
    ; 登录成功切换到相应目录 图 3
    wait '$'
    sendln 'cd /home/***'
    wait '$'
    ; 这里还是通过ssh连接到另外一台服务器,之所以上面要切换目录是因为这个目录项目有我们ssh连接时不使用密码登录要求的 私钥
    ; 如果有兴趣可以了解一下ssh 这个协议命令
    sendln 'ssh -i ***_private.pem homepage@210.***.***.***'
    
    ; 下面的这几条语句也是等待匹配 ':' 来输入密码的
    wait ':'
    ; 这里的密码是空的 图 4
    sendln ''
    wait ':'
    sendln '*****'
    
    ; 切换root用户 图 5
    wait '$'
    sendln 'su -'
    wait ':'
    sendln '*****'
    
    ; 这里之所以等待 '#' 是因为我们上面切换到了root ,所以匹配 #
    wait '#'
    ; 切换目录 图 6
    sendln 'cd /var/www/www.****.jp.net'
    
    ;++++++++++++++   总结  ++++++++++++++++++++++
    ; 当我们配置自动连接的 ttl脚本时,我们很难一次
    ; 弄正确,没关系,我们可以根据我们的实际情况,
    ; 首先连接到第一台服务器,然后其实很简单,只是
    ; 在我们平时需要切换到目标服务器的命令在这里用
    ; wait 和sendln 匹配就可以了,
    ; 我们不一定都是 wait '$' 这样根据我们到了这一
    ; 步,系统显示的是什么,是':' 那就 wait ':',
    ; 是 '#' 那就wait '#' 然后写上我们在命令行的
    ; 命令通过sendln,
    ; 当我们完成一部分ttl编写时,我们可以去试着连
    ; 接一下,看看是哪里出了问题,然后再来修改,这
    ; 样,一步一步就出来了
    ;++++++++++++++++++++++++++++++++++++++++++++
    restoresetup Ini

      我们平时可以用的的基本也就是这些简单的语法,更加复杂的大家可以去官网学习,下面贴出上面脚本执行的时序图,自行对应啊。

    图 1

    图 2

    图 3

    图 4

    图 5

    图 6

      好吧,就简单这么做个记录,希望可以帮到别人!如果有什么不清楚的地方,欢迎提问,欢迎转载。

     

  • 相关阅读:
    少走弯路的10条忠告
    思考
    哈弗经典校训
    项目导出excel引发的一些问题
    hibernate 缓存设置
    dubbo简单用法
    sql 类型问题
    spring this.logger.isDebugEnabled()
    红黑树
    归并排序
  • 原文地址:https://www.cnblogs.com/wxb0328/p/teraterm.html
Copyright © 2011-2022 走看看