更多: zhilu jiaocheng

示例 1:使用 ssh 提供密码

# sshpass -p 'password' ssh ldap.onitroad.com -l root -o StrictHostKeyChecking=no

其中:

password是服务器(ldap.onitroad.com)的密码。
'StrictHostKeyChecking=no '用于控制对主机密钥未知或者已更改的机器的登录。

示例 2:在远程服务器上运行一些命令

让我们尝试使用 sshpass commnad 在远程服务器上运行 2 个命令“uptime”和“uname”:

# sshpass -p 'password' ssh ldap.onitroad.com -l root -o StrictHostKeyChecking=no "uptime;uname -a"

示例输出:

18:49:34 up 21 days, 18:49,  3 users,  load average: 0.01, 0.00, 0.00
Linux ldap.onitroad.com 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux
sshpass 命令:使用 SSH 进行非交互式密码验证

sshpass 的一些常见用途

  1. 备份到远程服务器
  2. 在指定时间在系统上执行命令。

说明

Linux 系统管理员通常通过提供密码或者使用基于密钥的身份验证登录 Linux 服务器。
sshpass 是一个工具,它允许我们自动向命令提示符提供密码,以便用户可以根据需要运行自动化脚本。
sshpass 使用专用 tty 向 ssh 提示提供密码,欺骗 ssh 相信交互式用户正在提供密码。

sshpass 安装

  1. 基于 Centos 的发行版:

从 https://fedoraproject.org/wiki/EPEL 设置 EPEL 存储库,然后以 root 身份运行:

# yum -y install sshpass
  1. 基于 Ubuntu/Debain 的发行版:

以 root 身份运行:

# apt-get install sshpass
  1. 从源码编译安装:
# wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz
# tar -zxvf sshpass.tar.gz 
# cd sshpass-1.05/ 
# ./configure 
# make 
# make install
# which sshpass
/usr/local/bin/sshpass

获得帮助

# sshpass -h
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
-h            Show help (this screen)
-V            Print version information

至多使用 -f、-d、-p 或者 -e 之一

示例 3:使用 rsync 将文件复制到服务器

在我们的例子中,我们将文件 sshpass.tar.gz 复制到远程服务器 ldap.onitroad.com

# sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@ldap.onitroad.com:/tmp/

上述命令的输出:

sending incremental file list
sshpass.tar.gz
98362 100% 62.56MB/s 0:00:00 (xfer#1, to-check=0/1)
sent 98472 bytes received 31 bytes 197006.00 bytes/sec
total size is 98362 speedup is 1.00
root@server1:/home/onitroad#

示例 4:用于复制到远程服务器的 for 循环

创建一个文件如下:

# touch /tmp/scr

该文件应包含主机名:

server2.onitroad.com
server3.onitroad.com
server4.onitroad.com
server5.onitroad.com
# for i in `cat /tmp/scr`; do echo " ";echo "###$i####"; sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@$i:/tmp/; done

以上命令的输出:

###server2.onitroad.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33
###server3.onitroad.com####
sending incremental file list
sent 54 bytes received 12 bytes 44.00 bytes/sec
total size is 98362 speedup is 1490.33
###server4.onitroad.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33
###server5.onitroad.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33
日期:2020-09-17 00:14:42 来源:oir作者:oir