Sometimes we need to send the SSH command to the server in bulk, but it is possible that some of the servers are newly added, and we have not configured SSH encryption, this time will prompt us to enter yes/no or password, etc., expect script command is used to prompt these, Automatically enter the appropriate text for us
Expect script
See a shell script first to enable automatic SSH connection
# !/usr/bin/expect spawn ssh 192.168.1.241expect password send " 123456\r " expect ]# {send " ls-la\r " }interact
Note that the first line uses the #!/usr/bin/expect
- Spawn is used to start a new process.
- Expect "password", note that this is the expect script inside the command, which is used to wait for process feedback, can accept strings and regular expressions, which means that the output of the spawn-initiated process as the input of the expect command, If the password script is included, it will be output to the standard output 123456\r
, note \ r represents a line break, and it's a good idea to enter a command line with us.
- Send: Sends the interaction value instead of our manual input
- Then we entered 241 of the server, the window will output [[email protected] ~]#, with our]# match, then output to the console Ls-la command and newline. Here our expect is followed by curly braces, with the effect of writing on two lines above. Can be understood as another way of writing.
- The role of interact is very special, the representative waits for the Spawn command to end, and stays on the 241 service side to continue the interaction, if there is no this, then it is possible to expect corresponding to the match has not executed the send has already ended. Corresponding we can use expect EOF, instead of interact representative waiting for spawn to exit after the end (EOF is sent to expect after the spawn process is finished )
Multi-branch syntax
The above expect belong to a single branch mode, the representative of the match this one, if the match is not, then you have to input, but we may have different hints at the first time, this time we need to use the multi-branch syntax
Expect as long as the match to AAA or password one of them will be output.
#!/usr/bin/expectSpawn ssh 192.168.1.241Expect {"AAA"{Send"bbb\r"} "Password"{Send"nf123456\r"}}expect"]#"{Send"ls-la\r"}interact
Expect command-line arguments
The expect script feature above may have been reached, and to avoid having many expect scripts, I would recommend using the expect command
1 #!/bin/bash2 3Server="192.168.1.241"4Passwd=nf1234565 6Expect-c"7 set timeout-1;8 spawn ssh $SERVER;9 Expect {Ten \"Yes/no\"{send \"Yes\r\"; exp_contine;} One \"Password:\"{send \"$PASSWD\r\"; } A }; - - expect \"]#\ "{Send \" Ls-la \r\ "}; theExpect \"]#\"{Send \"Exit \r\" }; - expect EOF; - "
The above shell function and the expect script implementation of the same function, are logged in via SSH input, Ls-la command
- Set Timeout-1 setting Time-out
- Expect need to add-C after
- Expect commands are enclosed in double quotes, note that
- Inside if there is "need to use \" escaped.
- Line 15th represents the end of the Ls-la command and sends an exit command, which generally needs to be added to prevent blocking
- Expect EOF match spawn end
SSH Bulk Free demo
#!/bin/bashservers="192.168.1.241 192.168.1.242"PASSWD="123456"functionsshcopyid{Expect-C"set timeout-1; Spawn Ssh-copy-id $; Expect {\"Yes/no\"{send \"Yes\r\"; exp_contine;} \"Password:\"{send \"$PASSWD\r\"; exp_continue;} }; Expect EOF; "} forServerinch $SERVERS DoSshcopyid$server Done
Shell basic Knowledge Portal
Linux expect command for bulk SSH encryption