forked from skyformat99/ssh_exec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
348 lines (285 loc) · 7.92 KB
/
Copy pathmain.cpp
File metadata and controls
348 lines (285 loc) · 7.92 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <iostream>
#include <libssh2.h>
#include <unistd.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <fstream>
#include <vector>
#include <stdio.h>
LIBSSH2_SESSION * session;
LIBSSH2_CHANNEL * channel;
int rc;
/**
* ssh参数
*/
struct ssh_config{
unsigned int port;
std::string username;
std::string password;
std::string address;
std::string hosts_file;
std::string command;
};
/**
* 多台主机
*/
struct hosts{
int sock;
unsigned int port;
std::string username;
std::string password;
std::string address;
};
/**
* 处理错误
* @param opt
*/
void error_handle(struct ssh_config & opt){
std::string start("\033[41;36m");
std::string end("\033[0m");
if(opt.username.empty()){
std::cout <<start<<"[!]Username is empty "<<end << std::endl;
}
if(opt.password.empty()){
std::cout <<start << "[!]Password is empty "<<end << std::endl;
}
if(opt.address.empty()){
std::cout << start <<"[!]Host address is empty "<<end << std::endl;
}
}
/**
* 加载主机文件
* @param hosts_file
* @return
* 格式:[IP] : [用户名] : [密码] : [端口]
*/
std::vector<struct hosts> loadhosts(std::string hosts_file){
std::vector<struct hosts> remote_hosts;
std::fstream IO(hosts_file);
if(!IO.is_open()){
std::cout << "Can't Read Hosts file :" << hosts_file << std::endl;
exit(EXIT_FAILURE);
}
while(!IO.eof()){
struct hosts tmp;
char address[100],port[100],username[100],password[100];
std::string line;
getline(IO,line);
sscanf(line.c_str(),"%s - %s - %s - %s",address,username,password,port);
tmp.address = address;
tmp.username = username;
tmp.password = password;
tmp.port = atoi(port);
remote_hosts.push_back(tmp);
}
IO.close();
// std::cout << bash_connect << std::endl;
return remote_hosts;
}
/**
* 输出帮助信息
* @param program_name
*/
void help(char * program_name){
std::cout << "Usage: " << program_name << " -t <Address> -u <Username> -p <Password> -e <Bash_file>"<<std::endl<<std::endl;
std::cout << program_name << " -e <Bash Script> -l <Hosts File>"<<std::endl<< std::endl;
std::cout
<<"\t-t \t<Address>\tSSH主机地址"<<std::endl
<<"\t-u \t<Username>\tSSH登录用户名"<<std::endl
<<"\t-p \t<Password>\tSSH登录密码"<<std::endl
<<"\t-e \t<Bash_file>\tSSH执行脚本"<<std::endl
<<"\t-s \t<Port>\tSSH端口(默认22)"<<std::endl
<<"\t-c \t<Command>\t执行命令"<<std::endl
<<"\t-l \t<Hosts File>\t多台主机文件"<<std::endl
<<"\t-h \t<Help>\t输出帮助信息"<<std::endl
<<"\t-v \t<Version>\t输出版本信息"<<std::endl<<std::endl;
}
/**
* 读取bash file
* @param filename
* @return
*/
std::string get_bash_file(std::string filename){
std::fstream IO(filename);
std::string bash_connect;
if(!IO.is_open()){
std::cout << "Can't Read Bash file :" << filename << std::endl;
exit(EXIT_FAILURE);
}
while(!IO.eof()){
std::string line;
getline(IO,line);
bash_connect+="\n"+line;
}
IO.close();
// std::cout << bash_connect << std::endl;
return bash_connect;
}
/**
* 执行命令
* @param sock
* @param opt
*/
void ssh_exec(int sock,struct ssh_config & opt){
libssh2_channel_exec(channel,opt.command.c_str());
char result[1024] ="";
std::string commandRes="";
while(libssh2_channel_read(channel,result,1000) > 0){
std::cout << result;
commandRes+=result;
}
std::cout << std::endl;
}
/**
* 连接SSH
* @param host
* @return
*/
int connect_ssh(struct hosts & host){
int sock = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in ser_sock;
ser_sock.sin_addr.s_addr = inet_addr(host.address.c_str());
ser_sock.sin_port = htons(host.port);
ser_sock.sin_family = AF_INET;
if(connect(sock,(sockaddr *)&ser_sock,sizeof(ser_sock)) == -1){
std::cout <<"[!]Can't Connect to " << inet_ntoa(ser_sock.sin_addr) << std::endl;
return -1;
}
session = libssh2_session_init();
if(!session){
return -1;
}
rc = libssh2_session_handshake(session,sock);
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
}
if (libssh2_userauth_password(session, host.username.c_str(), host.password.c_str())) {
fprintf(stderr, "Authentication by password failed.\n");
return -1;
}
channel = libssh2_channel_open_session(session);
return sock;
}
/**
* 主函数
* @param argc
* @param argv
* @return
*/
int main(int argc,char * argv[]) {
int opt; // 用于匹配参数
// 多台主机结构体
std::vector<struct hosts> hosts;
// 输出彩色~
std::string start("\033[41;36m");
std::string end("\033[0m");
// 单台主机SSH配置
struct ssh_config sshConfig;
// 默认端口 22
sshConfig.port = 22;
// 参数序列
std::string options("t:u:p:l:e:s:r:c:h::v::");
//初始化libssh2库
rc = libssh2_init(0);
if(rc !=0){
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}
/*
/**
* 处理参数
*/
while((opt = getopt(argc,argv,options.data()))!=-1){
switch(opt){
// 加载bash脚本
case 'e':
sshConfig.command = get_bash_file(optarg);
break;
// 从配置文件读取多个主机
case 'l':
sshConfig.hosts_file = optarg;
break;
// 设置单个主机
case 't':
sshConfig.address = optarg;
break;
// 设置执行命令
case 'c':
sshConfig.command = optarg;
break;
// 设置用户名
case 'u':
sshConfig.username = optarg;
break;
// 设置密码
case 'p':
sshConfig.password = optarg;
break;
// 设置端口
case 's':
sshConfig.port = atoi(optarg);
break;
// 输出版本信息
case 'v':
std::cout << "[*]Version : 0.01 By payloads@aliyun.com" << std::endl;
exit(EXIT_FAILURE);
// 默认输出帮助信息
default:
help(argv[0]);
exit(EXIT_FAILURE);
}
}
/**
* 如果没有参数则输出帮助信息
*/
// std::cout << optind << std::endl;
if(optind < 5 ){
help(argv[0]);
exit(EXIT_FAILURE);
}
/**
* 处理错误
*/
error_handle(sshConfig);
/**
* 批量模式
*/
if(!sshConfig.hosts_file.empty()){
/**
* 获取多台主机
*/
hosts = loadhosts(sshConfig.hosts_file);
for (int i = 0; i < hosts.size(); ++i) {
// 循环获取每个主机的socket套接字
int sock = connect_ssh(hosts[i]);
if(sock != -1){
// 执行
ssh_exec(sock,sshConfig);
}
}
}else{
/**
* 单任务模式
*/
struct hosts host;
sshConfig.username.swap(host.username); //用户名
sshConfig.address.swap(host.address); //IP地址
host.port = sshConfig.port; //端口
sshConfig.password.swap(host.password); //密码
// 获取套接字
int sock = connect_ssh(host);
if(sock != -1){
// 执行命令
ssh_exec(sock,sshConfig);
}
}
/**
* 释放资源
*/
libssh2_channel_free(channel);
libssh2_session_disconnect(session,"Bye");
libssh2_session_free(session);
return 0;
}