-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.cpp
More file actions
264 lines (229 loc) · 7.87 KB
/
ssh.cpp
File metadata and controls
264 lines (229 loc) · 7.87 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "ssh.h"
#include <arpa/inet.h>
#include <libssh2.h>
#include <libssh2_sftp.h>
#include <fstream>
#include <iostream>
/**
* Establish SFTP channel with the server.
*
* @param sock socket handler.
* @param session pointer to the SSH session.
* @param SshArg SSH task arguments.
* @return Return 0 if success, other values for error exit.
*/
int sftpChannel(int sock, LIBSSH2_SESSION *session, SshArg &arg);
/**
* Establish command executing channel with the server.
*
* @param sock socket handler.
* @param session pointer to the SSH session.
* @param SshArg SSH task arguments.
* @return Return 0 if success, other values for error exit.
*/
int cmdChannel(int sock, LIBSSH2_SESSION *session, SshArg &arg);
/**
* Use select to wait on socket.
*
* @param sock socket handler.
* @param session pointer to the SSH session.
* @return Return 0 if success, other values for error exit.
*/
int waitSocket(int sock, LIBSSH2_SESSION *session);
int sshInit() {
int rc = libssh2_init(0);
if (rc != 0) {
fprintf(stderr, "libssh2 initialization failed\n");
}
return 0;
}
int sshConn(SshArg &arg) {
int rc;
// Establish SSH connection
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
fprintf(stderr, "socket failed\n");
libssh2_exit();
return 1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(arg.port);
sin.sin_addr.s_addr = inet_addr(arg.ipaddr.c_str());
if (connect(sock, (struct sockaddr *)(&sin), sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "connect failed\n");
close(sock);
libssh2_exit();
return 1;
}
// Setup SSH session
LIBSSH2_SESSION *session = libssh2_session_init();
if (!session) {
fprintf(stderr, "libssh2_session_init failed\n");
close(sock);
libssh2_exit();
return 1;
}
// Turn on debug info when necessary (also need support from libssh2 library built with ENABLE_DEBUG_LOGGING=on)
// libssh2_trace(session, LIBSSH2_TRACE_SOCKET | LIBSSH2_TRACE_TRANS | LIBSSH2_TRACE_KEX | LIBSSH2_TRACE_AUTH | LIBSSH2_TRACE_CONN | LIBSSH2_TRACE_ERROR);
// Try different algorithms
// libssh2_session_method_pref(session, LIBSSH2_METHOD_KEX, "diffie-hellman-group-exchange-sha256");
// libssh2_session_method_pref(session, LIBSSH2_METHOD_HOSTKEY, "ssh-rsa");
// libssh2_session_method_pref(session, LIBSSH2_METHOD_MAC_CS, "hmac-sha1");
// libssh2_session_method_pref(session, LIBSSH2_METHOD_MAC_SC, "hmac-sha1");
rc = libssh2_session_handshake(session, sock);
if (rc != 0) {
fprintf(stderr, "libssh2_session_handshake failed\n");
close(sock);
libssh2_session_free(session);
libssh2_exit();
return 1;
}
rc = libssh2_userauth_password(session, arg.username.c_str(), arg.password.c_str());
if (rc != 0) {
fprintf(stderr, "libssh2_userauth_password failed\n");
close(sock);
libssh2_session_free(session);
libssh2_exit();
return 1;
}
if (arg.numSftpPerSsh > 0) {
sftpChannel(sock, session, arg);
}
if (arg.numCmdPerSsh > 0) {
cmdChannel(sock, session, arg);
}
// Close SSH resources
libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session);
close(sock);
libssh2_exit();
return 0;
}
int sftpChannel(int sock, LIBSSH2_SESSION *session, SshArg &arg) {
for (int i = 0; i < arg.numSftpPerSsh; ++i) {
// Setup SFTP
LIBSSH2_SFTP *sftp = libssh2_sftp_init(session);
if (!sftp) {
fprintf(stderr, "libssh2_sftp_init failed\n");
close(sock);
libssh2_session_free(session);
libssh2_exit();
return 1;
}
// Download remote file to local
if (arg.enableDownload) {
LIBSSH2_SFTP_HANDLE *handle = libssh2_sftp_open(sftp, arg.remoteFilePath.c_str(), LIBSSH2_FXF_READ, 0);
if (!handle) {
fprintf(stderr, "libssh2_sftp_open failed\n");
libssh2_sftp_shutdown(sftp);
close(sock);
libssh2_session_free(session);
libssh2_exit();
return 1;
}
std::ofstream fout(arg.localFilePath, std::ios::out | std::ios::binary);
if (!fout.good()) {
fprintf(stderr, "Failed to open local file for writing\n");
libssh2_sftp_close(handle);
libssh2_sftp_shutdown(sftp);
close(sock);
libssh2_session_free(session);
libssh2_exit();
return 1;
}
// Write to local file
char buffer[1048576]; // buffer size
int len = 0;
while ((len = libssh2_sftp_read(handle, buffer, sizeof(buffer))) > 0) {
fout.write(buffer, len);
}
fout.close();
libssh2_sftp_close(handle);
}
// Close SFTP session
libssh2_sftp_shutdown(sftp);
}
return 0;
}
int cmdChannel(int sock, LIBSSH2_SESSION *session, SshArg &arg) {
LIBSSH2_CHANNEL *channel;
int rc;
for (int i = 0; i < arg.numCmdPerSsh; ++i) {
// Open channel
while ((channel = libssh2_channel_open_session(session)) == NULL &&
libssh2_session_last_error(session, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN) {
waitSocket(sock, session);
}
if (!channel) {
fprintf(stderr, "libssh2_channel_open_session failed\n");
return 1;
}
// Execute command on remote server
while ((rc = libssh2_channel_exec(channel, arg.command.c_str())) == LIBSSH2_ERROR_EAGAIN) {
waitSocket(sock, session);
}
if (rc) {
fprintf(stderr, " libssh2_channel_exec failed\n");
return 1;
}
// Read remote output
do {
char buffer[1024];
rc = libssh2_channel_read(channel, buffer, sizeof(buffer));
if (rc > 0) {
// Postive return value means the actual number of bytes read
if (arg.renderOutput) {
fprintf(stdout, "Read from remote:\n");
for (int j = 0; j < rc; ++j) {
fputc(buffer[j], stdout);
}
fflush(stdout);
}
} else if (rc < 0) {
// Negative return value means an error
if (rc == LIBSSH2_ERROR_EAGAIN) {
waitSocket(sock, session);
} else {
fprintf(stderr, "libssh2_channel_read returned %d\n", rc);
break;
}
}
} while (rc > 0); // Exit on 0 (no payload data was read) or negative (failure)
// Close channel
char *exitSignal = static_cast<char *>(malloc(5 * sizeof(char)));
while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
waitSocket(sock, session);
}
if (rc == 0) {
libssh2_channel_get_exit_signal(channel, &exitSignal, NULL, NULL, NULL, NULL, NULL);
}
if (exitSignal) {
fprintf(stdout, "\nGot signal: %s\n", exitSignal);
}
libssh2_channel_free(channel);
}
return 0;
}
int waitSocket(int sock, LIBSSH2_SESSION *session) {
fd_set fd;
fd_set *fdWrite = NULL;
fd_set *fdRead = NULL;
int dir;
FD_ZERO(&fd);
FD_SET(sock, &fd);
// Direction
dir = libssh2_session_block_directions(session);
if (dir & LIBSSH2_SESSION_BLOCK_INBOUND) {
fdRead = &fd;
}
if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) {
fdWrite = &fd;
}
struct timeval timeout = {
.tv_sec = 10,
.tv_usec = 0,
};
int rc = select(sock + 1, fdRead, fdWrite, NULL, &timeout);
return rc;
}