-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.c
87 lines (74 loc) · 1.9 KB
/
ssh.c
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
#include "ssh.h"
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static ssh_session my_ssh_session;
static ssh_channel channel;
int new_ssh_session() {
my_ssh_session = ssh_new();
if (my_ssh_session == NULL) {
fprintf(stderr, "Error creating ssh session\n");
return SSH_ERROR;
}
return SSH_OK;
}
int connect_ssh_session(const char* user, const char* host) {
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, user);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host);
int ret = ssh_connect(my_ssh_session);
if (ret != SSH_OK) {
fprintf(stderr, "Error connecting to %s: %s\n", host,
ssh_get_error(my_ssh_session));
}
return ret;
}
int authenticate_ssh_session() {
int ret = ssh_userauth_publickey_auto(my_ssh_session, NULL, NULL);
if (ret == SSH_AUTH_ERROR) {
fprintf(stderr, "Authentication failed: %s\n",
ssh_get_error(my_ssh_session));
}
return ret;
}
int new_ssh_channel() {
channel = ssh_channel_new(my_ssh_session);
if (channel == NULL) {
fprintf(stderr, "Error creating ssh channel\n");
return SSH_ERROR;
}
return SSH_OK;
}
int open_ssh_channel() {
int ret = ssh_channel_open_session(channel);
if (ret != SSH_OK) {
fprintf(stderr, "Authentication failed: %s\n",
ssh_get_error(my_ssh_session));
}
return ret;
}
int send_command_to_ssh_channel(char* command) {
int ret = ssh_channel_request_exec(channel, command);
if (ret != SSH_OK) {
fprintf(stderr, "request channel exec: %s\n",
ssh_get_error(my_ssh_session));
}
return ret;
}
void send_channel_eof() {
ssh_channel_send_eof(channel);
}
void close_channel() {
ssh_channel_close(channel);
}
void free_channel() {
ssh_channel_free(channel);
}
void disconnect_session() {
ssh_disconnect(my_ssh_session);
}
void free_session() {
ssh_free(my_ssh_session);
}