Interactive command expect

Source: Internet
Author: User

The shell is powerful though. But seemingly unable to complete the operation of the interactive command, instance SSH host if host and the machine does not join the trust. The time manually entered requires password.

In this case, you can use expect support.


Here is a sample example to illustrate the function and use of expect:


Function: Run the same instruction on the cluster machines such as cluster IP: 192.168.6.1~192.168.6.10. Need to log in and run the mkdir ttji_314 command on these 10 machines at the same time as the TT user.

First define a hostlist file:

192.168.6.1192.168.6.2192.168.6.3192.168.6.4192.168.6.5192.168.6.6192.168.6.7192.168.6.8192.168.6.9192.168.6.10


Then define a expect script complete on a single machine Run command: ssh_comm.sh

#!/usr/bin/expectset host [lindex $argv 0]set command [lindex $argv 1]set password 123456set username ttset Timeout 1send_ User "connect to $host ... \ n" Spawn ssh-l $username $hostexpect "Password:" send "$password \ r" expect "#$" send "$command \ r" E Xpect "#$" send "exit\r" #interactexpect EOF


A few notes:

1 First line plus/usr/bin/expect

2 spawn: followed by shell commands that need to be run 3 Expect: Only spawn Run command results will be captured expect, because the spawn will start process, only the relevant information of the process will be captured. Mainly includes: The standard input of the lifting The following information, EOF and timeout.

4 send and Send_ User:send will send the required information in the expect script to spawn launch already. 5.set timeout The instructions after 1 will all wait for 1s.
finally finished bulk function: command_all.sh

#!/bin/bashfunction use () {        echo "========================"        echo "./command_all.sh hostlist Command"        echo "========================"}if [$#! = 2] Then use        else                Echo ' Welcome to use:\n ' fifile=$1command=$ 2echo $fileecho $commandwhile Read Linedo    #echo $line    ./ssh_comm.sh $line "$command" Done < $file

use: ./command_all.sh hostlist "mkdir ttji_314"

There is a problem: when you log in using the root user in the expect script, there seems to be no concept of timeout, and the command returns immediately after running;

And with ordinary users will have such a manifestation.

If you have an understanding of the students please leave a message, thank you very much ~



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Interactive command expect

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.