linux免交互登陆远程主机并执行命令(密钥对和Expect)
原文章摘自:http://lizhenliang.blog.51cto.com/7876557/1607723/
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 还有就是免密码scp
[root@WEB1-live sh]# cat scp_licai_war.sh
#!/usr/bin/expect set ip 10.9.10.2 set pass Weddvasdfa set timeout 30 #spawn ssh root@$ip spawn scp /root/test/esb_fabric.1611231739.tar.gz root@10.9.10.2:/data/pro_fabu/front 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 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 |
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
linux免交互登陆远程主机并执行命令(密钥对和Expect)的更多相关文章
- ssh 免交互登录 ,远程执行命令脚本。
##免交互SSH登录auto_login_ssh () { expect -c "set timeout -1; spawn -noecho ssh -o ...
- shell脚本批量ssh登陆主机并执行命令
shell脚本批量ssh登陆主机并执行命令 今天在客户现场遇到了这个问题,客户没有管理工具,无法批量登陆主机下发命令,几个个C段啊,让我一个一个登陆,.................. 所以写了个s ...
- Linux下如何保持gnome-terminal窗口执行命令后停留而不立刻关闭(gnome-terminal -x)
Linux下如何保持gnome-terminal窗口执行命令后停留而不立刻关闭(gnome-terminal -x) 转自:http://jakfruit.blog.163.com/blog/stat ...
- expect实现远程主机自动执行命令脚本
2014年第一个脚本,哈哈!!! expect实现远程主机自动执行命令脚本: #!/usr/bin/expect -- if { [llength $argv] < 4 } { puts &qu ...
- Linux免密登陆配置(互信配置)
Linux免密登陆配置(互信配置) 1.生成当前用户的秘钥文件 [oracle@localhost .ssh]$ ssh-keygen -t rsa 2.配置远程登录用户的公钥文件 将公钥文件拷贝至另 ...
- linux 免密码登陆
1.Linux下生成密钥 ssh-keygen的命令手册,通过”man ssh-keygen“命令: 通过命令”ssh-keygen -t rsa“ 生成之后会在用户的根目录生成一个 “.ssh”的文 ...
- Linux使用expect实现免手动密码输入,linux免密码登陆
使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理. 脚本代码如下: ###### ...
- ansible 批量在远程主机上执行命令
ansible 和 saltstack 都是为了同时在多台主机上执行相同的命令, 但是 salt配置麻烦,ansible基本不用配置, ansible 通过ssh来连接并控制被控节点 1. 安装 第一 ...
- Linux免密登陆设置了免密登陆为啥还需要输入密码
一.设置了免密码登陆但是还是需要输入密码: 权限保证:1.authorized-keys 的权限为 600 2.home.账户所在的目录如hadoop..ssh这三个文件的权限都必须设置为700,缺少 ...
随机推荐
- iOS使用Zbar扫描二维码
iOS使用Zbar扫描二维码 标签(空格分隔):二维码扫描 iOS Zbar64位 正文: 首先下载一个支持64位系统的ZbarSDK的包,保存在了我的云盘里,地址:ZbarSDK 把文件拖到工程里面 ...
- 【转载】ReactiveX 的理念和特点
原作者地址:http://www.open-open.com/lib/view/open1440166491833.html ReactiveX是Reactive Extensions的缩写,一般简写 ...
- Windows下使用Xshell建立反向隧道
反向隧道是一个进行内网穿透的简单而有用的方法.在Linux下通过OpenSSH和AutoSSH可以很容易地建立稳定的反向隧道.但是在Windows下,还能看到有人特意装个Cygwin来运行这些工具…… ...
- jdbc连接数据库的步骤 (转)
1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的静态方法forName(String classN ...
- 通过例子学习 Keystone - 每天5分钟玩转 OpenStack(19)
上一节介绍了 Keystone 的核心概念.本节我们通过“查询可用 image”这个实际操作让大家对这些概念建立更加感性的认识. User admin 要查看 Project 中的 image 第 1 ...
- C语言猜拳游戏
2016年最后一篇文章 今天闲来无事Google了一段C语言写的猜拳游戏的代码(本人水平比较低,几乎是刚入门),我没做什么修改.这个switch语句里面对result的处理让我眼前一新,原来是这么玩的 ...
- Ubuntu nginx 配置404错误页面
1.创建自己的404.html页面: 2.更改nginx.conf在http定义区域加入: /etc/nginx# vim nginx.conf 下添加 fastcgi_intercept_error ...
- 【2016-10-17】【坚持学习】【Day9】【反射】
3个主要命名空间 System.Type System.Reflection System.Reflection.Assembly 2个主要类 System.Type System.Reflectio ...
- 【2016-10-17】【坚持学习】【Day8】【简单工厂模式】
今天学习简单工厂模式, 结构 一个抽象产品 多个具体产品 一个工厂类,通过传入参数,new出不同的产品 代码: abstract class Product { //所有产品类的公共业务方法 publ ...
- CNI插件源码示例,对于github.com/rajatchopra/ocicni库的分析
CNI插件初始化 // ocicni.go 1.func InitCNI(pluginDir string) (CNIPlugin, error) (1).先调用plugin := probeNetw ...