Softpanorama
May the source be with you, but remember the KISS principle ;-)

Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

TeraTerm Macros

News Teraterm Recommended Links Reference

Connect command

How to use Tera Term for the Serial Console
Expect SSH Password-less SSH login Telnet protocol Serial Console on Solaris sshfs
CygTerm+ LogMeTT Programmable Keyboards Tips Humor Etc

Introduction

Teraterm macro language is documented at MACRO Help Index.  They can be created in any text editor. There is VIM script that provide highlighting for Teraterm --  Teraterm MACRO syntax highlighting vim online

To run macro from within TeraTerm you need to click on Macro  in Control tab and select the macro to run (can be automated via Programmable Keyboards such as Logitech G510s gaming keyboard):

TeraTerm Macro language for dummies

TTL is a simple interpreted language like BASIC. To learn TTL quickly, study the sample macro files in the distribution package and the command reference.

Here is a brief tutorial called TeraTerm Macro language for dummies

TeraTerm basics

So to start things off, let's deal with variables.

There are two types of variables: Strings (limited to 255 characters) , and Integers (Limited to ~ +/-2 billion) (want to go bigger?). The type is determined implicitly when the variable is created and cannot be altered afterwards.

Also, since TeraTerm 4.72, there are now arrays of both of these types (limited to 65536 indices per array)

  1. integerVariable=10 ; An Integer variable
  2. strVariable='asdf' ; A string variable
  3. strVariable2='0' ; Still a string variable
  4. integerVariable='asdf' ; ERROR!!!!!!
  5. integerVariable=2
  6. ; A little fun with arrays:
  7. intdim integerArray 10 ; Create an integer array of size 10
  8. integerArray[0]=10 ; Assigns the first element of the array to 0
  9. integerArray[10]=10 ; ERROR!!!!!
  10. ; It's almost exactly the same for strings, just using strdim instead:
  11. strdim strArray integerVariable ; Creates a string array that has the size given in integerVariable
  12. ; Also, by the way, the semicolon (;) is the character that denotes a comment; anything after it in a line has no effect on the running of your script (notice how I used the semicolon in a grammatically correct way in a sentence about semicolons ;-)
But variables by themselves are boring. The real basis of any programming language is the if statement. However an if statement by itself is somewhat boring, so let's go a little further and also show you a wait statement:
  1. wait 'value = 0' 'ERROR'
  2. if result=1 then
  3. ; value = 0 was received on the terminal
  4. elseif result=2
  5. ; ERROR was received on the terminal
  6. endif
The wait statement is one of the more or less unique features of TeraTerm. Essentially, it reads through all of the serial output that hasn't been parsed yet, and when one of the strings in its sensitivity list is found, it sets result to the index of that string and moves on. The documentation for the setsync command has a partial general overview for how TeraTerm internally works.

Note that you can also set a timeout for wait, and it will then only hold up the execution of the script for at most timeout.mtimeout seconds:

  1. timeout=1
  2. mtimeout=500 ; Set the timeout for 1.5 seconds
  3. wait 'Good' 'Bad'
  4. if result=0 then
  5. ; Neither good nor bad appeared in the terminal output during the 1.5 seconds
  6. endif
A nice counterpoint to the 'wait' command is the 'sendln' and 'send' commands. These guys do pretty much what their names suggest: they write stuff back out to the terminal (with sendln adding a newline at the end of its string).
A common use that I have for the wait/sendln pair is to send a series of commands to a machine.
For example, when sending a series of commands to a Linux box, it might look something like the following:
  1. wait 'root@localhost#'
  2. sendln 'command'
  3. wait 'root@localhost#'
  4. sendln 'command'
  5. wait 'root@localhost#'
  6. sendln 'command'
This ensures that the prior command has finished before you send the next command. The sendkcode command is also occasionally useful in some cases.

Note that the above code is somewhat ugly. If the prompt were to change (say from root@localhost to root@servername), I'd have to manually replace a bunch of lines. Since I'm lazy, the following format is much nicer:

  1. cmdPrompt='root@localhost'
  2. wait cmdPrompt
  3. sendln 'command'
  4. wait cmdPrompt
  5. sendln 'command'
  6. wait cmdPrompt
  7. sendln 'command'
That looks much nicer.

Looping

Branching is all well and good, but eventually you will want to do something that has some repetition in it. Teraterm has several more or less standard looping constructs

  1. do while i>0
  2.     i = i - 1
  3. loop
  4. do
  5.     i=i-1
  6. loop while i>0
  7. for j 1 10
  8.     i=j+i
  9. next
  10. until i > 10
  11.   i = i + 1
  12. enduntil
  13. while i>0
  14.   i = i - 1
  15. endwhile
I won't go into too much detail about all of these; TeraTerm probably has more looping constructs than is really healthy, but definitely keep in mind the 'for' loop, and the fact that the 'do while' loop can be done as a pre-test loop or a post-test loop. The 'break' command can also be used to escape from a loop before its condition triggers

Lastly for the basics is TeraTerm's goto command. Some people (especially those experienced with C and who have experienced the nightmares of spaghetti code) view gotos as positively evil and wish they could be nuked from every language in existence. Gotos can do just about anything, but typically not as nicely as one of the above looping structures. About the only place that gotos should really go is in error handling:

  1. wait 'Good' 'Bad'
  2. if result=2 goto ERROR
  3. ; Presumably, a bunch of non-related code goes here
  4. exit ; Typically, you don't want to execute the error code upon successful completion of the normal code, so this exits before doing that
  5. :ERROR
  6. ; Error-handling/messaging code goes here
  7. exit
TeraTerm and regular expressions

Regular expressions are an interesting creature in and of themselves. An old and relatively well-known quote about regular expressions (regexes) goes something like this:

Quote: Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.

Regexes are a double-edged sword that allow you to do stuff with strings that would normally not be possible. Since I don't really want to go into all the nitty gritty, here's a link to wikipedia, as well as afairly-well-laid-out regex tutorial site. Note that TeraTerm has also posted a reference for the regex engine that they use, but that isn't really laid out for learning...

TeraTerm has several commands which use the power of regular expressions (let me know which one's I'm forgetting...):

  1. waitregex <string1 with regular expression> [<string2 with regular expression> ...]
  2. strmatch <target string> <string with regular expression>
  3. strreplace <strvar> <index> <regex> <newstr>
strmatch and strreplace are both relatively normal. You put your regular expression in, and TeraTerm works its magic.

waitregex is a beast that I've seen misused in these forums time and again. In many ways, it is identical to the wait command, just with regular expression support. The large difference is with regular expression matching. Regular expression matching is what happens when a regular expression with parenthesis triggers on a string. The result inside the string is then returned (in normal regular expressions, it is returned as \1, \2, \3, etc, while with TeraTerm, they are stored in the groupmatchstr1, groupmatchstr2,...,groupmatchstr9 variables).

An example is as follows:

  1. waitregex 'value = ([0-9]+)' 'ERROR'
  2. if result=1 then
  3.     str2int returnValue groupmatchstr1
  4. endif
Let's pick the above code fragment apart bit by bit. First we have '[0-9]+' This regex essentially grabs the largest amount of consecutive characters that fall between 0 and 9. The parenthesis '(...)' are what initiate the grouping. With the parenthesis, the string that was caught by '[0-9]+' gets stored into groupmatchstr1. If the regular expression as a whole was triggered, the result will be one and the if statement will let the str2int command work its magic. The str2int command (and its counterpart, the int2str command) do exactly what their names suggest: convert between string and integer variables.

Thus, if the 'value = 5643' was received on the terminal, this code would set returnValue to 5643

Note that waitregex only works on the first 255 characters of a line, so anything further than that and you're going to have to find another way.

File input and output

My file I/O skills are minimal at best. Anything that you'd care to offer is gladly accepted
Also, don't forget about the ability to use logging commands in some cases (check out loginfo, logopen, logclose, logstart, logpause, and logwrite)

User input and output

Most of the commands used here are described in the Miscellaneous Commands section of the TTL Command Reference.

  1. messagebox <message> <title>
This is one of the most commonly-used UI-type commands of the macro language. It pops up a window with the title equal to <title>, and the content equal to <message>, with an Ok button at the bottom. Note that code beneath this command won't execute until Ok is clicked.
  1. yesnobox <message> <title>
This is almost identical to the messagebox, however rather than an Ok button, there's a Yes and a No button. If Yes is clicked, result=1, otherwise result=0
  1. inputbox <message> <title> [<default>]
The third of the *box commands that I use on a normal basis, this is like the messagebox command, but with an area that the user can type stuff in. That area is populated with <default> when the window first appears, and after the user clicks Ok, the variable 'inputstr' contains the value of that area.

Usage of connect command and sample macros

The central command is Connect command for which we have a special page. Teraterm distribution contains six sample macros that illustrate is use. Examples are outdates and too chatty, but still useful:

delpassw.ttl
dialup.ttl
index
login.ttl
mpause.ttl
random.ttl
screencapture.ttl
ssh2login.ttl
wait_regex.ttl

The most important are two macros: login.ttl and ssh2login.ttl. They are a little bit too general to my taste and can benefit from some minor tweaking before you adopt them for login to real servers.  For example ssh2login.ttl asks a lot of unnecessary questions ;-)

... ... ... 

msg = 'Enter password for user '
strconcat msg username
passwordbox msg 'Get password'

msg = hostname
strconcat msg ':22 /ssh /auth=password /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg inputstr

connect msg
For Solaris servers it can be modified to
msg = 'box106.myfirma.com:22 /ssh /auth=password /user=root /passwd=mypass-1234'
connect msg
wait   "#"
sendln "bash"
sendln ". ~likbez/.profile"
sendln "cd ~likbez" 

For Linux boxes bash shell is default for root so the line sendln "bash" is redundant. After such a tweaking and testing, you can rename is to box106.ttl, drag this macro to the desktop and repeat the process for other servers so that each macro is individual to a server and represented by an icon.  Clicking on this icon will launch the macro that login you to a server. Say unto a hundred servers (ten rows ten servers each) this approach works reasonably well, especially if you have dual display configuration (in this case you better put icons on the second display). If you have more then that use folders for groups of similar servers such as Solaris, HP-UX, Linux, AIX and  put of the desktop folders instead of icons for the macros. Clicking on this icon will launch the macro. This way you can have unique macros for each of your important servers so that you do not need to recreate environment and other preliminary steps manually for it.  That can save a lot of time and is simpler and more efficient then programming keyboard macros.

You just need to write a script in Cygwin that changes passwords when time has come from a special table, using secret formula (for example $pass=$this_month_prefix."-".substr($server_name,-4) ), or whatever.

You can also use Passwordless SSH login for authentication to you own account and use sudo to get to root. That removes the choir of changing root accounts periodically.

Good source of inspiration for creating more complex Teraterm macros can be

 

Important additions to macro language since version 4.52

Generally you should use version 4.77 or later as there were important additions to macro language in each of recent versions (starting from 4.52):

2006.2.10 (Ver 4.27)

2006.10.09 (Ver 4.46)

2007.8.8 (Ver 4.53)

2007.9.30 (Ver 4.54)

New:

Priority Operator
1 not ~ ! +(unary) -(unary)
2 * / %
3 + -
4 >> << >>>
5 and &
6 xor ^
7 or |
8 < > <= >=
9 = == <> !=
10 &&
11 ||

Old:

Priority Operator
1 not ~ ! +(unary) -(unary)
2 * / %
2 and &
3 + -
3 or xor | ^
4 = <> < > <= >=
5 &&
6 ||

2007.12.4 (Ver 4.56)

2008.1.15 (Ver 4.57)

2008.6.25 (Ver 4.59)

added 'strmatch' macro command.
- added 'setrts' and 'setdtr' macro command.

2008.9.23 (Ver 4.60)

- added 'crc32' and crc32file macro command.
- added 'getttdir' macro command.

2009.3.22 (Ver 4.62)

2009.7.5 (Ver 4.63)

2009.11.10 (Ver 4.64)

2010.2.20 (Ver 4.65)

2010.5.31 (Ver 4.66)

2010.8.31 (Ver 4.67)

2011.3.5 (Ver 4.69)

Here is funny a sample program:

; 1. Open Tera Term Pro (free VT100 emulator)
; 2. Control -> Macro
; 3. choose 99b.ttl
;
; it will run even if you close the app, to kill prematurely please open
; taskmanager and kill the ttpmacro.exe
;
; 99 Bottles of Beer macro for Tera Term
; by Lance Yamada

for i 99 1

j = i - 1
int2str istr i
int2str jstr j
        strconcat istr ' bottles of beer on the wall,'
strconcat jstr ' bottles of beer on the wall!'

if i = 1 then
   messagebox '1 bottle of beer on the wall,' 'Tera Term'
   messagebox 'time to get more beer!' 'Tera Term'
else
   messagebox istr 'Tera Term'
   messagebox 'take one down pass it around,' 'Tera Term'
   messagebox jstr 'Tera Term'

endif
next

Several sample ttl files are included with Teraterm, but they usually need some minor tweaking before they work with real servers.  For example:

; sample macro of Tera Term
; 
; File: ssh2login.ttl
; Description: auto login with SSH2 protocol
; Environment: generic
; Update: 2004/12/4
; Author: Yutaka Hirata

username = 'nike'
hostname = '192.168.1.3'

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

msg = 'Enter password for user '
strconcat msg username
passwordbox msg 'Get password'

msg = hostname
strconcat msg ':22 /ssh /auth=password /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg inputstr
You can drag macro to the desktop so that each macro is represented by an icon.  Clicking on this icon will launch the macro.

This way you can have unique macros for each of you important servers so that you do not need to recreate environment and other preliminary steps manually for it.  That can save a lot of time and is simpler and more efficient then programming keyboard macros.


Top updates

Bulletin Latest Past week Past month
Google Search


NEWS CONTENTS

Old News ;-)

Teraterm Script to Automate Command entry Tera Term Script to Automate Command entry

 
 I wanted to create a script which when clicked on will log in to the node, run a list of commands located in a text file and save the output in a log file.

Initially I created a script using securecrt but I hit upon an hurdle. Some of the command output were many pages long and a '--More--' prompt appeared .
 
 

In some nodes like cisco routers this is not a problem. There is a command 'Terminal length 0' which allows you to print complete output for commands with long outputs like 'show run'. I was working on a linux based node and no such option existed. The script was unable to recognise this prompt. I had to manually hold down  the spacebar till the next prompt appeared.

This was solved in newer versions of securecrt with 'crt.screen.Readstring' command. However securecrt is not a freeware. So I searched for other alternatives and found TeraTerm
 

Teraterm is an alternative to putty and securecrt.

 

You can download from
 

http://logmett.com/index.php?/download/tera-term-476-freeware.html


 

The command reference can be found here


 

http://ttssh2.sourceforge.jp/manual/en/macro/command/index.html


It has its own TTL scripting language but its  very powerful and easy to learn. Best part is its open source and freeware.

Below is what the script does.

The commands to be run are put in file commands.txt in path  D:\TTERMPRO313.The output will be created in same path with name log appended with current timestamp.

The first command in the commands.txt file is run and a log file is opened , then it waits for prompt --More-- or @CLA.

If '--More--' prompt is detected it sends '#32' (i.e. spacebar). If '@CLA'  prompt is detected it runs the next command. When all commands are run its done.

Below is the script.
 

; ******* LogMeTT Macro Template for command output************************************
;commands to be put in file commands.txt in path D:\TTERMPRO313
;log Output with current timestamp created in same path 
;hardcoded username and password (not recommended)
;connect to node with ip 82.56.56.7 using ssh2 with username a username ans password as password
connect '82.56.56.7 /ssh /2 /auth=password /user=username /passwd=password'
; Entering synchronous communication mode (useful in most cases)
setsync 1
;wait for the screen prompt @CLA
wait '@CLA'
;put the command file containing the list of commands in this path
setdir 'D:\TTERMPRO313'
changedir 'D:\TTERMPRO313'
;logfile will be created with below name and appended with current timestamp
gettime logfile "log-%Y%m%d-%H%M%S.txt"
;start logging
logopen logfile 0 0
fileopen commandfile 'commands.txt' 0
:looper2
filereadln commandfile statement
if result goto fclose2
sendln statement
:label2
wait '@CLA' '-More-'
if result=2 send #32 
if result=1 goto looper2
goto label2
:fclose2
fileclose commandfile
;stop logging
logclose
;************************************************************************************

[Jul 08, 2014] TTLEditor: Video Tutorial - YouTube

[Jul 08, 2014] Teraterm Scripts - Texas Instruments Embedded Processors Wiki

Tera Term is an opensource terminal emulator on MS-Windows commonly used by us developers. Tera Term supports a "rich" macro language that can help in automating user actions. These scripts usually remain personal - rarely shared.

In this page, I intend to share the basic scripts that can be used to automate common tasks in the Linux PSP release. The scripts were created with intentional hierarchy to maximize reuse (via inclusion) and minimize redundancy across scripts. Currently, these scripts apply to OMAP35x Linux PSP. But, can be extended easily to other platforms.

Needless to say, these scripts are open to enhancements.

 

GroupStudy.com - CCIE, CCNA, CCNP and other Cisco Certifications


I saw a discussion about Hyperterm and Tera Term a few weeks ago. I had no
idea so many people liked Tera Term as much as I did. For the record, I used
Tera Term the whole time I was studying except for the last 2 weeks before
my exam. Then I switched to Hyperterm just to get used to the look & feel.

I wrote a useful Tera Term macro. It assumes you are connected to your
terminal server and that you are using that to connect to your routers. This
macro assumes you have the terminal server and 5 other routers, but as you
can see its very  easy to add or subtract for the actual number of routers
you have.

This macro automatically gets the running-config and the ip routing table
from all your routers and places into a log file. What could be easier?!?!
It prompts you for the filename; the directory is in the first few lines of
the macro (and can be easily changed to whatever you want). You can easily
add other commands - I created other macros based on this one for IPX, DLSw,
etc.

I found this useful since as I studied each major topic I wanted to record
the results in case I had questions later or if I wanted to review topics.
Since it does everything automatically its also handy in cases such as if
you are studying BGP. You can alter the preference, run the macro, alter the
MED, run the macro, etc. In that case you'd probably also want to include
show ip bgp commands.

My comments and the actual macro are included below. Hopefully this will
save some folks some time - you never have enough of that! If you don't
currently use Tera Term you may want to consider it, I believe it can be
downloaded from www.tucows.com.

Robert M. Webber
Senior Network Consultant
CCIE 6922
Callisma - Building Your Network of the Future. Today.
Cell: 508-254-6400
Pager: 877-964-8111
http://www.callisma.com

To use the macro, simply copy the macro into an editor such as notepad and
save it. I recommend saving it with a ".ttl" extension since that is what
Tera Term will look for by default. I keep my macros right in the Tera Term
default directory, c:\Program Files\TTERMPRO. This macro gets used when you
are connected to your terminal server and have already established
connections to your routers via the terminal server.

To specify the directory you want to place your logs in, simply edit line 2
(and then cut & paste that line into line 6 and 13).  The directory you see
is what my laptop uses for its "My documents" folder; obviously you can
change this to whatever you want. You can use whatever you want for the
filename (you aren't restricted to 8 letters). So you can call your log
"OSPF with area summarization.doc" if you want. I usually use the .doc
extension so Word will bring it up if I want to review it later.

The first 16 lines ask for the filename and check to make sure it doesn't
already exist.

Note that you must be in enable mode for this to work since Tera Term is
waiting for the "#" to come back from the router to let it know the router
is ready. The commands:

sendln "term length 0"
wait "#"
sendln "show running-config"
pause 16
sendln "show ip route"
wait "#"
sendln "term length 42"
wait "#"
pause 1

are what get sent to each router. Here is where you can enter whatever
commands you want. I included the "pause 16" (Tera Term waits 16 seconds)
just to make sure Tera Term doesn't overrun the router while the router is
generating its config. Tera Term is supposed to wait for the "#" before
going on, but I found this just seemed to work better.

the commands

send "1"
sendln #13
wait "#"

tell your terminal server to connect to session 1, and send a return. The
return simply gets the router to return its prompt.

The command

sendln #30#$78
wait "#"

sends the ctrl-shift-6 x so that you break back to the term server. As you
can see you then issue a "2" (going to the second connection) and repeat the
whole process. So just adjust these lines based on the number of routers you
are working with.

Good luck!
Here's the macro:
--------------------------
timeout = 120
directory = "C:\Documents and Settings\RMW1\My Documents\My Router configs\"
inputbox "Name of log file for this test:" "CCIE Here I come!"
strconcat directory inputstr
logfilename = directory
directory = "C:\Documents and Settings\RMW1\My Documents\My Router configs\"
:search_logfile
filesearch logfilename
if result=0 goto openlog
 inputbox "Enter new filename:" "File already exists!"
 strconcat directory inputstr
 logfilename = directory
 directory = "C:\Documents and Settings\RMW1\My Documents\My Router
configs\"
 goto search_logfile
:openlog
logopen logfilename 1 1

sendln "show clock"
wait "#"

sendln "term length 0"
wait "#"
sendln "show running-config"
pause 16
sendln "show ip route"
wait "#"
sendln "term length 42"
wait "#"
pause 1

send "1"
sendln #13
wait "#"

sendln "term length 0"
wait "#"
sendln "show running-config"
pause 16
sendln "show ip route"
wait "#"
sendln "term length 42"
wait "#"
pause 1

sendln #30#$78
wait "#"
send "2"
sendln #13
wait "#"

sendln "term length 0"
wait "#"
sendln "show running-config"
pause 16
sendln "show ip route"
wait "#"
sendln "term length 42"
wait "#"
pause 1

sendln #30#$78
wait "#"
send "3"
sendln #13
wait "#"

sendln "term length 0"
wait "#"
sendln "show running-config"
pause 16
sendln "show ip route"
wait "#"
sendln "term length 42"
wait "#"
pause 1

sendln #30#$78
wait "#"
send "4"
sendln #13
wait "#"

sendln "term length 0"
wait "#"
sendln "show running-config"
pause 16
sendln "show ip route"
wait "#"
sendln "term length 42"
wait "#"
pause 1

sendln #30#$78
wait "#"
send "5"
sendln #13
wait "#"

sendln "term length 0"
wait "#"
sendln "show running-config"
pause 16
sendln "show ip route"
wait "#"
sendln "term length 42"
wait "#"
pause 1

sendln #30#$78
wait "#"

logclose

_______________________________________________________
To unsubscribe from the CCIELAB list, send a message to
majordomo@xxxxxxxxxxxxxx with the body containing:
unsubscribe ccielab

[Jun 03, 2011] Power of Tera Term

May 7, 2007 | Extreme Networking

Tera Term is easily one of the most powerful free Terminal Emulation Software that I have come across and the power of it lies in its scripting language TTL. Of-course, it is not as extensive and capability rich as Perl but for a non-programmer, a browse through a single help file on TTL language is all it takes to create wonderful scripts that could make his/her life easier in maintaining huge networks.

The capabilities are limitless and if you could find better ways of enhancing the following script, please come forward and do your bit.

The purpose of the script is for terminal configuration of repetitive commands in multiple terminal enabled devices at once but sequentially without human intervention.

The other purpose of the script is to fetch terminal information from multiple terminal enabled devices at once but sequentially without human intervention.

The script is primarily designed for working with Cisco devices which can be configured using terminal emulation and which has a Command Line Interface ( CLI ).

The Procedure for execution of the script is as follows:

P.S.: While fetching configuration from any device like that of Cisco, please make sure to keep the terminal length to zero so that the device prompt appears immediately after the execution of the command.

Please give your feedbacks to make it better.

Below is the script for use with Tera Term. Just copy & paste the below code in a notepad and save it with a TTL extension.


;#######################################################
;Version 3.2 (Untested Beta Version)
;Created on 25/03/2007
;For Tera Term Macro Use Only
;Created originally for Cisco Switch & Routers.
;Customizable script, please feel free to edit it.(In fact you have to edit it)
;For usage, execute ttpmacro.exe and select this script
;Write all the ip addresses in sequential order in a file by the name of 'ip.txt'
;Write all the commands in sequential order in a file by the name of 'commands.txt'
;and place the text files in the directory as set by the 'setdir' keyword.
;Complaints and suggestions for improvement welcome.
;Please feel free to take it to the next level and share it with others.
;#######################################################
;######################################################
;Main Script
;######################################################
timeout = 5
dirname = 'E:\Program Files\TTERMPRO313'
setdir dirname
fileopen addressfile 'ip.txt' 0
inputbox 'Username:' 'Username Prompt'
Username = inputstr
passwordbox 'Password:' 'Password Prompt'
Password = inputstr
yesnobox 'Is enable password authentication required?' 'Enable Password Question'
if result=0 then
goto jumper1
endif
passwordbox 'Enable Password:' 'Enable Password Prompt'
Enable = inputstr
:jumper1
yesnobox 'Would you like to log the output?' 'Logging'
Logger = result
if Logger = 0 then
goto looper1
endif
inputbox 'Directory path & name followed by \' 'Logging Directory'
loggerpath = inputstr
dirmaker = 'cmd /C md '
strconcat dirmaker loggerpath
exec dirmaker
:looper1
filereadln addressfile ip
connect ip
if result = 1 then
goto fclose1
endif
wait 'Username:' 'Password:' '>' 'login:'
if result = 0 then
call terminator
goto looper1
elseif result = 1 then
goto jumper4
elseif result = 2 then
goto jumper5
elseif result = 3 then
goto jumper6
elseif result = 4 then
goto jumper4
endif
:jumper4
sendln Username
wait 'Password:'
:jumper5
sendln Password
if Logger = 0 then
goto jumper2
endif
loggerfile = loggerpath
strconcat loggerfile ip
logopen loggerfile 0 0
loggerfile = ''
:jumper2
wait '#' '>'
if result = 0 then
call terminator
elseif result = 1 then
call commander
call terminator
elseif result = 2 then
:jumper6
call enabler
call commander
call terminator
endif
goto looper1
:fclose1
fileclose addressfile
closett
end
;################################################
;Enabler is a sub-routine which provides enable password authentication.
;################################################
:enabler
setdir dirname
sendln 'enable'
wait 'Password:'
sendln Enable
return
;#####################################
;Commander is a sub-routine which executes the
;commands placed in 'commands.txt' file
;#####################################
:commander
setdir dirname
fileopen commandfile 'commands.txt' 0
:looper2
filereadln commandfile statement
if result goto fclose2
sendln statement
wait '#' '>'
if result = 2 then
goto fclose2
endif
goto looper2
:fclose2
fileclose commandfile
return
;###########################################
;Terminator is a sub-routine which provides terminal closure and
;passing the control to the main sub-routine for loop completion.
;###########################################
:terminator
beep
if Logger = 0 then
goto jumper3
endif
logclose
:jumper3
closett
return
;###########################################

http://logmett.com/forum/viewtopic.php?t=1085

Ishmael first of all thank you so much for your time and efforts! Below you'll find my script, please feel free to comment and to point me where i'm doing wrong any comments on how to improve are appreciated! thank you again!

Alessio

P.S. is your nick remotely connected with a David Gerrold Book?

Select all

  1. ;°°°°°°°°°°°°°°° Conf Parameters °°°°°°°°°°°°°
  2.  
  3. username='XXXXX' ; username for connection
  4. password='YYYYYY' ; password for connection
  5. ip_address='IP_TEST.txt'; file with ip addresses of the routers to analyze
  6. timeout=5; timeout
  7. Sedi='indirizzi.txt'; file containing results
  8.  
  9.  
  10. ;°°°°°°°°°°°°°°°° APERTURA FILE CONTENENTE IP °°°°°°°°°
  11. ; Opening file with addresses
  12.  
  13.     fileopen fhandle Ip_address 0
  14.     filecreate fh Sedi
  15.    
  16. ;°°°°°°°°°°°°°°°° INIZIO LOOP °°°°°°°°°°°°°°°°°°°°°°°°
  17. ; Initial Loop
  18.  
  19.     :loop
  20.         filereadln fhandle ip_address
  21.         if result goto fine
  22.         flushrecv
  23.  
  24. ;°°°°°°°°°°°°°°°° CONNESSIONE °°°°°°°°°°°°°°°°°°°°°°°°°
  25. ; TT connects to one of the addresses in the list specified above
  26.    
  27.      connect ip_address
  28.  
  29. ;°°°°°°°°°°°°°°°° INVIO USERNAME °°°°°°°°°°°°°°°°°°°°°°
  30. ; Routine for sending username to host
  31.  
  32.     wait 'Username:'
  33.        if result=0 then
  34.         space = '_'
  35.         strconcat Ip_address space
  36.         strconcat Ip_address 'non connessa'
  37.         filewriteln fh ip_address
  38.             goto prossimo
  39.        endif
  40.         sendln username
  41.  
  42. ;°°°°°°°°°°°°°°°° INVIO PASSWORD °°°°°°°°°°°°°°°°°°°°°°
  43. ; Routine for sending password to host
  44.  
  45.     wait 'Password:'
  46.        if result=0 then
  47.         space = '_'
  48.         strconcat Ip_address space
  49.         strconcat Ip_address 'Tacacs non funzionante'
  50.         filewriteln fh ip_address
  51.         goto prossimo
  52.        endif
  53.        setsync 1
  54.     sendln password
  55.        setsync 0
  56.    
  57. ;°°°°°°°°°°°°°°°° CONTROLLO LINEA BGP °°°°°°°°°°°°°°°°°°°°°°
  58. ; TT sends commands for checking BGP links
  59. ;°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
  60.  
  61.  
  62.        flushrecv                       ;ensure nothing is in the buffer
  63.        sendln 'terminal length 0'  
  64.        sendln 'sh bgp a s'
  65.             for i 1 16
  66.             recvln           ;skip past technobabble
  67.                next
  68.                recvln
  69.                filewriteln fh inputstr  ;now save two relevant lines in a file
  70.                recvln
  71.                filewriteln fh inputstr
  72.         goto prox
  73.  
  74. ;°°°°°°°°°°°°°°°° DISCONNESSIONE  con HOST °°°°°°°°°°°°°°°°
  75. ; Disconnect from the host
  76.  
  77.     sendln 'exit'
  78.  
  79. ;°°°°°°°°°°°°°°°°     LOOP     °°°°°°°°°°°°°°°°°°°°°°°°°°°°
  80. ;Loop Procedure
  81.  
  82.     :prox
  83.         pause 1
  84.         disconnect
  85.         closett
  86.         goto loop
  87.  
  88. ;°°°°°°°°°°°°°°°°° FINE PROGRAMMA °°°°°°°°°°°°°°°°°°°°°°
  89. ; Close TT after ll IP have been processed
  90.  
  91.     :end
  92.         fileclose fhandle
  93.         end
  94.  

I would put in a testlink after your connect to make sure the connect worked. I think you might be having another issue related to perhaps your hardware or network setup that is not allowing you to connect.

As a suggestion, do a minor rewrite to get rid of all GOTO statements. They were unavoidable in TeraTERM 2.3, but the latest version has more than enough types of flow control statements to avoid them completely. This will save you much grief in the long run, trust me. Also, it would be easier to read if you only indented loops and if statement bodys.

Think Herman Melville instead of David Gerrold. Rather pretentious literary reference from my younger days that has somehow stuck.

[Mar 16, 2007] TeraTerm Support Forums View topic - First step to Tera Term Macros

linkstate
Joined: 16 Mar 2007
Posts: 7

Posted: Fri Mar 16, 2007 5:34 pm    Post subject: First step to Tera Term Macros


First to all I want to congratulate this wonderful tool of trade 


I'm willing to take my first step into Tera Term Macros and I will probably won't make it on my own.

So here's the deal.

I want to be able to telnet to an access router in a companies HQ and then telnet into all the switches and do a "show tech" command in order to backup all the configs and informations about the switches into a text file.

All the switches are Cisco 2950.


Please help this poor man in need of your assistance.

With My best regards, boris

====
 

boris
Guru


Joined: 08 Jan 2005
Posts: 496
Location: Seattle, WA, USA
 

Posted: Fri Mar 16, 2007 6:37 pm    Post subject: Reply with quote

boris wrote:
Depending on the number of switches different approaches can be taken. Can you say roughly how many switches you need to connect to?

About 600 Switches :s

Cheers,

Posted: Fri Mar 16, 2007 7:16 pm    Post subject: Reply with quote

TeraTerm macro can definitely help you but writing such script is not an easy task. I'd suggest you to approach it in few steps.

Start with writing telnet connection macro script to establish connection to your access router in HQ. You can use connection template from LogMeTT or sample code from one of the topics in these forums.

Then develop your script further to establish the second connection.

Step 3 will be to get familiar with opening and closing log files from macro. Here you can also add your "show tech" command.

Once you get to this point your macro will be able to reach one of your Cisco-s collect printout and save it into local file.

Then comes the tricky part. You need to store IP addresses of all your 600 switches in plain text file and run your macro in the loop where every execution will be using one IP from your IP list. Similar issue was discussed not long ago on our forums; just do some search and reading here. 2 more files that will help you are macro.hlp and LogMeTT.chm. You will find them both in TeraTerm package. Finally do not forget to give different file names to each log file. You probably can build log name from IP address and current time stamp. Check code snippets in our forums for the example.
_________________
Thanks.
Best regards,
// Boris

linkstate
Newbie


Joined: 16 Mar 2007
Posts: 7

 

Posted: Fri Mar 16, 2007 8:24 pm    Post subject: Reply with quote

I'm feeling a bit fuzzy about this ....


My first piece of code turned out something like this:

connect 'ip address:23 /nossh' ----> that's my office
wait'Username:'
sendln'usernamexxx'
wait'Password:'
sendln'passwordxxx'
wait'hostname>'
sendln'telnet 172.23.128.11' ----> that's the HQ of the company
wait'Username:'
sendln'usernamexxx'
wait'Password:'
sendln'passwordxxx'

fileopen filehandle 'file.txt' 0

filereadln filehandle line

while result=0

sendln line

wait'Username:'
sendln'usernamexxx'
sendln'passwordxxx'
sendln 'show tech'
sendln'exit'

endwhile

exit


My problem is that the macro does read the first ip address of the text file but it doesn't continue with the other ones.

Regarding the issue of saving the logs with each hostname or ip address, that remains a complete phantom to me.


Hope that explained myself correctly to you.


My regards,

boris
Guru


Joined: 08 Jan 2005
Posts: 496
Location: Seattle, WA, USA
 

Posted: Sat Mar 17, 2007 6:22 am    Post subject: Reply with quote

Do you have word 'telnet' followed by IP address on each line of your file.txt?
_________________
Thanks.
Best regards,
// Boris
linkstate
Newbie


Joined: 16 Mar 2007
Posts: 7

 

Posted: Sat Mar 17, 2007 2:45 pm    Post subject: Reply with quote

No. Just the ip addresses. why ? is it supposed to ?
boris
Guru


Joined: 08 Jan 2005
Posts: 496
Location: Seattle, WA, USA
 

Posted: Sat Mar 17, 2007 5:03 pm    Post subject: Reply with quote

The same way as you do
Quote:
sendln'telnet 172.23.128.11'
earlier in your script, you need to pass method of connection and IP address in
Quote:
sendln line
Content of variable line is one string of your file.txt. If file does not contain words 'telnet' than you get as the result
 
Code:
sendln  172.23.x.y  ; <- whatever IP you have
which is the same as if you would type only IP address at command prompt without mentioning whether you need telnet, ssh, rlogin etc.

Instead of adding 600 words 'telnet' to your file, the better approach would be to concatenate word 'telnet' and IP taken from the file.

The code can look like this:
 

Code:
...

fileopen filehandle 'file.txt' 0

filereadln filehandle line

while result=0
connect_line='telnet ' ; <-- make sure you have trailing space
strconcat connect_line line
sendln connect_line

wait'Username:'
sendln'usernamexxx'
sendln'passwordxxx'
sendln 'show tech'
sendln'exit'


endwhile
...


_________________
Thanks.
Best regards,
// Boris
linkstate
Newbie


Joined: 16 Mar 2007
Posts: 7

 

Posted: Sun Mar 18, 2007 10:46 am    Post subject: Reply with quote

Okay.
I have something like this...
I'm testing with a .txt file with just two ip addresses for testing.
The thing is that he only telnets into the first ip address in that file and doesn't jump into the 2 ip address.
Could it be something with the while procedure ?


sendln line

while result=0
connect_line='telnet ' ;
strconcat connect_line line

wait'Username:'
sendln'XXXXX'
sendln'XXXX'
timeout = 4
sendln 'show tech'
sendln'exit'

endwhile


Thank you for your assistance

boris
Guru


Joined: 08 Jan 2005
Posts: 496
Location: Seattle, WA, USA
 

PostPosted: Sun Mar 18, 2007 11:11 am    Post subject: Reply with quote

Read description of strconcat function. Your connection string is in connect_line and not in line.
There are few other issues with your last code. Please post here your complete macro, otherwise it is unclear if there are scripting or copy/pasting errors.
_________________
Thanks.
Best regards,
// Boris
linkstate
Newbie


Joined: 16 Mar 2007
Posts: 7

 

PostPosted: Sun Mar 18, 2007 11:18 am    Post subject: Reply with quote

Let me see if I can explain this better to you.

Here's the whole code with comments

connect '213.xx.xx.xx:23 /nossh' ---> My first telnet to my company
wait'Username:'
sendln'xxxxxxx'
wait'Password:'
sendln'xxxxxxx'
wait'hostname>'
sendln'telnet 172.23.xx.xx' ---------> My telnet to the other company
wait'Username:'
sendln'xxxxx'
wait'Password:'
sendln'xxxxxxx'

-------------"So far So good here"--------------------


fileopen filehandle 'andre.txt' 0

filereadln filehandle line

sendln line

while result=0
connect_line='telnet ' ;
strconcat connect_line line

wait'Username:'
sendln'xxxxx'
wait'Password:'
sendln'xxxxx'
timeout = 4
sendln 'xxxxx'
sendln'exit'

endwhile

exit

boris
Guru


Joined: 08 Jan 2005
Posts: 496
Location: Seattle, WA, USA
 

PostPosted: Sun Mar 18, 2007 2:48 pm    Post subject: Reply with quote

Try this code

 

Code:
connect '213.xx.xx.xx:23 /nossh'
wait 'Username:'
sendln 'xxxxxxx'
wait 'Password:'
sendln 'xxxxxxx'
wait 'hostname>'
sendln 'telnet 172.23.xx.xx'
wait 'Username:'
sendln 'xxxxx'
wait 'Password:'
sendln 'xxxxxxx'

fileopen filehandle 'andre.txt' 0

while result=0
 connect_line='telnet '
 filereadln filehandle line
 strconcat connect_line line
 sendln connect_line
 wait 'Username:'
 sendln 'xxxxx'
 wait 'Password:'
 sendln 'xxxxx'
 timeout = 4        ; I'm not sure why do you need this line
 sendln 'xxxxx'     ; and this line
 sendln 'show tech'
 wait 'xxxxxxxxx'  ; <= whatever appears at the end of 'show tech'  printout
 sendln 'exit'
endwhile

fileclose filehandle

exit


_________________
Thanks.
Best regards,
// Boris
 

IshmaelCallMe
Coach


Joined: 25 Jan 2006
Posts: 119
Location: Denver, Colorado, USA
 

PostPosted: Mon Mar 19, 2007 6:53 am    Post subject: Reply with quote

You need to move your filereadln command to just before the while and a copy of it to just before the endwhile so that the result variable gets set correctly for both the first and subsequent loops.
boris

Guru

Joined: 08 Jan 2005
Posts: 496
Location: Seattle, WA, USA
Mon Mar 19, 2007 7:14 am    Post subject: Thanks, I missed that

Code:

connect '213.xx.xx.xx:23 /nossh'
wait 'Username:'
sendln 'xxxxxxx'
wait 'Password:'
sendln 'xxxxxxx'
wait 'hostname>'
sendln 'telnet 172.23.xx.xx'
wait 'Username:'
sendln 'xxxxx'
wait 'Password:'
sendln 'xxxxxxx'

fileopen filehandle 'andre.txt' 0
filereadln filehandle line

while result=0
 connect_line='telnet '
 strconcat connect_line line
 sendln connect_line
 wait 'Username:'
 sendln 'xxxxx'
 wait 'Password:'
 sendln 'xxxxx'
 timeout = 4        ; I'm not sure why do you need this line
 sendln 'xxxxx'     ; and this line
 sendln 'show tech'
 wait 'xxxxxxxxx'   ; <= whatever appears at the end of 'show tech'  printout
 sendln 'exit'
 filereadln filehandle line
endwhile

fileclose filehandle

exit
_________________
Thanks.
Best regards,
// Boris

IshmaelCallMe
Coach

Joined: 25 Jan 2006
Posts: 119
Location: Denver, Colorado, USA
Posted: Mon Mar 19, 2007 8:11 am   


There ya go, that oughta work. 

One additional thing I would suggest to the original poster is to set the "timeout" variable BEFORE anyents (not in the middle of the loop, as it is ) and then (ideally) check the "result" variable each time.

If the timeout variable is not set, I believe it defaults to 0, which means the script will wait forever at that point if the text does not show up.

Recommended Links

Tera Term Scripting, need help developing a macro - Stack Overflow

Good source of inspiration can also be

Forums

 

TTL  reference

TTL is a simple interpreted language like BASIC. To learn TTL quickly, study the sample macro files in the distribution package and the command reference.

 

Command index

Communication commands

Control commands

String operation commands

File operation commands

Password commands

Miscellaneous commands

 




Etc

Society

Groupthink : Understanding Micromanagers and Control Freaks : Toxic Managers : BureaucraciesHarvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Two Party System as Polyarchy : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

Skeptical Finance : John Kenneth Galbraith : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Oscar Wilde : Talleyrand : Somerset Maugham : War and Peace : Marcus Aurelius : Eric Hoffer : Kurt Vonnegut : Otto Von Bismarck : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Oscar Wilde : Bernard Shaw : Mark Twain Quotes

Bulletin:

Vol 26, No.1 (January, 2013) Object-Oriented Cult : Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks: The efficient markets hypothesis : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

 

The Last but not Least


Copyright © 1996-2014 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine. This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting hosting of this site with different providers to distribute and speed up access. Currently there are two functional mirrors: softpanorama.info (the fastest) and softpanorama.net.

Disclaimer:

The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: July 13, 2014