In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Expect study notes and examples are quoted from: http://wenku.baidu.com/view/b65e103610661ed9ad51f374.html
1. Expect is based on tcl, so many grammars are similar to tcl. The basic syntax is as follows: 1.1 the first line plus / usr/bin/expect1.2 spawn: followed by shell commands to be executed, such as spawn sudo touch testfile1.3 expect: only commands executed by spawn will be captured by expect, because spawn will start a process and only the relevant information about this process will be captured. It mainly includes: standard input prompt information, eof and timeout. 1.4 send and send_user:send send the information needed in the expect script to the process started by spawn, while send_user simply echoes the message sent by the user, similar to echo in shell.
two。 A small example for setting up an account under linux: filename: account.sh, which can be executed using. / account.sh newaccout 1 #! / usr/bin/expect23 set passwd "mypasswd" 4 set timeout 6056 if {$argc! = 1} {7 send "usage. / account.sh\ $newaccount\ n" 8 exit9} 1011 set user [lindex $argv [expr $argc-1]] 1213 spawn sudo useradd-s / bin/bash-g mygroup-m $user1415 expect {16 "assword" {17 send_user "sudo now\ n" 18 send "$passwd\ n" 19 exp_continue20} 21 eof22 {23 send_user eof\ n "24} 25} 2627 Spawn sudo passwd $user28 expect {29 "assword" {30 send "$passwd\ n" 31 exp_continue32} 33 eof34 {35 send_user "eof" 36} 37} 3839 spawn sudo smbpasswd-a $user40 expect {41 "assword" {42 send "$passwd\ n" 43 exp_continue44} 45 eof46 {47 send_user "eof" 48} 49}
3. Note:
Line 3: method of assigning values to variables; line 4: by default, timeout is 10 seconds; line 6: the number of parameters can be obtained in $argc; line 11: parameters are stored in $argv, such as [lindex $argv 0]; and if you need to calculate, you must use expr, if you calculate 2-1, you must use [expr 2-1]. Line 13: use spawn to execute a shell command, the shell command can be adjusted according to specific circumstances; there is an article that sudo to add-S, after the actual test, do not need to add-S; line 15: in general, if you do two expect in a row, then it is actually executed serially, with. There must be a space or TAB interval between expect and "{" directly, otherwise there will be trouble and an error will be reported that the structure in the invalid command name "expect {" example is executed in parallel, mainly to see which one is matched; in this case, if you write it in serial, that is, expect "assword" send "$passwd\ n" expect eofsend_user "eof", it will run correctly the first time, because the password is required for the first time sudo. However, in the second run, since the password has already been typed (by default, the time for re-entering the sudo password is 5 minutes), the user will not be prompted to enter the password, so the first expect will not match the assword, and it must be noted that if the spawn command is asked interactively but the expect does not match, then the program will wait according to the setting of timeout However, if the spawn issues the eof directly, as in this case, then the expect "assword" will not wait and execute the expect eof directly. At this point, expect: spawn id exp6 not open will be reported, because there is no spawn executing, and the subsequent expect script will no longer be executed for this reason; so for cases with uncertain command branches such as sudo, it is best to use a parallel approach; line 17: just a user prompt and can be deleted; line 18: send password to the spawn process Line 19: causes the spawn process to match the next interaction prompt after matching; line 21: eof must be matched, and eof; will be sent to expect at the end of the spawn process. If it does not match, sometimes it can run, such as how many seconds after sleep goes to spawn the next command, but do not rely on this behavior, it is likely that it will work today, but it will not work tomorrow.
4. The other example below is so special that you cannot expect eof in the whole process: 1 #! / usr/bin/expect23 set timeout 30440spawn ssh 10.192.224.2245 expect "password:" 6 send "mypassword\ n" 7 expect "* $" 8 send "mkdir tmpdir\ n" # remote execution commands are sent using send instead of spawn9 expect "* $" # Note this place should match the environment variable PS1 on the operating system, especially if there is PS1 and spaces Be sure to add the space in expct "* $". If you don't, you're screwed. I tried. This example is actually through ssh to log in to the remote machine, and create a good directory on the remote machine, we see that we did not go to expect eof after entering the password, this is because the spawn of ssh does not end, and manual operation ssh will not actually end itself unless you exit; so you can only expect bash the prompt, of course, it can also be the machine name, etc., so that you can remotely create a directory. Note that please do not use spawn mkdir tmpdir, which will end the last spawn, that is, ssh, and your tmpdir will be created locally. Of course, in practice, you may be asked to confirm ssh key, which can be processed through parallel expect, without much detail.
5. Feel that bash is already very powerful in many cases, so maybe you only need to master these when using expect, and you can go to google again if you use others.
Source code picture:
6\ example: the following script completes the scp task for a single server.
10: spawn scp $src_file $username@$host:$dest_file 11: expect {7: cat $list_file | while read line 13:. / expect_scp $host_ip $username $password $src_file $dest_file 15: done
The picture of the reference code is as follows:
Very simple code that specifies three parameters: the location of the list file, the local source file path, and the remote host destination file path. It is important to note that the list file specifies the remote host ip, user name, and password, which needs to be written in the following format:
IP username password
Separated by a space or tab key, the information of multiple hosts needs to be written to multiple lines of content.
This specifies the information for the two remote hosts. Note that if there are special characters such as "$" and "#" in the remote host password, you need to precede these special characters when writing the list file, otherwise expect will enter the wrong password during execution.
For this shell script, save it as a batch_scp.sh file, put it in the same directory as the expect_scp file and list file (defined as hosts.list file) you just saved, and enter the command in the following way when executing:
1.jpg
2.jpg
3.jpg
1.jpg
2.jpg
11.jpg
12.jpg
13.jpg
14.jpg
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.