Linux已经成为企业使用最为广泛的操作系统。作为运维工程师、开发工程师会经常在Linux平台上完成日常工作。所以会有这样的需求,例如将本机的数据备份远程传输到备份服务器、在本机执行命令获取远程服务器信息等。了解Linux的朋友知道,每次远程登录服务器是要输入用户名密码的,人工输入没毛病。但很多情况是周期性自动化执行的,例如备份。

所以,今天的主题是怎么实现不手动输入密码登录远程服务器,并执行Bash命令。在Linux系统下实现免交互登录一般有两种:

一、SSH密钥认证方式

远程登录Linux系统采用SSH协议,专为远程登录会话和其他网络服务提供安全性的协议。分为SSH客户端和SSH服务端。支持口令和密钥认证。

密钥认证过程:

SSH客户端可使用ssh-keygen工具生成密钥对,将公钥复制到SSH服务端/home/user/.ssh/authorized_keys文件,当SSH客户端使用私钥(~/.ssh/id_rsa)访问服务端时,服务端会将发来的私有在本机公钥匹配,如果匹配成功,则用公钥加密给客户端,客户端再用私钥进行解密,实现加密传输数据。

1、在SSH客户端创建密钥对

#ssh-keygen -t rsa #一路回车

2、在SSH服务端要远程登录的账户家目录下创建.ssh目录及设置权限

# mkdir /root/.ssh# chmod 700 /root/.ssh

3、将公钥上传到SSH服务端并重命名为authorized.keys

# scp /root/.ssh/id_rsa.pub root@remote_ip:/root/.ssh/authorized_keys 说明:remote_ip替换远程服务器IP,authorized_keys也可以存放多个id_rsa.pub

4、在SSH服务端启用SSH服务密钥认证

# vi /etc/ssh/sshd_config RSAAuthentication yes           PubkeyAuthentication yesAuthorizedKeysFile .ssh/authorized_keysPasswordAuthentication no # service sshd restart  #重启ssh服务

5、在SSH客户端测试

# ssh root@remote_ip  

这样就可以免交互登录远程服务器,并可以执行命令:

# ssh root@remote_ip 'df -h'

二、利用expect工具自动输入用户名和密码

Expect是一个免费的编程工具语言,用于实现自动交互,而无需人工干预。

在登录时,就可以用这个工具实现自动输入用户名和密码了!

Linux系统中默认通常没有安装的,需要先安装下再使用。

CentOS安装:yum install expect

Ubuntu安装:sudo apt-get install expect

1、免交互SSH登陆远程服务器,并查看磁盘分区

#!/usr/bin/expectset ip 192.168.1.156set pass 123.com    set timeout 30spawn ssh root@$ipexpect {   "(yes/no)" {send "yes\r"; exp_continue}   "password:" {send "$pass\r"}}expect "root@*"  {send "df -h\r"}expect "root@*"  {send "exit\r"}expect eof

2、在Shell脚本中嵌入Expect语法

方法1:使用EOF,将内容段让expect执行

#!/bin/bashuser=rootpass='123'ip='192.168.1.154'/usr/bin/expect << EOFset timeout 30spawn ssh $user@$ip   expect { "(yes/no)" {send "yes\r"; exp_continue} "password:" {send "$pass\r"}}expect "root@*"  {send "df -h\r"}expect "root@*"  {send "exit\r"}expect eof EOF

或者

#!/bin/bashuser=rootpass='123'ip='192.168.1.154'expect -c "spawn ssh $user@$ipexpect {  \"(yes/no)\" {send \"yes\r\"; exp_continue}  \"password:\" {send \"$pass\r\"; exp_continue}  \"root@*\" {send \"df -h\r exit\r\"; exp_continue} }"

方法2:将expect脚本独立出来

免交互登录脚本:

# vi login.exp     #!/usr/bin/expect set ipaddress [lindex $argv 0]set username [lindex $argv 1]set password [lindex $argv 2]if { $argc != 3 } {puts "Usage: expect login.exp ipaddress username password"  exit 1}set timeout 30spawn ssh $username@$ipaddressexpect {    "(yes/no)" {send "yes\r"; exp_continue}    "password:" {send "$password\r"}}expect "$username@*"  {send "df -h\r"}expect "$username@*"  {send "exit\r"}expect eof

用户信息文件:

# vi user_info  192.168.1.156   user    user192.168.1.154   root    123.com

Shell脚本:读取用户信息并赋值到变量

xpect.sh      #!/bin/bashfor ip in `awk '{print $1}' user_info`do    user=`awk -v I="$ip" '{if(I==$1)print $2}' user_info`    pass=`awk -v I="$ip" '{if(I==$1)print $3}' user_info`    expect login.exp $ip $user $passdone

写好这三个文件之后,执行Shell脚本,就会按顺序执行远程服务器信息文件,并查看磁盘分区。

Expect指令说明:

set:可以设置超时,也可以设置变量。

timeout:expect超时等待时间,默认10S。

spawn:执行一个命令。

expect"":匹配输出的内容。

exp_continue:继续执行下面匹配。

\r:可以理解为回车。

$argc:统计位置参数数量。

[lindex$argv 0]:脚本后第一个参数,类似于shell中$1,以此类推。

puts:打印字符串,类似于Shell的echo。

awk-v I="$ip":赋值变量。

expect{...}:输入多行记录。

其他指定说明:

timeout-1:永不超时退出。

log_file/var/log/expect.log:记录交互信息。

interact:交互后不退出远程终端,如果加要把expect "root@*" {send "exit\r"}注释掉,如果不加,就直接退出。

©著作权归作者所有:来自51CTO博客作者mob604756e5abbc的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. Python运维自动化开发之Fabric模块
  2. 运维少年系列 python and cisco (4)
  3. 2021-03-11:go中,协程内部再启用协程,它们是没关系,对吧?外部协程奔溃
  4. 记一次Oracle数据更新经历
  5. SQL高级知识V2——动态SQL
  6. IDEA激活码注册码密钥,2021年最新永久激活码共享!
  7. 多厂商***系列之一:加密基础与IPSec【附带思科与H3C的配置介绍】
  8. ansible初入
  9. SQL存储过程的详细用法,不信你看不懂

随机推荐

  1. MySQL5.7以上版本root用户空密码修改(wind
  2. Auto-generated primary key in sql data
  3. 解决Linux主机上的 远程MySQL客户端无法
  4. 安装MySQL时出现黄色感叹号,提示3306已被
  5. java链接数据库--Mysql
  6. MySQL常用命令与常见问题解决
  7. profiles在mysql中的应用
  8. 30分钟安装linux版本mysql5.7.21版本,没
  9. MySQL官网示例数据库emploees分析使用
  10. Mysql 查询—按位运算