Shell automatic interaction method

  
 

Do you know Linux? Are you a Linux system application? If you want to learn Linux, you may encounter the Linux Shell automatic interaction problem, here will introduce the Linux Shell automatic interaction solution, here to share with you. First, the background shell script can save a lot of time in dealing with automatic loops or large tasks. By creating a list of commands for processing tasks, using variables, conditions, arithmetic, and loops to quickly create scripts to complete the work, this is better than It takes a lot of time and effort to type commands one by one on the command line. But sometimes we may need to implement interaction with interactive programs such as ftp, telnet server, etc. At this time we need to use the shell's automatic interaction function, this article collects three commonly used automatic interaction methods, and compares them. And summary. Second, the demand Demand 1: From a Linux machine ftp login to another Linux machine, after a series of operations closed, too lazy to manually enter the password each time. Requirement 2: Change the login user password, too lazy to enter new and old passwords every time. Requirement 3: I want su to log in to the root account automatically. I am too lazy to enter the root password every time. Third, the debugging environment terminal: SecureCRT system: WinXP, CentOS 4.4 (VmWare)

Shell: bash Note: There are many kinds of shell, B class SHELL (sh, bash, ksh) behavior is similar; C class SHELL (csh, tcsh) behavior is similar, there are shells such as zsh and rc, the debugging environment of this article is bash. Fourth, the automatic interaction method The most critical part of the automatic interaction is the automatic input of interactive information, first think of file redirection, there is such a usage in shell programming (refer to Linux and UNIX SHELL programming guide chapt 5.7): "command < ;< delimiter reads from standard input until it encounters the delimiter delimiter. The " redirection operator command << delimiter is a very useful command, the shell will take all the content before the delimiter delimiter until the next same delimiter as input, encounter the next delimiter, the shell will I know that the input is over. The most common delimiter delimiter is EOF, and of course you can customize it to other characters. For the automatic login ftp required by requirement 1, and for a series of operations, this method can be used for automatic interaction. 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 Tests can be found, the above code uses the account name hzc, password 123456 successfully logged into the ftp server, and entered the directory, printed out pwd. V. Automatic Interaction Method 2 Requirement 2 requires a non-interactive way to change the login user password. Try Method 1 and cannot be implemented. This is another automatic input method that associates interactive information, pipeline, via echo + sleep +

Copyright © Windows knowledge All Rights Reserved