Linux expect detailed (SSH automatic login)

Source: Internet
Author: User
Tags ftp file ftp login remote ftp server

Shell script implementation SSH Automatic login remote Server example:

#!/usr/bin/expectspawn ssh [email protected]expect "*password:"send "123\r"expect "*#"interact 

Original link: http://www.xuanhao360.com/linux-expects/

Expect is a command to handle interactions . With expect, we can write the interactive process on a script so that it is done automatically. The image of the said, SSH login, FTP login and so on are consistent with the definition of interaction . Here we first ask a question, then introduce the basic knowledge of the four commands, and finally propose a workaround.

Problem

How do I ssh from machine A to machine B, and then execute the command on machine b? How do I automate it?

Four commands

The most critical four commands in expect are send,expect,spawn,interact.

send:用于向进程发送字符串expect:从进程接收字符串spawn:启动新的进程interact:允许用户交互
1. Send command

The Send command receives a string parameter and sends the parameter to the process.

expect1.1> send "hello world\n"hello world
2. Expect command (1) Basic knowledge

The expect command is just the opposite of the Send command, and expect is typically used to wait for feedback from a process. Expect can receive a string parameter, or it can receive a regular expression parameter. In conjunction with the Send command above, we can now look at one of the simplest interactive examples:

expect "hi\n"send "hello there!\n"

These two lines of code mean: After the standard input medium to hi and the newline key, output hello there to the standard output .

tips: $expect_out(buffer)存储了所有对expect的输入,<$expect_out(0,string)>存储了匹配到expect参数的输入。

For example, the following programs:

expect "hi\n"send "you typed <$expect_out(buffer)>"send "but I only expected <$expect_out(0,string)>"

When entering in the standard input

testhi

Yes, the result of the operation is as follows

you typed: testhiI only expect: hi
(2) Mode-action

The most commonly used syntax for expect is the mode-action from the TCL language. This syntax is extremely flexible, so let's explain each of the various syntaxes below.

Single branch mode syntax:

expect "hi" {send "You said hi"}

After matching to Hi, it will output "you said HI"

Multi-branch mode syntax:

expect "hi" { send "You said hi\n" } "hello" { send "Hello yourself\n" } "bye" { send "That was unexpected\n" }

Executes the corresponding output when matched to any one of the Hi,hello,bye strings. Equivalent to the following wording:

expect {"hi" { send "You said hi\n"}"hello" { send "Hello yourself\n"}"bye" { send "That was unexpected\n"}}
3. Spawn command

All of the demos above are interacting with the standard input and output, but we want him to interact with a process. The SPAWM command is used to start a new process. The send and expect commands after spawn are interactive with the spawn open process. In conjunction with the send and expect commands above, we can look at the more complex pieces of the program.

SetTimeout-1SpawnFtpFtp.Test.ComOpens a new process where the user connects to the remote FTP serverExpect"Name"//Process returns name when send  "User\r" //to process input Anonymous\rexpect  "Password:" //process return Password: When send  "123456\r" //to process input [email  protected]\rexpect  "ftp>" //when the process returns to Ftp> send  "Binary\r" //to process input binary\ Rexpect  "ftp>" //process returns ftp> when send  "get Test.tar.gz\r" //to process input get test.tar.gz\r  

The purpose of this code is to log on to the FTP server FTP ftp.uu.net and download the file test.tar.gz on the server in binary mode. There are detailed comments in the program.

4.interact

So far, we have been able to combine spawn, expect, and send automation to accomplish many tasks. But how to get people to intervene in the process at the right time. For example, when downloading the ftp file, you can still stay in the FTP command line state, in order to manually execute subsequent commands. Interact can achieve these goals. The following demo allows users to interact after automatically logging on to FTP.

spawn ftp ftp.test.comexpect "Name"send "user\r"expect "Password:"send "123456\r"interact
Workaround

The above mentioned:

How do I ssh from machine A to machine B, and then execute the command on machine b? How do I automate it?

The following script implements a login from machine A to machine B, then executes the PWD command on machine B and stays on the B machine, waiting for user interaction. Please refer to the above for specific meanings.

#!/home/tools/bin/64/expect -f set timeout -1 spawn ssh $BUser@$BHost expect "*password:" { send "$password\r" } expect "$*" { send "pwd\r" } interact

 

Linux expect detailed (SSH automatic login)

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.