-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
193 lines (159 loc) · 4.69 KB
/
main.go
File metadata and controls
193 lines (159 loc) · 4.69 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"strings"
"golang.org/x/crypto/ssh"
"github.com/pkg/sftp"
)
type Config struct {
Method string `json:"method"`
Host string `json:"host"`
Port string `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
LocalDir string `json:"local_dir"`
}
func main() {
configPath := flag.String("config", "", "Path to config.json")
flag.Parse()
var cfg Config
reader := bufio.NewReader(os.Stdin)
if *configPath != "" {
file, err := os.Open(*configPath)
if err != nil {
log.Fatal("Failed to open config file:", err)
}
defer file.Close()
if err := json.NewDecoder(file).Decode(&cfg); err != nil {
log.Fatal("Failed to parse config file:", err)
}
fmt.Println("Loaded config.json successfully")
} else {
fmt.Print("Choose method (ssh/sftp): ")
cfg.Method, _ = reader.ReadString('\n')
cfg.Method = strings.TrimSpace(strings.ToLower(cfg.Method))
fmt.Print("Enter host (domain or IP): ")
cfg.Host, _ = reader.ReadString('\n')
cfg.Host = strings.TrimSpace(cfg.Host)
ips, err := net.LookupHost(cfg.Host)
if err != nil {
log.Fatal("DNS lookup failed:", err)
}
fmt.Printf("Resolved host %s → %v\n", cfg.Host, ips)
fmt.Print("Proceed with this host? (y/n): ")
confirm, _ := reader.ReadString('\n')
if strings.TrimSpace(confirm) != "y" {
fmt.Println("Aborted by user.")
return
}
if cfg.Method == "sftp" {
fmt.Print("Enter port (default 22): ")
cfg.Port, _ = reader.ReadString('\n')
cfg.Port = strings.TrimSpace(cfg.Port)
if cfg.Port == "" {
cfg.Port = "22"
}
} else {
cfg.Port = "22"
}
fmt.Print("Enter username: ")
cfg.Username, _ = reader.ReadString('\n')
cfg.Username = strings.TrimSpace(cfg.Username)
fmt.Print("Enter password: ")
cfg.Password, _ = reader.ReadString('\n')
cfg.Password = strings.TrimSpace(cfg.Password)
fmt.Print("Enter local download folder (default ./): ")
cfg.LocalDir, _ = reader.ReadString('\n')
cfg.LocalDir = strings.TrimSpace(cfg.LocalDir)
if cfg.LocalDir == "" {
cfg.LocalDir = "./"
}
}
// Always ask for ADE view and remote file (even if config is used)
fmt.Print("Enter your ADE lsview name: ")
adeView, _ := reader.ReadString('\n')
adeView = strings.TrimSpace(adeView)
fmt.Print("Enter remote file path or filename: ")
remoteFile, _ := reader.ReadString('\n')
remoteFile = strings.TrimSpace(remoteFile)
// SSH config
config := &ssh.ClientConfig{
User: cfg.Username,
Auth: []ssh.AuthMethod{ssh.Password(cfg.Password)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
address := fmt.Sprintf("%s:%s", cfg.Host, cfg.Port)
client, err := ssh.Dial("tcp", address, config)
if err != nil {
log.Fatal("Failed to connect:", err)
}
defer client.Close()
var remotePath string
if strings.Contains(remoteFile, "/") {
remotePath = remoteFile
} else {
baseDir := fmt.Sprintf("/scratch/%s/view_storage/%s", cfg.Username, adeView)
session, _ := client.NewSession()
defer session.Close()
findCmd := fmt.Sprintf(`bash -c "find %s -type f -name '%s' "`, baseDir, remoteFile)
output, _ := session.CombinedOutput(findCmd)
paths := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(paths) == 0 || paths[0] == "" {
log.Fatal("File not found in ADE view")
}
// Show choices
fmt.Println("Found matches:")
for i, p := range paths {
fmt.Printf("[%d] %s\n", i+1, p)
}
fmt.Print("Choose file number: ")
choiceStr, _ := reader.ReadString('\n')
choiceStr = strings.TrimSpace(choiceStr)
var choice int
fmt.Sscanf(choiceStr, "%d", &choice)
if choice < 1 || choice > len(paths) {
log.Fatal("Invalid choice")
}
remotePath = paths[choice-1]
}
localPath := filepath.Join(cfg.LocalDir, filepath.Base(remotePath))
if *configPath == "" {
fmt.Printf("Download %s → %s ? (y/n): ", remotePath, localPath)
confirm, _ := reader.ReadString('\n')
if strings.TrimSpace(confirm) != "y" {
fmt.Println("Aborted.")
return
}
}
if cfg.Method == "sftp" {
sftpClient, err := sftp.NewClient(client)
if err != nil {
log.Fatal("SFTP failed:", err)
}
defer sftpClient.Close()
srcFile, err := sftpClient.Open(remotePath)
if err != nil {
log.Fatal("Cannot open remote file:", err)
}
defer srcFile.Close()
localFile, err := os.Create(localPath)
if err != nil {
log.Fatal("Cannot create local file:", err)
}
defer localFile.Close()
_, err = srcFile.WriteTo(localFile)
if err != nil {
log.Fatal("Download failed:", err)
}
fmt.Println("Download completed:", localPath)
} else {
fmt.Println("SSH mode is for running commands, not file transfer. Use SFTP for downloads.")
}
}