In these tcpdump examples you will find 22 tactical commands to zero in on the key packets. Know your network with this powerful packet capture tool. Examples for http, icmp, dns, snmp and more. I used the Tera Term Language (TTL) and created a macro which solved my problem. I used code to wait for a prompt from the system and then print out the command I wanted. It is not made for scripting, but rather for interactive use. Try to get a command line serial terminal emulator.
- Ttl Script Examples
- Ttl Script Examples Definition
- Tera Term Macro Commands
- Ttl Script Examples Python
- Teraterm Ttl Script Examples
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 execution of the script requires Tera Term Software installed on the user's computer. The Tera Term software can be downloaded from the following URL:
http://hp.vector.co.jp/authors/VA002416/teraterm.html
The Procedure for execution of the script is as follows:
- Download Tera Term from the above mentioned URL.
- Unzip and downloaded file and execute the Setup.exe file.
- Install the Software in a specified destination.
- Copy the below provided script in the Tera Term directory.
- The script has an extension of .TTL and can be opened in notepad.
- Open the file in notepad and change the variable ‘dirname' in the script to reflect your Tera Term Directory.
- Make two Text Files in the directory specified by the variable ‘dirname' by the following names:
ip.txt
commands.txt
- Make sure you have telnet access to all the devices you would like to configure.
- Open the ip.txt file and enter the IP addresses of all the devices for which the configuration needs to be fetched or pushed. Place only one IP address per line.
- Open the commands.txt file and enter the commands for configuration or commands for fetching configuration from the devices. Please note that the commands need to be compatible on all the devices entered in the ip.txt file. Please put in the commands in sequential order as you would configure the device in the normal circumstances. For eg:
terminal length 0
show running-config
config t
enable secret cisco
exit
terminal length 24
write memory
- Please place the commands that are required after authentication is done. The script expects a device prompt (which can be customized easily) by default after authentication. You may change it at any point of time.
- Execute the file ttpmacro.exe present in the Tera Term directory and select the macro '.ttl' extension.
- Please enter the Username for logging into the devices and press OK. Please note the Username and Password prompt would appear only once. The same username and password would be used to login into all the devices.
- Please enter the password in the Password Prompt Dialog Box and press OK.
- For Enable Password Authentication Prompt, click yes if you have enable password authentication configured in atleast some of the devices or click no if you don't have enable password configured in any of the devices.
- If yes, then provide the enable password to log into all the devices. The password is assumed to be common on all the devices.
- For the Logging Dialog Box. Click yes if you want to log the output or click no if you don't want to log the output.
- If yes, enter the directory in which the logs have to be placed. For eg: If the directory is ‘C:logs' then enter it in the following format:
c:logs
- Please note that the ‘' sign is important after you enter the directory path and name.
- Sit back and relax, as the commands entered in the commands.txt file is executed in every device entered in ip.txt.
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 FilesTTERMPRO313'
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
;###########################################
Next, in a slightly more useful example, let's study about a script to ping a specified IP address and record the results in a log.
Here, let's assume that the scripts indicated as examples are all saved as executable files.
Execute a router command
With the Lua script function, the rt.command function to execute normal router commands from script is provided as a library. Thus, you can execute a ping by calling the pingrouter command using the rt.command function.
[Ping1.lua] Download
How was that? Were you able to ping? When it is not possible to reach the destination IP address, nothing is displayed, so in that case rewrite the address to the IP address that is reachable for '192.168.100.100' and try again.
Now, we will explain the script. First, the first line executes the router's ping command.
The rt.command function interprets a string given as an argument as a router command and executes it. Additionally, the rt.command function returns 2 values as return values. The first return value shows that the string is properly interpreted as a command and whether or not the command was executed. The second return value is the command output result or an error message. In this example, they are respectively assigned to flag and outputs variables.
terminal length 24
write memory
- Please place the commands that are required after authentication is done. The script expects a device prompt (which can be customized easily) by default after authentication. You may change it at any point of time.
- Execute the file ttpmacro.exe present in the Tera Term directory and select the macro '.ttl' extension.
- Please enter the Username for logging into the devices and press OK. Please note the Username and Password prompt would appear only once. The same username and password would be used to login into all the devices.
- Please enter the password in the Password Prompt Dialog Box and press OK.
- For Enable Password Authentication Prompt, click yes if you have enable password authentication configured in atleast some of the devices or click no if you don't have enable password configured in any of the devices.
- If yes, then provide the enable password to log into all the devices. The password is assumed to be common on all the devices.
- For the Logging Dialog Box. Click yes if you want to log the output or click no if you don't want to log the output.
- If yes, enter the directory in which the logs have to be placed. For eg: If the directory is ‘C:logs' then enter it in the following format:
c:logs
- Please note that the ‘' sign is important after you enter the directory path and name.
- Sit back and relax, as the commands entered in the commands.txt file is executed in every device entered in ip.txt.
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 FilesTTERMPRO313'
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
;###########################################
Next, in a slightly more useful example, let's study about a script to ping a specified IP address and record the results in a log.
Here, let's assume that the scripts indicated as examples are all saved as executable files.
Execute a router command
With the Lua script function, the rt.command function to execute normal router commands from script is provided as a library. Thus, you can execute a ping by calling the pingrouter command using the rt.command function.
[Ping1.lua] Download
How was that? Were you able to ping? When it is not possible to reach the destination IP address, nothing is displayed, so in that case rewrite the address to the IP address that is reachable for '192.168.100.100' and try again.
Now, we will explain the script. First, the first line executes the router's ping command.
The rt.command function interprets a string given as an argument as a router command and executes it. Additionally, the rt.command function returns 2 values as return values. The first return value shows that the string is properly interpreted as a command and whether or not the command was executed. The second return value is the command output result or an error message. In this example, they are respectively assigned to flag and outputs variables.
In line 2, the value of the flag variable in the if statement is examined.
In an if statement, when the value of the formula between if and then is true, the statements in the interval between then and end are executed. The expression in this example is merely a flag so it becomes an execution result of the rt.command function, and if the format of the command is proper and the command is executed, it will be true.
In the 3rd line, just the first line of the output results of the ping command is extracted.
In the outputs variable, all the output results of the ping command are included as a string, so, in order to extract only the first line from there, the string.match function is used. Then, the extracted result is substituted for the first_line variable.
For specifics of use of the string.match function, you should look at the Library Function Explanation, with the understanding here that if you write it as per this example the first line will be extracted.
The results are displayed in the 4th line.
Results are displayed using the print function. You can simply display such character strings using the print function.
The 5th line from the end is an end statement that displays the end of the if statement on the 2nd line.
Use of command arguments
With this script, the party to 'ping' is fixed at '192.168.100.100'. Next, let's try making it possible to specify the IP address when executing lua command.
[ping2.lua] download
With 'ping2.lua,' we made it possible to designate the IP address by an argument when running a script with the lua command. In the first line of the script, the 1st argument is substituted for the variable dst.
'arg' is a variable that expresses the argument specified by the command, and arg[1] is the first argument, arg[2] is the second argument and arg[N] Huniecam studio download free. becomes the Nth argument.
The 2nd line executes the 'ping' command.
Compared to 'ping1.lua,' the argument given to the rt.command function is changed. The string called ' ping -c 1 ' and variable dst are concatenated with the . signal and become one string.
The symbol .is called a 'string concatenation operator,' and this symbol can form a single string by concatenating two strings, before and after it.
The third line and lines thereafter are exactly the same as for 'ping1.lua.'
if~then~else
So, for 'ping2.lua,' the designation of the correct IP address for the argument of the lua command is a condition, but if the wrong argument is given as the IP address, what will happen?
Nothing will be displayed, will it? This is because the rt.command function, which is executing the 'ping' command, has failed due to a grammatical error. So, even in that case, let's try displaying something.
[ping3.lua] download
Ok, how was it this time?
An error string was displayed, wasn't it? In 'ping3.lua', the else section from the 6th line and 7th line are added to 'ping2.lua'.
In an if statement, when the value in the value of the formula between if and then is true, the statement after then is executed. Additionally, statements after else are executed when the value of the formula is false. This time the 'ping' is a grammatical error, and because the flag, which is the rt.command function return value, is false, the if statement executes the else section and displays the error message.
Function definition and call
Only one IP address could be specified with 'ping3.lua' on the command line. Next, let's set it to ping multiple parties.
[ping4.lua] download
So, let's try executing 'ping4.lua.'
In 'ping4.lua,' almost the same thing as 'ping3.lua' is enclosed by the function ping(dst)~end. In this way, a chunk of code enclosed by function~end is called a 'function.' In this case, the name ping is attached to the function, and afterward when this name is used to call the function, this chunk of code can be executed.
After a function has been defined with function~end it can be called at any time. The relationship between the function definition and the function call in 'ping4.lua' is as follows.
In a function call arg[i] is passed as a real argument to the ping function. The value of arg[i] is substituted for the provisional argument dst in the function definition and the function is executed.
arg[i] and dst are completely different variables, and this is an operation in which, before the function starts to execute, the value of arg[i] is merely substituted for dst. For this reason, during execution of the function, even if the value of dst is changed, there is no change in arg[i] on the function call side.
If there is a return statement in the function definition, then processing returns to the side that called the function. At such time, the formula value continued after the return is passed to the call side as a function value. With ping4.lua, for both success and failure, ping returns a string as a return value, and at the call side this is output as print.
There are several reasons for defining a code chunk as a function. One reason is that when the same process appears many times, that will be defined as a function and called when necessary. By collecting the same process in one place, it becomes clear that the same process is occurring, and when change is necessary, the changed places may be reduced.
Another reason is to give a name to the process and clarify the meaning. In this example, the code that runs 'ping' is given the name ping. Due to this, when this function is used later, it becomes possible to clearly understand that 'ping' is running.
Ttl Script Examples
Combining processes in a suitable unit as a function is very useful when explaining code to people, and afterward when code must be maintained. Let's use functions actively.
Ttl Script Examples Definition
Loops
Tera Term Macro Commands
With ping4.lua in order to process multiple IP addresses passed by the command line, loops with for statements are used. The relevant parts are extracted as follows.
Ttl Script Examples Python
In this for statement, while increasing the value of the variable i from 1 to #arg one by one, between do ~end, that is, print(ping(arg[i])) is executed. # means a length operator that returns the size of the array and the length of the string, and #arg represents the size of the arg array, so in this for statement, in the order of the IP addresses given at the command line, the function ping is called and its return value is output.
In this way, as statements for repeated execution, other than the for statement, there are the while statement and the repeat-until statement. When writing code with the same meaning as a for statement, a while statement and a repeat-until statement, the results will be as follows:
In the while statement, the block is executed while each conditional formula is fulfilled, and in the repeat-until statement, the block is executed as long as each conditional formula is unfulfilled. As the conditional formula is checked after execution of the block for the repeat-until statement, the block will be executed without fail at least once. Thus, in the example above, it is configured so that when the whole is enclosed by an if statement and when the #arg is 0, that is, if no argument is given to the command, the repeat-until statement is not executed.
Teraterm Ttl Script Examples
Although for statements, while statements and repeat-until sentences are sentences that can be repeated in the same way, there are situations that are suitable for use of the respective sentences, so use the appropriate repeating sentence.