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 yesAuthorizedKeysFile .ssh/authorized_keysPasswordAuthentication 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/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# 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/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 |
|
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/bashuser=rootpass='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 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 |
|
1
2
3
4
5
6
7
8
9
10
11
|
# vi user_info #用户信息文件192.168.1.156 user user192.168.1.154 root 123.com# vi expect.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 |
参数说明:
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,缺少 ...
随机推荐
- SharePoint 2013 Excel Services ECMAScript 示例之明日限行
前言:最近遇到一个“明日限行”的功能,北京的交通啊,这个不在今天讨论范围内,暂不吐槽,想想代码开发,还要写WebPart部署,很麻烦,而且部署服务器,需要领导审批,想绕过这个麻烦事儿,就想到客户端了, ...
- Linux系统NFS网络文件系统
Linux系统NFS网络文件系统 NFS(network file system)网络文件系统,就是通过网络让不同的主机系统之间可以共享文件或目录,此种方法NFS客户端使用挂载的方式让共享文件或目录到 ...
- Java Web中请求转发和请求包含
1.都是在一个请求中跨越多个Servlet 2.多个Servlet在一个请求中,他们共享request对象.就是在AServle中setAttribute()保存数据在BServlet中由getAtt ...
- 环信SDK报错处理方法obtain an updated library from the vendor, or disable bitcode for this target. for archit
ld: '/Users/momo/Desktop/ThreeFingers/Pods/EaseMobSDKFull/EaseMobSDKFull/lib/libEaseMobClientSDK_arm ...
- IOS开发基础知识--碎片17
1:contentSize.contentInset和contentOffset区别 contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个s ...
- android handler传递消息机制
当工作线程给主线程发送消息时,因为主线程是有looper的,所以不需要初始化looper,注意给谁发消息就关联谁的handler,此时用的就是主线程的handler handler会把消息发送到Mes ...
- Redis 5种数据结构使用及注意事项
1优缺点 非常非常的快,有测评说比Memcached还快(当大家都是单CPU的时候),而且是无短板的快,读写都一般的快,所有API都差不多快,也没有MySQL Cluster.MongoDB那样更新同 ...
- Linux 如何查看修改DNS配置
DNS服务器介绍 DNS是计算机域名系统(Domain Name System 或Domain Name Service) 的缩写,它是由域名解析器和域名服务器组成的.域名服务器是指保存有该网络中所有 ...
- 利用 filter 机制 给 静态资源 url 加上时间戳,来防止js和css文件的缓存,利于开发调试
直接上代码: public class WeiXinFilter implements Filter{ private static Logger logger = LoggerFactory.get ...
- Python安装
Win10 安装Python 1.官网下载Python安装包,一直点击下一步进行安装 安装完Python后要设置环境变量,右键我的电脑-->属性-->高级系统设置-->环境变量--& ...