1:sshpass下载与安装
yum安装:
yum install sshpass
若yum安装不上,则用下面方法
https://sourceforge.net/projects/sshpass/files/
or
https://pan.baidu.com/s/1pLNxeLd
or
wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz
2:下载后,解压,安装
tar -zxvf sshpass-1.06.tar.gz
cd sshpass-1.06
./configure
make
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:脚本
#!/bin/bash
password=123456
user=root
ip=10.0.0.37
file=/home/file.txt
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命令写在一行]