前言:
ssh命令, 没有指定密码的参数. 以至于在脚本中使用ssh命令的时候, 必须手动输入密码, 才能继续执行. 这样使得脚本的自动化执行变得很差, 尤其当ssh对应的机器数很多的时候, 会令人抓狂.本文讲解了两种方式, 一种借助expect脚本, 一种借助sshpass来实现.
*) 借助expect脚本来实现
1. expect不是系统自带的工具, 需要安装
yum install expect -y
2. expect脚本的编写规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
1 . [#!/usr/bin/expect]
|
简单例子:
1 2 3 4 5 6 |
#! /usr/bin/expect
|
这样就可以避免输入sudo密码了
3. 案例编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#! /bin/bash
|
评注:
ssh -o StrictHostKeyChecking=no 对首次登录的机器不进行检查, 避免了用户手动输入yes/no
echo -e $ssh_warpper, -e参数对后续字符串, 打开转义支持开关.
*) sshpass的使用
官网地址: http://sourceforge.net/projects/sshpass/
安装sshpass
wget http://nchc.dl.sourceforge.net/project/sshpass/sshpass/1.05/sshpass-1.05.tar.gz
tar zxvf sshpass-1.05.tar.gz
cd sshpass-1.05
./configure
make && make install
或者
# yum install epel-release
已加载插件:fastestmirror
# yum repolist
# yum -y install sshpass
用法:
# sshpass
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename Take password to use from file
-d number Use number as file descriptor for getting password
-p password Provide password as argument (security unwise)
-e Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-P prompt Which string should sshpass search for to detect a password prompt
-v Be verbose about what you‘re doing
-h Show help (this screen)
-V Print version information
At most one of -f, -d, -p or -e should be used
# export SSHPASS="user_password"
# sshpass -e ssh -p22022 [email protected] "hostname"
Nasty PTR record "192.168.5.77" is set up for 192.168.5.77, ignoring
vic8