Keep CT simple tutorial

Source: Internet
Author: User
Tags case statement ssh server
ArticleDirectory
    • Iii. Application
    • Iv. References
I. Overview

Trusted CT is a software tool used in UNIX systems for automated control and testing. It is made by Don libes and is an extension of the Tcl scripting language. It is used in interactive software such as telnet, FTP, passwd, fsck, rlogin, tip, ssh, etc. This tool uses UNIX pseudo terminals to package its sub-processes, allowing arbitraryProgramAutomated control through terminal access. You can also use TK tools to package interactive programs in the X11 graphic user interface.

We can use shell to implement simple control flow functions, such as loops and judgments. However, manual intervention is required for scenarios that require interaction. For example, when a common user uses the sudo command, the user needs to manually enter the password. Secondary CT can complete this automatic interaction task, without human intervention.Don libes, author of objective CT, made the following definition for objective CT when it was written in 1990: automated CT is a software suite for automatic interaction (Automated CT [is a] software suite for automatic interactive tools ). The system administrator can use it to create a script to provide input to commands or programs: Generally, these inputs need to be manually entered (For example, the execution of the sudo program mentioned earlier requires the user to enter the user password from the terminal.,Secondary CTYou canAccordingProgramPrompt SimulationStandardInputProvide program information for interactive program execution. You can even implement simple BBS chatbots.

Trusted CT is evolving. As time passes, it becomes more and more powerful and has become a powerful assistant to system administrators. Secondary CT requires TCLProgramming LanguageTo run the secondary CT on the system, you must first install TCL.

Ii. Working Principle At the simplest level, secondary CT works like a universal chat script tool. The chat script was first used in the uucp network to automate specific logon sessions when computers need to establish connections.
Chat scripts are composed of a series Keep CT-send pair Composition: Secondary CT WaitOutput Specific charactersGenerally, it is a prompt. If the prompt arrives, use send SendSpecific Response, For example, the following example uses the login CT-send pair to simulate the interactive input of the user name and password. # CT Wait" Login:"String. If" Login:"String, use send to respond Somebody(\ N is used to simulate carriage return)
Secondary CT "Login :"
Send"Somebody \ N "

# CTWait"Password:", Use send to respond111111
Secondary CT"Password :"
Send111111\ N"The preceding statements describe the simplest script operation mode of CT. 3. Application 3.1 automatically logs on to the SSH server

There are a lot of scripts for automatic login using CT, but there is no clear explanation. Beginners generally copy and add to favorites. However, I don't know why. This article uses a shortest example to illustrate the script principle.

#! /Usr/bin/CT
Set timeout30
Spawn SSH test @127.0.0.1
Secondary CT"Password :"
Send"123456 \ n"
Interact 1 .[#! /Usr/bin/expect] This line tells Code Use that shell for execution. The objective CT is similar to bash in Linux and CMD in windows. Note: This line must be in the first line of the script. 2. [set timeout 30] set the timeout time, in seconds. The timeout time of the secondary CT. The default timeout value is 10 s. 3. [spawn SSH test@127.0.0.1] spawn is a trusted CT internal command that can be executed only after entering the trusted CT environment. If there is no risk CT installed or the spawn command cannot be found directly executed in shell. This is like CD is a built-in shell command. If you leave the shell, you cannot execute CD. Its main function is to add a shell to the SSH running process to transmit interactive commands. 4. [risk CT "Password:"] The risk CT here is also an internal command of the risk CT script language. If you enter reverse CT under shell, the working interface of reverse CT will be displayed. This reverse CT is a shell command, and [reverse CT "password: "] CT is just a built-in command with the same name as the CT Script Name. This command is used to determine whether the output result contains a string of "Password:". If yes, it returns immediately and runs down. Otherwise, it waits until the time-out period ends, it is 30 seconds in 2. 5. [Send "123456 \ n"] Here is the interactive action, which is equivalent to the action of manually entering the password. The effect is the same as if you manually enter 123456 on the terminal and press Enter. 6. [interact] After the preceding command is executed, it remains in the interactive State. At this time, the trusted CT will hand over the control to the console. At this time, the manual operation will be performed and the trusted CT has been executed successfully. If this statement is not entered, the system immediately exits, instead of staying on the remote terminal. If you log on and run a command to exit, you can change it to [cT EOF]. The script is executed as follows:Gyl @ gyl:~$./ Test. Exp
Spawn SSH Test@127.0.0.1
Test@127.0.0.1 'S password:
Linux gyl 2.6.32-46-generic # 108-ubuntu SMP Thu Apr 11 15:55:01 UTC 2013 i686 GNU/Linux
Ubuntu 10.04.4 lts

Welcome to Ubuntu!
* Documentation: https://help.ubuntu.com/

New Release 'precise'Available.
Run'Do-Release-Upgrade'To upgrade to it.

Last login: Sat May 11 01:08:19 2013 from localhost
$3.2 encapsulate SCP statements to achieve automatic interaction between uploads and downloads#! /Usr/bin/CT

#
# Auto down/up files from/to server over SCP. Protocol
# Usage:
# Autoscp-down srcpath (the same as autoscp-down srcpath .)
# Autoscp-down srcpath dstpath (dstpath is based on $ PWD)
# Autoscp-up srcpath (the same as autoscp-up srcpath ~)
# Autoscp-up srcpath dstpath (dstpath is based on ~ On the server)
#

Set timeout100
Set cmd [lindex $ argv0]
Set option [lindex $ argv0]
Set srcpath [lindex $ argv1]
Set dstpath [lindex $ argv2]

# Parse command number
Switch--$ Argc {
2{
Set dstpath.
}
3{
}
Default {
Send_user"Usage: $ cmd option srcpath \ [dstpath \] \ n"
Exit
}
}

# Parse command and do it.
If {$ Option = "-Down" }{
Spawn SCP - R gyl @ 127 . 0 . 0 . 1 : / Home / Gyl / $ Srcpath $ dstpath
} Else {
If {$ Option = "-Up" }{
Spawn SCP - R $ srcpath gyl @ 127 . 0 . 0 . 1 : / Home / Gyl / $ Dstpath
}Else {
Send_user "Bad Arg: \" $ option \ "\ n"
Exit
}
}

# AutoFill Infomation
Secondary CT"Password :"
Send"123456 \ n"

# Exit
CT EOFSimilar to the command line parameters in C language, this script uses the switch-case statement and the IF-else statement to provide comprehensive functions. This script specifies four usage methods, and provides the corresponding prompt when it does not meet the usage requirements. The upload and download operations include dstpath and dstpath. There are four types;Autoscp-Down srcpath dstpath# Srcpath is based on the server user's home directory, and dstpath is based on the local current directory
Autoscp-Down srcpath# It is equivalent to autoscp-down srcpath.
Autoscp-Up srcpath dstpath# Srcpath is based on the local current directory, and dstpath is based on the server user's home directory
Autoscp-Up srcpath# It is equivalent to autoscp-up srcpath ~ Note: Because this script uses the SCP upload/download function, it takes a long time to operate a file. If the timeout value is too small, the data transmission will end when the timeout value reaches, and the program will exit. Therefore, the timeout value should be adjusted according to the file size. Iv. Refer to kernel CT manual Chinese edition Linux Automatic Logon system execution command <2> expectshell_scp kernel CT Linux Shell + kernel CT: Batch SCP script Tool

From Weizhi note (wiz) time = 17:24:26

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.