Linux Shell Automatic Interaction

Source: Internet
Author: User
Linux Shell Automatic Interaction
Favorites

In order to do this, I wrote a script to automatically log on to the remote machine by Using SSH. The script is as follows:

======================================

#! /Usr/bin/CT

Set timeout 30

Spawn ssh-l root 192.168.1.117

Reset CT "Password :"

Send "nopasswd/R"

Interact

======================================

Set timeout 30

Set the timeout value in seconds.

Spawn ssh-l root 192.168.1.117
Spawn is the internal CT command that can be executed only after entering the CT environment. If the CT is not installed or the spawn command cannot be found directly in the default shell
. Therefore, do not use "which"
Spawn and other commands to find the spawn command. For example, in windows, DIR is an internal command, which is provided by Shell and you cannot find a dir.com
Or dir.exe executable file. Its main function is to add a shell to the SSH running process to transmit interactive commands.

Reset CT "Password :"
The secondary CT is also an internal command of secondary CT. The shell command of secondary CT is the same as the internal command, but it is not a function. This command is used to determine whether the last output is complete.
Whether the string "Password:" is included in the result. If yes, the string is returned immediately. Otherwise, the string is returned after a period of time. Here, the waiting duration is 30 seconds.

Send "nopasswd/R"

The interaction is performed here, which is equivalent to the password input.

Tip: do not add "/R" to the end of the command string. If an exception occurs, check it.

Interact

After the execution is complete, the interaction status is maintained, and the control is handed over to the console. At this time, you can perform manual operations. If this statement is not entered, the system will exit after logon, instead of staying on the remote terminal. If you log on and run a command to exit, you can change it to [cT EOF 〕

Note: If you execute the keep CT script in crontab, the last sentence cannot be interact. It should be an exact CT EOF

Reference: http://my.unix-center.net /~ Xiaoshe/Tag/expectcrontabmysql regularly backs up the database/

The following describes other methods for automatic shell interaction

SRC: http:// OS .51cto.com/art/200912/167898.htm

Do you know about Linux? Are you an application of Linux? If you want to learn about Linux, you may encounter the problem of automatic Linux Shell interaction. Here we will introduce how to solve the problem of automatic Linux Shell interaction. Here we will share it with you.

I. background

Shell scripts can save a lot of time in processing automatic or large tasks. By creating a command list for processing tasks, using variables, conditions, arithmetic, and loops to quickly create scripts to complete the corresponding work is much more time-and effort-saving than typing the next command in the command line.

However, sometimes we may need to implement interaction functions with interactive programs such as FTP and Telnet servers. At this time, we need to use the automatic shell interaction function, this article collects three commonly used Automatic Interaction methods and compares and summarizes them.

Ii. Requirements

Requirement 1:
Log on from one Linux machine FTP to another Linux machine and disable it after a series of operations. You are too reluctant to manually enter the password every time.

Requirement 2:
Change the password of the logon user, so you do not have to enter the new and old passwords every time.

Requirement 3:
If you want Su to log on to the root account automatically, you are too reluctant to enter the root password every time.

Iii. debugging environment

Terminal: securecrt

System: WINXP, centos 4.4 (VMware)

Shell: Bash

Note: There are many shell types. Class B shell (Sh, Bash, KSh) have similar behavior; Class C shell (CSH, tcsh) have similar behavior, and shell such as zsh and RC, the debugging environment in this article is Bash.

Iv. Automatic Interaction method 1

The key to automatic interaction is the automatic input of interaction information. First, we think of file redirection. This is a usage in shell programming (refer to the Linux and Unix shell programming guide ).
Chapt 5.7): "command <delimiter reads data from the standard input until the delimiter Delimiter is encountered. "

Redirect operator command <
Delimiter is a very useful command. Shell uses all the content after delimiter and before the next same delimiter as input, and encounters the next demarcation
Shell will know that the input is over. The most common delimiter Delimiter is EOF. Of course, you can define it as another character.

You can use this method to automatically log on to FTP as required by requirement 1 and perform a series of operations. The Code is as follows:

1 .#! /Bin/bash
2. FTP-I-n 192.168.167.187 <EOF
3. User hzc 123456
4. pwd
5. CD Test
6. pwd
7. Close
8. Bye
9. EOF

The test showed that the above Code successfully logged on to the FTP server with the account name hzc and password 123456, and entered the directory, printed out the PWD.

V. Automatic Interaction method 2

In requirement 2, non-interactive login password change is required. method 1 cannot be used.

At this time, we think of another Automatic Input Method of interactive information, pipeline, which can be achieved through ECHO + sleep + |.

1 .#! /Bin/bash
2. (echo "curpassword"
3. Sleep 1
4. Echo "newpassword"
5. Sleep 1
6. Echo "newpassword") | passwd

Test passed. Run this script to change the current user's curpassword to newpassword.

Vi. Automatic Interaction method 3

In requirement 3, automatic logon to the root account is required. If methods 1 and 2 are attempted, the error code "standard in must be a tty" is displayed.

At this time, I tried to find external help. A shell tool called javasct can implement this function. In fact, javasct is a tool specifically used to implement automatic interaction. The syntax of javasct can refer to the relevant information. The Code is as follows:

1 .#! /Usr/bin/CT
2. Spawn su Root
3. Keep CT "Password :"
4. Send "123456/R"
5. Exact CT EOF
6. Exit

Test passed. Run this script to log on to the root user directly from the current user.

VII. method summary

Method 1 (redirection) is simple and intuitive, and often has practical applications. However, it has limited functions in the field of automatic interaction.

Method 2 (pipeline) is simple and intuitive. Sometimes it can show powerful automatic interaction without sleep, but sometimes it is helpless.

Method 3 (CT) is the most powerful in terms of functions. CT was originally born to realize the automatic interaction function, but the disadvantage is that it is difficult to install the CT package in an embedded environment.

Each of the three methods has its own advantages and disadvantages. If the application is good, automatic Linux Shell interaction can be completed.

Related Article

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.