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

How to implement automatic login script in expect in Linux

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to realize the automatic login script of expect in Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Expect

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.

Introduction

At present, there is one virtual machine per person in the company, and most of the work has to be done on the virtual machine, so you have to execute [ssh xxx@xxxxxx] instructions to log in to the virtual machine many times a day. There are many ways to solve this problem, such as using xshell, secureCRT and other tools to record common connections. I wrote a simple script to log in with one click.

The login.sh script contains the following:

#! / usr/bin/expectspawn ssh xxx@xxxxxxexpect "* password:" send "password\ r" interact

Then configure the alias alias of [. / login.sh], or move to the executable directory to achieve [login] one-click login.

Expect is an instruction to deal with interaction. Through expect, we can write the interaction process into Shell scripts to achieve some automated operations.

Expect has four core instructions:

Spawn: start a new process, followed by the instructions to be executed by the new process

Expect: specifies the string to listen for, and triggers send if the spawn process returns a matching string (such as a prompt for standard input)

Send: sends the specified string to the spawn process instead of standard input

Interact: user participation interaction

The execution process for login.sh is as follows:

[spawn] starts a new process to execute ssh login instructions. [expect] listens to the standard input prompt that "xxx@xxxxxx's password" meets the matching rules, triggers [send] to send a password instead of manual input, and completes the login. The [interact] user participates in the interaction and is logged on to the virtual machine.

Using expect, you can also complete functions such as batch updating of git code base, automatic login to FTP, etc.

This is the end of the article on "how to achieve automatic login script in expect in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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

Servers

Wechat

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

12
Report