linux免交互登陆远程主机并执行命令(密钥对和Expect)

简介:

Linux下实现免交互登陆一般有两种:

1. SSH无密码认证方式

    客户端使用ssh-keygen生成密钥对,将公钥复制到服务端(authorized_keys),SSH提供公钥登陆,当SSH访问服务端时,服务端先在本机寻找客户端的公钥,然后把客户端发来的公钥进行比较,如果一致,则用公钥加密给客户端,客户端再用私钥进行解密,实现加密所有传输的数据。

1>.在客户机上创建密钥对

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

2>.登陆ssh服务器,创建.ssh目录及设置权限

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

3>.将公钥上传到服务器并重命名为authorized.keys

1
# scp /root/.ssh/id_rsa.pub root@服务端IP:/root/.ssh/authorized_keys #id_rsa.pub可以追加多个客户端的公钥

4>.设置ssh服务器

1
2
3
4
5
6
# vi /etc/ssh/sshd_config 
RSAAuthentication  yes            #这三行取消注释,开启密钥对验证
PubkeyAuthentication  yes
AuthorizedKeysFile . ssh /authorized_keys
PasswordAuthentication no     #关闭密码验证
# service sshd restart

5>.免交互登陆测试,并查看远程主机磁盘分区

1
# ssh root@服务端IP 'df -h'

2. 利用expect工具自动实现交互任务

   Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。

   CentOS安装:yum install expect

   Ubuntu安装:sudo apt-get install expect

1>.免交互登陆,查看远程主机磁盘分区

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/expect
set  ip 192.168.1.156
set  pass 123.com    
set  timeout 30
spawn  ssh  root@$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
# interact

2>.在Shell脚本中嵌入Expect语法

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
user=root
pass= '123'
ip= '192.168.1.154'
/usr/bin/expect  << EOF
set  timeout 30
spawn  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
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
user=root
pass= '123'
ip= '192.168.1.154'
expect -c "
     spawn  ssh  $user@$ip
     expect {
         \"( yes /no )\" {send \" yes \r\"; exp_continue}
         \"password:\" {send \"$pass\r\"; exp_continue}
         \"root@*\" {send \" df  -h\r  exit \r\"; exp_continue}
     }"

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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 30
spawn  ssh  $username@$ipaddress
expect {
         "(yes/no)"  {send  "yes\r" ; exp_continue}
         "password:"  {send  "$password\r" }
}
expect  "$username@*"   {send  "df -h\r" }
expect  "$username@*"   {send  "exit\r" }
expect eof
1
2
3
4
5
6
7
8
9
10
11
# vi user_info     #用户信息文件
192.168.1.156   user    user
192.168.1.154   root    123.com
# vi expect.sh     #读取用户信息并赋值到变量
#!/bin/bash
for  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 $pass
done

参数说明:

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

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

spawn:执行一个命令

expect "":匹配输出的内容

exp_continue:继续执行下面匹配

\r:可以理解为回车

$argc:统计位置参数数量

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

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

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

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

其他参数说明:

timeout -1:永不超时退出

log_file /var/log/expect.log:记录交互信息,一般crontab时使用

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

将spawn ssh root@$ip换成spawn ssh -o StrictHostKeyChecking=no root@ip既不会再提示是否将服务器计算机密钥加入本地known_hosts



本文转自 李振良OK 51CTO博客,原文链接:http://blog.51cto.com/lizhenliang/1607723,如需转载请自行联系原作者

相关文章
|
1天前
|
安全 网络协议 Linux
linux必学的60个命令
Linux是一个功能强大的操作系统,提供了许多常用的命令行工具,用于管理文件、目录、进程、网络和系统配置等。以下是Linux必学的60个命令的概览,但请注意,这里可能无法列出所有命令的完整语法和选项,仅作为参考
139 2
|
1天前
|
Linux 程序员 计算机视觉
【linux 学习】在Linux中经常用到的cmake、make、make install等命令解析
【linux 学习】在Linux中经常用到的cmake、make、make install等命令解析
12 0
|
1天前
|
Linux
Linux的find命令使用
【5月更文挑战第11天】Linux的find命令使用
12 3
|
1天前
|
监控 Linux 数据处理
|
1天前
|
编解码 Ubuntu Linux
|
1天前
|
JSON Linux 数据格式
Linux命令发送http
请注意,`curl`命令非常灵活,可以根据您的需求进行多种配置和自定义。您可以查看 `curl`命令的文档以获取更多详细信息。
11 0
|
1天前
|
存储 缓存 Linux
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(三)--实时与非实时数据交互
本文介绍了Xenomai中的XDDP(Xenomai Distributed Data Protocol)通信机制,XDDP用于实时和非实时进程之间的数据交换。XDDP在Xenomai内核中涉及的数据结构和管理方式,以及创建XDDP通道后的实时端和非实时端连接过程。
14 0
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(三)--实时与非实时数据交互
|
分布式计算 Hadoop Java
linux expect 自动交互脚本用法
linux expect 自动交互脚本用法
3600 0
|
Shell Linux 网络安全
|
Shell Linux 网络安全