Automatic Interaction of Linux scripts

Source: Internet
Author: User

Summary of automatic remote host login methods in Linux scripts

 

 

 

For convenience, I wrote a script to automatically log on to the remote machine by Using SSH, as shown in the following code. This script requires the installation of the trusted CT package.

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

#! /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 commands such as "which spawn" to find the spawn command. For example, in windows, DIR is an internal command that comes with shell. You cannot find an executable file of dir.com or dir.exe. 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 output result contains a string of "Password:". If yes, return immediately; otherwise, return after a period of time, the waiting duration is the last 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

 

 

--------------------------------------------------------------------------------
How to perform automatic interaction in shell scripts

When writing the batchcompute code for shell scripts, users are generally only required to enter a limited number of parameters when calling the shell, and then the shell will execute the batchcompute job. Before the batchcompute job is completed, no

Any interruptions. However, some Linux commands require human-computer interaction. The commands basically interact with users on the terminal in the form of one question and one answer, such as passwd and smbpasswd.

Enter the username parameter when you press Enter. The system requires that you enter the password, while smbpasswd also requires that you enter the password again. In this way, you need to manually intervene twice before the command can be executed.

I recently wrote a small tool. This problem happened. The user asked for a shell. After Entering username and port number, the system automatically created Linux users, Set passwords, and enabled network neighbors.

All accounts have the same username as passwd. After you input shell commands and press enter, the system will no longer interact with the system.

All operations.

However, since both passwd and smbpasswd commands can be completed only after user intervention, is there any way for the system to simulate user input without user intervention? The answer is yes.

Use the Linux pipeline technology, use the "Echo" command for standard output, and then use the Pipeline character "|" to convert it to the standard input of passwd and smbpasswd, thus simulating man-machine interaction. The specific implementation is as follows:

Assume that the user calls the shell script as follows:

[Root @ myhost] #./myshell. Sh Username

In the script:

#! /Bin/sh
# Usage: myshell. Sh Username
Username = $1;
Password = $1;

# Add User and set default password
Useradd $ username;
# Specify to accept standard input by setting the-stdin parameter;
Echo $ password | passwd--stdin $ username;

# Add Samba user and set default password
# Specify the password in quiet mode by setting the-S parameter
(Echo $ password; echo $ password) | smbpasswd-S-A $ username;

In this way, the standard output is converted to the standard input of some commands through the pipeline technology of Linux, simulating manual input, thus shielding man-machine interaction and achieving completely automatic

.

 

Example:
#! /Usr/bin/CT-F

Set Password

Spawn SCP username @ target IP Address: path of the copy file to store the local file

Set timeout 300 (s)

Login CT "username @ Destination IP's password :"

Set timeout 300

Send "$ password/R"

Set timeout 300

Send "Exit/R"

CT EOF

--------------------------------------------------------------------------------

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. In shell programming, there is such a usage (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. "

The redirection operator command <Delimiter is a very useful command. Shell uses all the content after the delimiter until the next same delimiter as input, and encounters the next delimiter, shell knows 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.

 

 

**************************************** ****

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.

The simplest example is to create a user. I have created 100 users and the initial password is 123. If passwd username is used to interactively enter the password, you should not go crazy, but the script should

How to write it? Enter the password 123 to the passwd command?

This article collects and compares common interaction methods.

Automatic Interaction Method 1: using the built-in parameters of the command, the standard input is used as the manual input content.

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 chapt in Linux and Unix shell programming guide ).

5.7): "command <delimiter reads data from the standard input until the delimiter Delimiter is encountered. "

The redirection operator command <Delimiter is a very useful command. Shell uses all the content after the delimiter until the next same Delimiter is input.

The next separator, and 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:

[Yjwan @ test ~] $ FTP-I-n 192.168.21.46 <EOF

User dbftp dbftp101

Ls

EOF

The result is as follows:

Connected to 192.168.21.46.

220 export jail FTP Server (version 6.00ls) ready.

331 Password required for dbftp.

230 user dbftp logged in.

Remote system type is UNIX.

Using binary mode to transfer files.

229 entering extended passive mode (||| 54281 |)

150 opening ASCII mode data connection for '/bin/ls '.

Total 8847424

-RW-r -- 1 dbftp WWW 9055318991 Aug 28 ewiz90. SQL _ 2010-08-29

226 transfer complete.

221 goodbye.

Test shows that the above Code uses the account name dbftp and password dbftp101 to successfully log on to the FTP server and enter the directory. ls will output the files in the current directory.

Note:

1. Add-I here; otherwise, it will fail !!

The-I parameter used by FTP ends the interactive input, so the user password and operation method can be automatically entered in text mode.

If you do not need the-I parameter, you must enter the password manually !! The Automatic Login requirement is not met.

You can find this parameter through man FTP.

-I: Disable the interactive prompt in Multi-file transfer. Refer to the prompt, mget, mput, and mdelete subcommands to get the description of the prompt in Multi-file transfer.

-N prevents Automatic Logon during the initial connection. Otherwise, the FTP command searches for $ home/. netrc logon item, which describes the logon and initialization processes of the remote host. Refer to user sub-life

.

2. Password-based commands generally contain a parameter that allows you to input, input, and operate the user password.

Parameters of other common automatic login commands

1) passwd command

In Linux, passwd has parameters.

-- Stdin

This option is used to indicate that passwd shoshould read the new password from standard input, which can be a pipe.

So the method for changing the user password in Linux is

Echo password | passwd-stdin User Name

FreeBSD does not have the preceding parameters.

The method is Echo passwd | PW mod User username-H 0.

2) smbpasswd

-S

This option causes smbpasswd to be silent (I. e. Not issue prompts)

And to read its old and new passwords from standard input, rather

Than from/dev/tty (like the passwd (1) program does). This option

Is to aid people writing scripts to drive smbpasswd

3) sudo

In the shell script, you need to use the root command to execute the command:

Sudo automatically enters the password

Echo "password" | Sudo-s netstat-tlnp

-S

The-s (stdin) option causes sudo to read the password from the standard input instead of the terminal device.

3 standard input can also be written in the following two ways:

You can write the following content to a text

Then FTP-I-n IP <text

Or echo ".." | FTP-I-n IP

Yes!

Because they all meet the condition that "there is a standard input ".

4 SSH automatic login does not have such automatic password input parameter

Generally, the Public Key/private key is used for automatic logon.

 

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.