This commit is contained in:
hero920103 committed 2026-11-13 20:26:16 +08:00
1 parent b6b528490c
commit 4ab63e77ed
28 files changed
+1858 -695

No files matched your search

+11 -7
View File
@@ -15,7 +15,7 @@
//
// 用法:
//
// deploy -host 10.31.100.2 -user root -dir /www/wwwroot/api.yunzer.cn -local .
// deploy -host 212.64.112.158 -port 8822 -user root -dir /www/wwwroot/api.yunzer.cn -local .
package main
import (
@@ -51,8 +51,8 @@ var defaultExcludeFiles = []string{
"*.exe", "*.log", "*.pid", "*.bak", "*.tmp", "*.pub",
"nohup.out", "output.log", "go.log",
"test-output-*.txt", "migrate_*.csv",
"server", // 无扩展名的编译产物
"*_id_ed25519", // SSH 私钥:绝对不能传到服务器
"server", // 无扩展名的编译产物
"*_id_ed25519", // SSH 私钥:绝对不能传到服务器
".deploy_known_hosts",
}
@@ -60,6 +60,7 @@ var defaultExcludeFiles = []string{
type options struct {
host string
port string // ← 新增
user string
pass string
keyFile string
@@ -79,7 +80,8 @@ var errCancelled = errors.New("用户取消")
func main() {
var opt options
flag.StringVar(&opt.host, "host", "10.31.100.2", "服务器地址")
flag.StringVar(&opt.host, "host", "212.64.112.158", "服务器地址")
flag.StringVar(&opt.port, "port", "22", "SSH 端口") // ← 新增
flag.StringVar(&opt.user, "user", "root", "SSH 用户名")
flag.StringVar(&opt.pass, "pass", "", "SSH 密码(留空则读环境变量 DEPLOY_PASS)")
flag.StringVar(&opt.keyFile, "key", "", "SSH 私钥路径(指定后用密钥登录,忽略密码)")
@@ -137,7 +139,7 @@ func run(opt options) error {
}
fmt.Printf("本地目录 : %s\n", localRoot)
fmt.Printf("远程目录 : %s@%s:%s\n", opt.user, opt.host, opt.dir)
fmt.Printf("远程目录 : %s@%s:%s:%s\n", opt.user, opt.host, opt.port, opt.dir) // ← 输出带端口
client, err := dial(opt, localRoot)
if err != nil {
@@ -583,10 +585,12 @@ func dial(opt options, localRoot string) (*ssh.Client, error) {
HostKeyCallback: hostKeyChecker(localRoot),
Timeout: 20 * time.Second,
}
addr := fmt.Sprintf("%s:%d", opt.host, 22)
// ← 关键:用 net.JoinHostPort 支持自定义端口(同时兼容 IPv6)
addr := net.JoinHostPort(opt.host, opt.port)
client, err := ssh.Dial("tcp", addr, cfg)
if err != nil {
return nil, fmt.Errorf("连接服务器失败: %w", err)
return nil, fmt.Errorf("连接服务器失败(%s): %w", addr, err)
}
return client, nil
}