Linux之expect非交互式功能
我在上一篇博文linux之SSH密钥认证 提过ssh之间的相互认证,但是每次使用ssh登录到其它服务器还是要输入密码的。
expect是用于提供自动交互的工具,自动连接被管理的服务器,不需要手动输入密码。
1、安装expect
[root@mg ~]# yum install -y expect
2、编写expect脚本,直接分发SSH公钥,不用手工输入密码。
vim /server/scripts/expect.exp
#!/usr/bin/expect #-------------CopyRight-------------
# Name:ssh send password
# Version Number:1.00
# Type:sh
# Language:expect
# Date:--
# Author:sandy
# QQ:
# Email:eeexu123@.com
# Blog:https://www.cnblogs.com/eeexu123/ if { $argc != } {
send_user "usage: expect fenfa_expect file host\n" //判断传入参数是否是2个
exit
} #define var
set file [lindex $argv ] //第一个参数是ssh公钥
set host [lindex $argv ] //第二个参数是连接的远程主机地址
set passwd "herine" //设置连接用户的密码 #send ssh key
spawn ssh-copy-id -i $file "-p 22 root@$host" //发送ssh公钥命令
expect {
"yes/no" {send "yes\r";exp_continue} //是否继续连接,expect交互式功能,自动添加yes,并继续。yes后成必须加\r回车符 } sleep 3 //等待连接到远程主机
expect "*password" //输入密码,expect交互功能,自动添加密码变量。后面加\r回车符
send "$passwd\r"
expect eof exit -onexit {
send_user "Goodbye!\n" //退出
}
3、测试
/usr/bin/expect test_expect.exp ~/.ssh/id_dsa.pub 172.16.1.72
上面一条命令可以放在脚本里,大批量建立ssh密钥连接
[root@mg scripts]# /usr/bin/expect test_expect.exp ~/.ssh/id_dsa.pub 172.16.1.72
spawn ssh-copy-id -i /root/.ssh/id_dsa.pub -p root@172.16.1.72
The authenticity of host '172.16.1.72 (172.16.1.72)' can't be established.
RSA key fingerprint is a5::d4:::::aa::8d:f0:ce::5a:d3:f4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.72' (RSA) to the list of known hosts.
root@172.16.1.72's password:
Now try logging into the machine, with "ssh '-p 22 root@172.16.1.72'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. Goodbye!
ssh远程使用命令
[root@mg scripts]# ssh root@172.16.1.72 "/sbin/ifconfig eth1"
eth1 Link encap:Ethernet HWaddr :0C::8D::
inet addr:172.16.1.72 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe8d:/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (70.5 KiB) TX bytes: (38.8 KiB)
由上可以,expect交互功能在SSH免密码操作成功。
Linux之expect非交互式功能的更多相关文章
- Linux Socket - 内核非阻塞功能
select 函数 int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval*tim ...
- Linux 命令 - wget: 非交互式网络下载器
命令格式 wget [option]... [URL]... 命令参数 启动选项 -V, --version 打印版本信息 -h, --help 打印帮助信息 日志和输入文件选项 -o logfile ...
- (转)SSH批量分发管理&非交互式expect
目录 1 SSH批量分发管理 1.1 测试环境 1.2 批量管理步骤 1.3 批量分发管理实例 1.3.1 利用sudo提权来实现没有权限的用户拷贝 1.3.2 利用sudo提权开发管理脚本 1.3. ...
- 两种Linux下非交互式命令的实现
一.概述 在Linux环境,有多种实现自己主动化的脚本语言.如Shell.Python.Perl.Tcl等. Shell语言因与Shell外壳结合紧密,是最常见的实现自己主动化的脚本语言. 同一时候, ...
- RHEL6非交互式工具sshpass和expect安装
RHEL6非交互式工具sshpass和expect安装 1 sshpass 在rhel6.4上,没有sshpass的软件包,无法采用yum方式安装.从源码编译安装非常简单. 1) 下载sshpass源 ...
- 《学渣Linux笔记》——关于.bashrc与profile(涉及交互式与非交互式、登录与非登录shell)
<学渣Linux笔记>--关于.bashrc与profile(涉及交互式与非交互式.登录与非登录shell) 1.基本概念(个人理解) 交互式shell:等待用户输入,并执行相应操作的sh ...
- Linux下 expect 使用详解与实例
一.概述 我们通过Shell可以实现简单的控制流功能,如:循环.判断等.但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如telnet服务器等进行交互的功能.而Expec ...
- mysql交互式连接&非交互式连接
交互式操作:通俗的说,就是你在你的本机上打开mysql的客户端,就是那个黑窗口,在黑窗口下进行各种sql操作,当然走的肯定是tcp协议. 非交互式操作:就是你在你的项目中进行程序调用.比如一边是tom ...
- shell脚本分为三类:登录脚本、交互式脚本、非交互式脚本
shell脚本分为三类:登录脚本.交互式脚本.非交互式脚本 一. 登录脚本类似于windows下的计算机设置中的登录脚本和账户设置下的登录脚本的合集(我是这么理解的哈). 其配置文件的关键词为pref ...
随机推荐
- Redis persistence demystified
https://redis.io/topics/persistence http://oldblog.antirez.com/post/redis-persistence-demystified.ht ...
- 第三章 Java 的基本程序设计结构
1.Java基本数据类型 Java中一共有8种基本数据类型. 4种整形:int-4字节.long-8字节.short-2字节.byte-1字节 2中浮点型:float-4字节.double-8字节 1 ...
- 我对Swift的几点疑问
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/caowenbin/article/details/31418773 Swift自问世 ...
- Break、Continue、Return区别
1)break 直接跳出当前的循环,从当前循环外面开始执行,忽略循环体中任何其他语句和循环条件测试.他只能跳出一层循环,如果你的循环是嵌套循环,那么你需要按照你嵌套的层次,逐步使用break来 ...
- javascript面向对象技术基础总结
javascript面向对象和php不太一样,语法不太相同,总结如下 //cat 对象 function Cat(name,color){ this.name = name;//不确定的成员属性 th ...
- BZOJ2120 数颜色 —— 待修改莫队
题目链接:https://vjudge.net/problem/HYSBZ-2120 2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: ...
- CSU - 1547 Rectangle —— DP(01背包)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 题解: 关键是怎么处理长度为1的长方形.当长度为1的长方形的个数cnt> ...
- Chapter 3 Shared Assemblies and Strongly Named Assemblies
As time marches on,MS developers and control developer modify their code:they fix bugs,patch securit ...
- codeforces A. Nuts 解题报告
题目链接:http://codeforces.com/problemset/problem/402/A 题目意思:几经辛苦,终于体明题目噶意思了 = =,完全是考验一个人是否清醒的最简便方法- -! ...
- 【转】看C++文档的小知识
转载:http://www.ggv.com.cn/forum/clib/ctype/isspace.html 函数isspace 原型:extern int isspace(int c); 用法 ...