ssh服务是不能用非交互的方式传递密码,想不输入密码,直接ssh连接到服务器有两种方法,sshpass和expect

sshpass

# wget http://downloads.sourceforge.net/project/sshpass/sshpass/1.04/sshpass-1.04.tar.gz?use_mirror=cdnetworks-kr-1
# tar zxvf sshpass-1.04.tar.gz 
# cd sshpass-1.04
# ./configure 
# make && make install

sshpass为C编写的一个小程序使用比较简单,用法如下

sshpass  -p 密码 ssh 用户名@目标IP 要执行的命令

此处密码为明文。sshpass也可以使用密码文件。具体可以通过‘sshpass -h’看看帮助。

对于密码安全问题,如使用密码文件可以将文件权限修改为600。如果直接写在shell中,可以使用加密脚本的

工具,缺点是每次修改都要编译脚本。

expect

1.首先确认expect的包要安置。

[root@localhost]$ rpm -qa | grep expect

如果没有则需要下载安装,yum -y install expect expect-devel

安装过后会显示:

[root@localhost] rpm -qa | grep expect

expect-5.43.0-5.1
expect-devel-5.42.1-1

2.查看expect的路径,可以用

[root@localhost] which expect

/usr/bin/expect

3.确定脚本有可执行权限

chmod +x test.sh

#!/usr/bin/expect   //这个expect的路径就是用which expect 查看的结果

spawn ssh -l     远程主机的用户名 远程主机的ip地址    
expect "password:"           //提示让输入密码
send "远程主机的密码\r"       
interact                             //操作完成

另外需要注意的是:

不能按照习惯来用sh test.sh来这行expect的程序,会提示找不到命令,如下:

test.sh: line 2: spawn: command not found
couldn't read file "password": no such file or directory
test.sh: line 4: send: command not found
test.sh: line 5: interact: command not found

 

因为expect用的不是bash所以会报错。执行的时候直接./test.sh就可以了

http://blog.csdn.net/zhuying_linux/article/details/6657020

最新文章

  1. MyBatis处理一行数据-MyBatis使用sum语句报错-MyBatis字段映射-遁地龙卷风
  2. Python 开发轻量级爬虫08
  3. mariadb用户和权限管理
  4. 【翻译】Kinect v2程序设计(C++) BodyIndex篇
  5. (转)HTML5开发学习(3):本地存储之Web Sql Database
  6. 【转】PHP程序员的技术成长规划
  7. 关于Parse库的配置问题
  8. Delphi自定义消息应用及delphi托盘实现
  9. 剑指offer-面试题20.顺时针打印矩阵
  10. Android消息机制之Handler
  11. python编程快速上手之第6章实践项目参考答案
  12. Dynamics CRM 给视图配置安全角色
  13. Morris遍历-如何用空间复杂度O(1)来遍历二叉树
  14. 【原创】beyond compare 解决文件一样,对比有差异的问题
  15. 基于Nginx 和 uwsgi 搭建 django.
  16. select()函数 的学习
  17. linux 时间和时区设置
  18. D- 泛型练习 ,继承,方法
  19. 15个超级实用的jQuery插件
  20. 基于redis分布式锁实现“秒杀”(转载)

热门文章

  1. JAVA 内部类 (二)
  2. React.js:template
  3. bzoj4259
  4. 用HTML 5打造斯诺克桌球俱乐部(1) – 51CTO.COM
  5. view-source协议
  6. JS正则表达式(一)
  7. studio 集成 Genymotion后打开模拟器出错、打开虚拟机VirtualBox出错
  8. win10系统,mysql-installer-community-5.7.19.0.msi安装
  9. shader实例(八)渲染路径RenderingPath
  10. TFS 无法找到新加的Windows用户