Some minor problems in the first use of Expect, SCP and Net::scp::expect, Net::ssh::expect

Source: Internet
Author: User

I've learned some shells before, but not very deep, and the time to write code is not much. Shortly before the shell was more thin, leader assigned a task to write a script with the shell to upload the Redis source tarball from one server to another server, while the command on the server, unzip the source package and start the Redis service on another server.

Although some shells have been learned, the SCP and expect that need to be used in this task are the first to be used, and some problems have been encountered, so here is a summary.

The modern shell provides minimal control (start, stop, and so on) to the program, leaving the interactive features to the user. This means that there are programs that you cannot run non-interactively, such as passwd. The simple point is that you have to manually enter the password, the shell does not provide you with the password directly in the program to do a one-time approach.

It is precisely because of this, expect was born.

Note that expect is independent of the shell, or that they are not a single thing at all (but both are powerful tools under Linux). at that time I was because the expect is a shell supplement, so in the expect used a lot of shell syntax, hehe!!! You know the result.

I am here to briefly say I use the basic grammar of the expect, as well as through the pit, at the same time if you have the desire to learn, I will be in the back to provide a number of reference to my page, so that we can learn in depth.

    1. The simplest #!/usr/bin/env sh and #!/usr/bin/env expect are lazy to say;
    2. Second, in expect, the number of incoming parameters is accessed using $ARGC, but $# in the shell. Although it is simple, the following parameters in the shell are still well remembered:

  

3. Set variable in shell, $a = "Hello World", set a "Hello World" in expect

4. View the value of the variable in expect: Send_user "$a";

5. Execute shell program in expect: Spawn <shellCommand>

6. View the values of the parameters passed in expect: [lindex $argv 1] [lindex $argv 2] [lindex $argv 3] ... [Lindex $argv N]

7. Particularly useful: expect and send for interaction. I did not learn too deeply, only to give an example:

Spawn ssh-l username 192.168.1.1

Expect "*password:"

Send "ispass\r"

It's almost like this! Special attention is:send the password after the last must be with a \ r, or you wait for the wrong!!!

Just come here and say that I met a big pit-the situation is this, in order to improve the code reuse, the tutor is let me do not put what password ah this kind of write dead, or pass the parameter or set the environment variable value, I chose the method of parameters, such as the following program:

hello.sh

#!/usr/bin/env expect

Set host [lindex $ARGV 1]

Set passwd [lindex $argv 2]

Spawn ssh-l username $host

Expect "*password:"

  Send "$passwd"

The command executed is this:./hello.sh "192.168.1.1" "ispass\r"

All right, TMD. Have been wrong, do not remember the specific report what is wrong, as if the prompt password error. How can it be wrong, tangled, tangled ..... For a couple of hours!!! I don't know where it went wrong.

Later saw this document (HTTP://BLOG.ITPUB.NET/27042095/VIEWSPACE-745589/) The first picture, suddenly dawned Ah!!! Fix it quickly.

hello.sh

#!/usr/bin/env expect

Set host [lindex $ARGV 1]

Set passwd [lindex $argv 2]

Spawn ssh-l username $host

Expect "*password:"

  Send "$passwd \ r"

  Execute command:./hello.sh "192.168.1.1" "Ispass"

8. This is important to note: If you are using SSH to login to a server in expect, you want to go directly to that server (the simple point is that you are now in the server long operation, see the server file) must use interact

  Let's give an example:

#!/usr/bin/ssh192.168. 1.1  "password: " "Ispass\r" Interact
Expect EOF

  9. The program! Try to have expect EOF and exit a little better.

10. The largest pit came, the if usage of the expect:

If {< condition judgment >} {<commands> ...}
Perhaps you did not look closely:
    1. If is followed by curly braces {}, not ();
    2. Do you create a space between if and the curly braces that follow it?

Some minor problems in the first use of Expect, SCP and Net::scp::expect, Net::ssh::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.