windows 安装open SSH

一、下载

https://github.com/PowerShell/Win32-OpenSSH/releases

二、解压

方式一:解压到 C:\Program Files 目录
C:\Program Files\OpenSSH-Win64
方式二:放任意目录,配置环境变量

三、安装

1、打开cmd,cd进入C:\Program Files\OpenSSH-Win64(安装目录),执行命令:

powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

2、设置服务自动启动并启动服务:

sc config sshd start= auto
net start sshd

3、防火墙开放22端口

netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22

cd C:\ProgramData\ssh
端口号:Port 22

密钥访问:PubkeyAuthentication yes
密码访问:PasswordAuthentication no
空密码:PermitEmptyPasswords no

四、生产密钥

# 生成密钥
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# 拷贝密钥
cat ~/.ssh/id_rsa.pub | clip

通过以上配置,即可完成openSSH 的安装配置。

五、实例参考

#!/bin/bash

# 当前时间
CUR_TIME=$(date "+%Y%m%d_%H%M%S")
# 源码编译目录(含dist)
#DIST_DIR="/root/test/xxx-front-web/dist"
DIST_DIR="$WORKSPACE/dist"
# 检查传值
# echo "当前构建项目路径: $DIST_DIR"

# WIN源码目录
WIN_DIR="C:\Users\Public\www\tmp\xxx"
# WINb备份目录
WIN_BAK_DIR="C:\Users\Public\www\barkup"
# WIN服务器地址
WIN_IP="172.18.0.xxx"
# WIN登录密码
WIN_PWD="123456"
# WIN登录用户
WIN_USER="administrator"

# 部署服务器地址
UP_SVR_IP="172.18.0.xxx"
# 部署目录
#UP_DIR="/home/test/xxx"
UP_DIR="/home/src/xxx

# 删除dist
del_win_dist(){
    sshpass -p "123456" ssh -Tq -o 'StrictHostKeyChecking=no'  ${WIN_USER}@${WIN_IP} << EOFDEL
    cd $WIN_DIR
    # if exist dist (7z a -tzip  ${WIN_BAK_DIR}\archive_${CUR_TIME}.zip dist)
    if exist dist (rd /s /q dist ) else (echo not exis)
EOFDEL
}

# 传输编译文件
send_win_dist(){
    sshpass -p $WIN_PWD scp -o StrictHostKeyChecking=no -r ${DIST_DIR}  ${WIN_USER}@${WIN_IP}:${WIN_DIR}
}

# 登录Windows并压缩打包
zip_win_target(){
    sshpass -p $WIN_PWD ssh -Tq -o 'StrictHostKeyChecking=no'  ${WIN_USER}@${WIN_IP}  << EOFZIP
    cd $WIN_DIR
    if exist archive.zip (del archive.zip) else (echo not exist)
    7z a -tzip archive.zip dist
EOFZIP
}

# 部署前端到指定目录
deploy_front() {
    # 登录
    ssh -Tq -o 'StrictHostKeyChecking=no' root@${UP_SVR_IP} << EOFSSH
    [ ! -d $UP_DIR ] &&  mkdir -p $UP_DIR
    cd $UP_DIR

    # 备份
    [ -d dist ] && tar -czf xxx_${CUR_TIME}.tar.gz dist/ || echo "tar dist not exist"

    # 删除
    [ -e archive.zip ] && rm archive.zip -f || echo "rm archive.zip  not exist"
    [ -d dist ] && rm dist -rf || echo "rm dist not exist"

    # 部署
    wget http://${WIN_IP}/tmp/xxx/archive.zip # &>/dev/null
    unzip archive.zip  &>/dev/null
    # 检测
    if [ $? -eq 0 ]; then
            echo "部署成功!"
        else
            echo "部署失败!"
    fi 
    # 保留10个版本 
    cd $UP_DIR && ls -lt |grep tar.gz |awk 'NR>10 {print \$NF}' |xargs rm -rf
    echo "-------------------------------------------------------------------------------"

EOFSSH
}

del_win_dist
send_win_dist

zip_win_target
deploy_front

解决错误:ECDSA host key for 172.18.5.xxx has changed and you have requested strict checking
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:cXQubDeVsd9hEz0Ln94+lqJ2srSZo4jOZ5F09zLJqr0.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:7
ECDSA host key for 172.18.5.xxx has changed and you have requested strict checking.
Host key verification failed.

#进入目录:
cd ~/.ssh/
# 编辑目录:
vim known_hosts
# 删除172.18.5.xxx 整行

再次尝试
ssh Administrator@172.18.5.xxx
The authenticity of host '172.18.5.xxx (172.18.5.xxx)' can't be established.
ECDSA key fingerprint is SHA256:cXQubDeVsd9hEz0Ln94+lqJ2srSZo4jOZ5F09zLJqr0.
ECDSA key fingerprint is MD5:5f:d4:6c:c2:93:a1:03:71:18:64:a1:47:9a:f8:fc:61.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.18.5.xxx' (ECDSA) to the list of known hosts.
Administrator@172.18.5.xxx's password:
输入密码即可登录成功。

posted @ 2021-12-09 17:31  刘文江  阅读(47)  评论(0)    收藏  举报  来源