Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Automatically log in to expect script instance for graphic tutorials

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Brief introduction:

The explanation of 1.expect

two。 Automatic login script for instance operation

Expect explanation

Expect allows us to log in to remote machines automatically and execute commands remotely. Of course, automatic login and automatic remote execution of commands can also be achieved by using key authentication without a password. But when key authentication cannot be used, there is nothing we can do about it. Therefore, at this time, as long as you know the account number and password of the other machine, you can log in and remote commands through the expect script.

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

Send: used to send strings to the process

Expect: receives a string from a process

Spawn: start a new process

Interact: allow user interaction

1. Send command

The send command takes a string argument and sends it to the process.

Expect1.1 > send "hello world\ n" hello world

2. Expect command

The expect command is the opposite of the send command, and expect is usually used to wait for feedback from a process. Expect can receive either a string parameter or a regular expression parameter.

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

These two lines of code mean: from standard input medium to hi and newline keys, output hello there to standard output

3. Spawn command

All of the demo above interacts with standard input and output, but we want him to interact with a certain process. The spawm command is used to start a new process.

Set timeout-1spawn ftp ftp.test.com / / opens a new process where the user connects to the remote ftp server

When the expect "Name" / / process returns Name

Send "user\ r" / / enter to the process

Anonymous\ rexpect "Password:" / / when the process returns Password:

Send "123456\ r" / / enter don@libes.com into the process\ r

When the expect "ftp >" / / process returns ftp >

Send "binary\ r" / / enter binary into the process\ r

When the expect "ftp >" / / process returns ftp >

Send "get test.tar.gz\ r" / / enter get test.tar.gz into the process\ r

4.interact

Up to now, we can automate a lot of tasks by combining spawn, expect and send. But how to get people to intervene in the process at the right time. For example, after downloading the ftp file, you can still stay on the ftp command line so that subsequent commands can be executed manually. Interact can achieve these goals. The following demo allows user interaction after automatically logging in to ftp.

Spawn ftp ftp.test.com

Expect "Name"

Send "user\ r"

Expect "Password:"

Send "123456\ r"

Interact

2 hosts

Server: back: 192.168.19.134

Client: other:192.168.19.135

Server-side operation

1. Before using expect, you need to install expect:

Yum install-y expect

Install ssh

Yum install openssh-server-y

Yum-y install openssh-clients

two。 Create a new automatic login script file

Format:

Spawn ssh-l username 192.168.1.1

Expect {

"(yes/no)?" {send "yes"; exp_continue}

"password:" {send "AAAA"}

}

Interact

Vim login.expect

#! / usr/bin/expect

# about login

# written by lizheng

Set host "192.168.19.135"

Set passwd "lizheng123"

Spawn ssh root@$host

Expect {

"yes/no" {send "yes\ r"; exp_continue}

"password:" {send "$passwd\ r"}

}

Interact

3. Change permissions

Chmod + x login.expect

4. Execute script statement

. / login.expect

Enter the password of the client to log in

5. Quit

Logout

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.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report