In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-06-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use Shell script to automatically enter password to log on server". In daily operation, I believe that many people have doubts about how to use Shell script to automatically input password to login server. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to use Shell script to automatically enter password to log on server". Next, please follow the editor to study!
Programmers using Linux must be familiar with entering passwords. There are strict permission restrictions on users under Linux. They have to enter passwords to do many things beyond their permissions, such as using superusers to execute commands, such as ftp, ssh to connect to remote hosts, and so on, as shown below:
So the question is, what if you need to enter a password when the script is executed automatically? For example, if you have a scp statement in your script, you can't enter the password manually when the script executes to this sentence.
For ssh or scp commands, some people may answer that it is to establish a trust relationship. For the method of establishing a ssh trust relationship, please use Baidu Google, which can be done with only two lines of simple commands, but this is not a conventional solution. If it is a ftp connection, there is nothing you can do. Moreover, you cannot manually establish ssh trust for every host you want to connect to in order to execute certain commands, which has deviated from the original intention of today's topic. Today we are going to talk about automatically entering passwords in scripts. We can imagine that a more elegant way is to configure your own passwords in the script and enter them automatically when screen interaction needs to be typed. To achieve this effect, you need to use expect.
Installation
The installation command under CentOS is simple, as follows
The code is as follows:
Sudo yum install expect
As for Mac users, you can install it through homebrew (if you need to install homebrew first, please Google yourself)
The code is as follows:
Brew install expect
Test script
We write a simple script to implement the scp copy file, configure the password in the script, and save it as scp.exp as follows
The code is as follows:
#! / usr/bin/expect
Set timeout 20
If {[llength $argv]
< 2} { puts "Usage:" puts "$argv0 local_file remote_path" exit 1 } set local_file [lindex $argv 0] set remote_path [lindex $argv 1] set passwd your_passwd set passwderror 0 spawn scp $local_file $remote_path expect { "*assword:*" { if { $passwderror == 1 } { puts "passwd is error" exit 2 } set timeout 1000 set passwderror 1 send "$passwd\r" exp_continue } "*es/no)?*" { send "yes\r" exp_continue } timeout { puts "connect is timeout" exit 3 } } 注意,第一行很重要,通常我们的脚本里第一行是#!/bin/bash,而这里是你机器上expect程序的路径,说明这段脚本是由expect来解释执行的,而不是由bash解释执行,所以代码的语法和shell脚本也是不一样的,其中set passwd your_passwd设置成你自己的密码,然后执行如下命令 代码如下: ./scp.exp ./local_file user@host:/xx/yy/ 执行前确保scp.exp有执行权限,第一个参数为你本地文件,第二个为远程主机的目录,运行脚本如果报错"connect is timeout",可以把超时设长一点,第二行set timeout 20可以设置超时时间,单位是秒。脚本执行效果如下 还能做什么 细心的同学一定发现了,其实expect提供的是和终端的一种交互机制,输入密码只是其中一种应用形式,只要是在终端阻塞需要输入时,都可以通过expect脚本完成自动输入,比如前面脚本里配置了两种交互场景,一种是终端提示"password:"时输入密码,还有一种是提示"yes/no)?"时输入"yes",如果和远程主机是第一次建立连接,执行scp.exp脚本效果是这样的At this point, the study on "how to use Shell scripts to automatically enter passwords to log in to the server" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.