sshpass的安装使用

1:sshpass下载与安装
     yum安装: 

yum install sshpass

     若yum安装不上,则用下面方法 

 
  1. https://sourceforge.net/projects/sshpass/files/

  2. or

  3. https://pan.baidu.com/s/1pLNxeLd

  4.  or

  5. wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz


 

2:下载后,解压,安装

  
 
  1. tar -zxvf sshpass-1.06.tar.gz

  2. cd sshpass-1.06

  3. ./configure

  4. make

  5. make install

3:使用命令

     
sshpass -p 123456 scp /home/file.txt [email protected]:/home/copy

 

      后面这个是 “Are you sure you want to continue connecting (yes/no)”使得这个自动接受,若不加,则成功不了

4:脚本

复制代码
 
  1. #!/bin/bash

  2. password=123456

  3. user=root

  4. ip=10.0.0.37

  5. file=/home/file.txt

  6. sshpass -p $password scp file $user@$ip:/home/copy/

sshpass:用于非交互的ssh 密码验证
使用 -p 参数指定明文密码,然后直接登录远程服务器。 它支持密码从命令行,文件,环境变量中读取
1、从命令行方式传递密码
sshpass -p user_password ssh [email protected]  【登录远程机器】
sshpass -p user_password scp -P22 [email protected]:/home/test  ./ 【远程机器/home/test 复制到本机当前目录】
还可以加参数 -q 【去掉进度显示】

2、从文件读取密码
echo "user_password" > user.passwd
sshpass -f user.passwd ssh [email protected]

3、从环境变量获取密码
export SSHPASS="user_password"
sshpass -e ssh [email protected] 

4、sshpass -p user_password ssh  -o StrictHostKeyChecking=no  [email protected] 
【-o StrictHostKeyChecking=no 表示远程连接时不提示是否输入yes/no】

5、使用sshpass远程免密,在远程主机上执行shell命令,如下远程执行命令:touch /opt/file.txt
sshpass -p user_password ssh  -o StrictHostKeyChecking=no  [email protected]  touch /opt/file.txt
[注:shell命令要和sshpass命令写在一行]

猜你喜欢

转载自blog.csdn.net/jycjyc/article/details/108789763