1
/* Copyright (C) The libssh2 project and its contributors.
3
* Sample showing how to use libssh2 to execute a command remotely.
5
* The sample code has fixed values for host name, user name, password
8
* $ ./ssh2_exec 127.0.0.1 user password "uptime"
10
* SPDX-License-Identifier: BSD-3-Clause
13
#include "libssh2_setup.h"
16
#ifdef HAVE_SYS_SOCKET_H
17
#include <sys/socket.h>
22
#ifdef HAVE_NETINET_IN_H
23
#include <netinet/in.h>
25
#ifdef HAVE_ARPA_INET_H
33
static const char *hostname = "127.0.0.1";
34
static const char *commandline = "uptime";
35
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
36
static const char *privkey = "/home/username/.ssh/id_rsa";
37
static const char *username = "user";
38
static const char *password = "password";
40
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
42
struct timeval timeout;
45
fd_set *writefd = NULL;
46
fd_set *readfd = NULL;
55
#pragma GCC diagnostic push
56
#pragma GCC diagnostic ignored "-Wsign-conversion"
58
FD_SET(socket_fd, &fd);
60
#pragma GCC diagnostic pop
63
/* now make sure we wait in the correct direction */
64
dir = libssh2_session_block_directions(session);
66
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
69
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
72
rc = select((int)(socket_fd + 1), readfd, writefd, NULL, &timeout);
77
int main(int argc, char *argv[])
80
libssh2_socket_t sock;
81
struct sockaddr_in sin;
82
const char *fingerprint;
84
LIBSSH2_SESSION *session = NULL;
85
LIBSSH2_CHANNEL *channel;
87
char *exitsignal = (char *)"none";
88
ssize_t bytecount = 0;
90
LIBSSH2_KNOWNHOSTS *nh;
96
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
98
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
104
hostname = argv[1]; /* must be ip address only */
113
commandline = argv[4];
116
rc = libssh2_init(0);
118
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
122
hostaddr = inet_addr(hostname);
124
/* Ultra basic "connect to port 22 on localhost". Your code is
125
* responsible for creating the socket establishing the connection
127
sock = socket(AF_INET, SOCK_STREAM, 0);
128
if(sock == LIBSSH2_INVALID_SOCKET) {
129
fprintf(stderr, "failed to create socket.\n");
133
sin.sin_family = AF_INET;
134
sin.sin_port = htons(22);
135
sin.sin_addr.s_addr = hostaddr;
136
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
137
fprintf(stderr, "failed to connect.\n");
141
/* Create a session instance */
142
session = libssh2_session_init();
144
fprintf(stderr, "Could not initialize SSH session.\n");
148
/* tell libssh2 we want it all done non-blocking */
149
libssh2_session_set_blocking(session, 0);
151
/* ... start it up. This will trade welcome banners, exchange keys,
152
* and setup crypto, compression, and MAC layers
154
while((rc = libssh2_session_handshake(session, sock)) ==
155
LIBSSH2_ERROR_EAGAIN);
157
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
161
nh = libssh2_knownhost_init(session);
163
/* eeek, do cleanup here */
167
/* read all hosts from here */
168
libssh2_knownhost_readfile(nh, "known_hosts",
169
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
171
/* store all known hosts to here */
172
libssh2_knownhost_writefile(nh, "dumpfile",
173
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
175
fingerprint = libssh2_session_hostkey(session, &len, &type);
177
struct libssh2_knownhost *host;
178
int check = libssh2_knownhost_checkp(nh, hostname, 22,
180
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
181
LIBSSH2_KNOWNHOST_KEYENC_RAW,
184
fprintf(stderr, "Host check: %d, key: %s\n", check,
185
(check <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH) ?
186
host->key : "<none>");
189
* At this point, we could verify that 'check' tells us the key is
194
/* eeek, do cleanup here */
197
libssh2_knownhost_free(nh);
199
if(strlen(password) != 0) {
200
/* We could authenticate via password */
201
while((rc = libssh2_userauth_password(session, username, password)) ==
202
LIBSSH2_ERROR_EAGAIN);
204
fprintf(stderr, "Authentication by password failed.\n");
209
/* Or by public key */
210
while((rc = libssh2_userauth_publickey_fromfile(session, username,
213
LIBSSH2_ERROR_EAGAIN);
215
fprintf(stderr, "Authentication by public key failed.\n");
221
libssh2_trace(session, ~0);
224
/* Exec non-blocking on the remote host */
226
channel = libssh2_channel_open_session(session);
228
libssh2_session_last_error(session, NULL, NULL, 0) !=
229
LIBSSH2_ERROR_EAGAIN)
231
waitsocket(sock, session);
234
fprintf(stderr, "Error\n");
237
while((rc = libssh2_channel_exec(channel, commandline)) ==
238
LIBSSH2_ERROR_EAGAIN) {
239
waitsocket(sock, session);
242
fprintf(stderr, "exec error\n");
247
/* loop until we block */
250
nread = libssh2_channel_read(channel, buffer, sizeof(buffer));
254
fprintf(stderr, "We read:\n");
255
for(i = 0; i < nread; ++i)
256
fputc(buffer[i], stderr);
257
fprintf(stderr, "\n");
260
if(nread != LIBSSH2_ERROR_EAGAIN)
261
/* no need to output this for the EAGAIN case */
262
fprintf(stderr, "libssh2_channel_read returned %ld\n",
268
/* this is due to blocking that would occur otherwise so we loop on
270
if(nread == LIBSSH2_ERROR_EAGAIN) {
271
waitsocket(sock, session);
277
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
278
waitsocket(sock, session);
281
exitcode = libssh2_channel_get_exit_status(channel);
282
libssh2_channel_get_exit_signal(channel, &exitsignal,
283
NULL, NULL, NULL, NULL, NULL);
287
fprintf(stderr, "\nGot signal: %s\n", exitsignal);
289
fprintf(stderr, "\nEXIT: %d bytecount: %ld\n",
290
exitcode, (long)bytecount);
292
libssh2_channel_free(channel);
298
libssh2_session_disconnect(session, "Normal Shutdown");
299
libssh2_session_free(session);
302
if(sock != LIBSSH2_INVALID_SOCKET) {
304
LIBSSH2_SOCKET_CLOSE(sock);
307
fprintf(stderr, "all done\n");