使用expect 打通到其他服务器无密码访问

公司网站功能陆续开放 ,做的电商(B2B),由于zabbix对功能,业务监控比较方便,就用zabbix做监控,zabbix唯一的缺点就是需要在每台服务器上安装客户端,,即便是几十台服务器,一台一台做着实繁琐,何况更多,时间紧急,无密码登陆通道还没打通,就准备先打通,再写脚本来安装agentd,就选择了使用except。关于expect语法与使用方法,可以私下交流。

具体脚本如下。

本脚本可以循环服务器ip,完全做到自动化,缺点:服务器密码保持一致。

#!/bin/bashcat>login.exp<<EOF#!/usr/bin/expect-fsetip[lindex\$argv0]setpasswordZn6R7F4ffE#dIzkY%rfsettimeout3spawnssh-p60022admin@\$ipssh-keygen-trsa;expect{“yes/no”{send”yes\r”;exp_continue}}expect”admin@\$ip’spassword:”settimeout2send”\$password\r”settimeout2expect”(/home/admin/.ssh/id_rsa):”send”\r”expect{“y/n”{send”y\r”;exp_continue}}expect”(emptyfornopassphrase):”send”\r”expect”Entersamepassphraseagain:”send”\r”settimeout10send”exit\r”expecteofsettimeout3spawnssh-p60022admin@\$ipcat~/.ssh/id_rsa.pub>>~/.ssh/authorized_keys;expect{“yes/no”{send”yes\r”;exp_continue}}expect”admin@\$ip’spassword:”settimeout3send”\$password\r”settimeout10send”exit\r”expecteofsettimeout3spawnscp-P60022~/.ssh/authorized_keysadmin@\$ip:~/.ssh/authorized_keys;expect{“yes/no”{send”yes\r”;exp_continue}}expect”admin@\$ip’spassword:”settimeout3send”\$password\r”settimeout10send”exit\r”expecteofEOFforiin`catiplist`doexpectlogin.exp$idone说明,iplist里面写入服务器ip地址即可[root@zabbixadmin]#catiplist172.16.8.34172.16.8.35

另一个工具 sshpass 也可以实现无密码访问。

需要编译安装,源码下载地址

解压编译安装即可

使用:#从命令行方式传递密码

sshpass -p password ssh root@ip -p后面直接指定密码

#从文本传递密码

sshpass -f filessh root@ip 把密钥写到file里即可。

#从环境变量传递

export SSHPASS=”user_password”

sshpass -e ssh user_name@192.168..1.2

本文出自 “章小鱼儿_linux” 博客,请务必保留此出处

答:他是憋死的,因为沙漠里没有电线杆撒尿。问:

使用expect 打通到其他服务器无密码访问

相关文章:

你感兴趣的文章:

标签云: