惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
博客园_首页
J
Java Code Geeks
C
Check Point Blog
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
D
Docker
U
Unit 42
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
GbyAI
GbyAI
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 microbundle 源码 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc vue -- vue3利用createVNode函数,建立命令式调用组件 | 时间的朋友
sshpass 使用
2024-07-29 · via 时间的朋友

sshpass 使用 🔗

在提供的文档中,提到了使用 sshpass 来实现自动化部署。sshpass 是一个非交互式 SSH 密码认证工具,它允许你通过命令行传递密码,从而实现自动化脚本中的 SSH 登录。

以下是文档中提到的 sshpass 的用法示例:

  1. 安装 sshpass

    • 在 Linux 系统中,可以通过包管理器安装sshpass

      。例如,在 Ubuntu 或 Debian 上,可以使用以下命令:

      1
      
      sudo apt-get install sshpass
      
  2. 使用 sshpass 进行 SSH 登录

    • 使用sshpass可以通过命令行传递密码来登录服务器。示例如下:

      1
      
      sshpass -p 'your_password' ssh user@host
      
  3. 在脚本中使用 sshpass

    • 在自动化脚本中,可以将sshpassscpssh命令结合使用,实现文件传输和命令执行。示例如下:

      1
      2
      
      sshpass -p 'your_password' scp -P port file_to_transfer user@host:/path/to/destination
      sshpass -p 'your_password' ssh -p port user@host 'command_to_execute'
      
  4. 在 package.json 中使用 sshpass

    • 可以将sshpass命令集成到package.json的脚本中,以便通过 npm 脚本执行。示例如下:

      1
      2
      3
      4
      5
      
      {
        "scripts": {
          "ssh:dev": "sshpass -p 'your_password' scp -P port file_to_transfer user@host:/path/to/destination"
        }
      }
      
  5. 安全注意事项

    • 将密码直接写在脚本中是不安全的,建议使用 SSH 密钥认证或配置免密码登录。
    • 如果必须使用密码,可以考虑使用环境变量或其他安全方式传递密码。

文档中还提到了一些具体的命令和脚本示例,例如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash

# 定义打包的项目
project=$1 # 接受项目名称参数
if [ ! $project ]; then
echo "请输入部署的项目"
exit
fi

# 全局参数
homeDir=/usr/local/nginx/html
host=xxxx
port=xxxx
user=xxxx
password=xxxx

# 打包项目
echo "开始打包项目"
cd out
tar -zcvf $project.tar.gz $project/

# 上传代码并进入服务器中
sshpass -p $password scp -P $port $project.tar.gz $user@$host:$homeDir
sshpass -p $password ssh -p $port $user@$host "cd $homeDir && tar -zxvf $project.tar.gz && chown -R root:root $project && chmod -R 755 $project && rm -f $project.tar.gz"

echo "部署完成"
exit