# 安装 sshpass
yum -y install sshpass
# 卸载
yum -y reomve sshpass
#第一次登录时候需要输入yes 执行之后会登录到目标主机
sshpass -p{密码} ssh -p {端口} -o StrictHostKeyChecking=no root@{ip}
sshpass -proot ssh -p 22 -o StrictHostKeyChecking=no root@192.168.1.102
#使用sshpass获取ip地址
sshpass -p{密码} shh -p {端口} root@{ip} "{命令}"
sshpass -proot ssh root@192.168.1.102 -p 22 "ip a"
# 指定文件
echo "passwd" > passwd
sshpass -f -p@Baipiao123 ssh root@{123.123.123.123} "ip a"
echo "password" > password
sshpass -f password ssh root@10.36.182.100 "ip a"
# 结合scp一起使用
sshpass -p 密码 scp file root@ip:/路径
## 示例:把本地文件/etc/hosts传入远程机器10.36.182.100
sshpass -p 1 scp /etc/hosts root@10.36.182.100:/etc
# 远程解压
sshpass -p 密码 ssh root@ip -tt tar -xzf 远程文件.tar包 -C 解压路径
#!/bin/bash
<<bak
pass.txt 文件示例
192.168.1.2 root password 22
192.168.1.3 root password 22
bak
[ ! -f $HOME/.ssh/id_rsa ] && ssh-keygen -t rsa -b 2048 -N "" -f $HOME/.ssh/id_rsa
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
INFO=pass.txt
# yum install -y sshpass
for IP in $(awk '/^[^#]/{print $1}' $INFO); do
USER=$(awk -v I=$IP 'I==$1{print $2}' $INFO)
PASS=$(awk -v I=$IP 'I==$1{print $3}' $INFO)
PORT=$(awk -v I=$IP 'I==$1{print $4}' $INFO)
sshpass -p $PASS ssh-copy-id -o StrictHostKeyChecking=no ${USER}@${IP} && \
action "秘钥传输成功 $IP" /bin/true || \
action "秘钥传输失败 $IP" /bin/false
done
#!/bin/bash
<<bak
pass.txt 文件示例
192.168.1.2 root password 22
192.168.1.3 root password 22
bak
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
INFO=pass.txt
# yum install -y sshpass
for IP in $(awk '/^[^#]/{print $1}' $INFO); do
USER=$(awk -v I=$IP 'I==$1{print $2}' $INFO)
PASS=$(awk -v I=$IP 'I==$1{print $3}' $INFO)
PORT=$(awk -v I=$IP 'I==$1{print $4}' $INFO)
sshpass -p $PASS scp -o StrictHostKeyChecking=no $1 ${USER}@${IP}:$2 && \
action "拷贝成功 $IP" /bin/true || \
action "拷贝失败 $IP" /bin/false
done
#!/bin/bash
<<bak
old_pass.txt 文件示例
192.168.1.2 root password 22
192.168.1.3 root password 22
bak
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
OLD_INFO=old_pass.txt
NEW_INFO=new_pass.txt
# yum install -y sshpass
for IP in $(awk '/^[^#]/{print $1}' $OLD_INFO); do
USER=$(awk -v I=$IP 'I==$1{print $2}' $OLD_INFO)
PASS=$(awk -v I=$IP 'I==$1{print $3}' $OLD_INFO)
PORT=$(awk -v I=$IP 'I==$1{print $4}' $OLD_INFO)
NEW_PASS=$(openssl rand -base64 8)
#NEW_PASS=1
sshpass -p "$PASS" ssh -p $PORT $USER@$IP -o StrictHostKeyChecking=no "echo $NEW_PASS |passwd --stdin $USER" && \
(
echo "$IP $USER $NEW_PASS $PORT [`date '+%F %T'`]" >> $NEW_INFO && \
action "配置成功 $USER $IP $NEW_PASS" /bin/true
) || \
action "配置失败 $USER $IP $PASS" /bin/false
done
本章完结
爱情不是牢笼 而应该吧最好的展现给对方 让对方觉得被爱 和 尊重