如何使用libssh和[C]登录远程SSH服务器并发送命令

时间:2015-05-07 11:44:11

标签: c linux ssh libssh

有人可以帮我登录ssh并发送简单的ls命令吗?这是我的代码:

你可以帮帮我吗?这是我的代码:

#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h> 
int main()
{
  ssh_session my_ssh_session;
  int verbosity = SSH_LOG_PROTOCOL;
  int rc;
  int port = 22;
  char user = "root";
  char pass = "password";
  my_ssh_session = ssh_new();
  if (my_ssh_session == NULL)
    exit(-1);
  ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "192.168.1.100");
  ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
  ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, user);
  ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);

  rc = ssh_userauth_password(my_ssh_session,NULL,pass);
  if (rc == SSH_AUTH_ERROR)
  {
    fprintf(stderr, "Error connecting to localhost: %s\n",
            ssh_get_error(my_ssh_session));
    exit(-1);
  }


  rc = ssh_channel_request_exec(my_ssh_session, "ls -l");
  if (rc != SSH_OK)
  {
    ssh_channel_close(my_ssh_session);
    ssh_channel_free(my_ssh_session);
    return rc;
  }

  ssh_disconnect(my_ssh_session);
  ssh_free(my_ssh_session);

}

以下是构建时返回的上述代码:

"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/root/NetBeansProjects/SSH connect'
"/usr/bin/gmake"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/ssh_connect
gmake[2]: Entering directory `/root/NetBeansProjects/SSH connect'
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/main.o.d"
gcc -lssh   -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/main.o.d" -o build/Debug/GNU-Linux-x86/main.o main.c
main.c: In function 'main':
main.c:10:15: warning: initialization makes integer from pointer without a cast [enabled by default]
   char user = "root";
               ^
main.c:11:15: warning: initialization makes integer from pointer without a cast [enabled by default]
   char pass = "password";
               ^
main.c:17:3: warning: passing argument 3 of 'ssh_options_set' makes pointer from integer without a cast [enabled by default]
   ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, user);
   ^
In file included from main.c:1:0:
/usr/include/libssh/libssh.h:406:16: note: expected 'const void *' but argument is of type 'char'
 LIBSSH_API int ssh_options_set(ssh_session session, enum ssh_options_e type,
                ^
main.c:20:3: warning: passing argument 3 of 'ssh_userauth_password' makes pointer from integer without a cast [enabled by default]
   rc = ssh_userauth_password(my_ssh_session,NULL,pass);
   ^
In file included from main.c:1:0:
/usr/include/libssh/libssh.h:456:16: note: expected 'const char *' but argument is of type 'char'
 LIBSSH_API int ssh_userauth_password(ssh_session session, const char *username, const char *password);
                ^
main.c:29:3: warning: passing argument 1 of 'ssh_channel_request_exec' from incompatible pointer type [enabled by default]
   rc = ssh_channel_request_exec(my_ssh_session, "ls -l");
   ^
In file included from main.c:1:0:
/usr/include/libssh/libssh.h:345:16: note: expected 'ssh_channel' but argument is of type 'ssh_session'
 LIBSSH_API int ssh_channel_request_exec(ssh_channel channel, const char *cmd);
                ^
main.c:32:5: warning: passing argument 1 of 'ssh_channel_close' from incompatible pointer type [enabled by default]
     ssh_channel_close(my_ssh_session);
     ^
In file included from main.c:1:0:
/usr/include/libssh/libssh.h:329:16: note: expected 'ssh_channel' but argument is of type 'ssh_session'
 LIBSSH_API int ssh_channel_close(ssh_channel channel);
                ^
main.c:33:5: warning: passing argument 1 of 'ssh_channel_free' from incompatible pointer type [enabled by default]
     ssh_channel_free(my_ssh_session);
     ^
In file included from main.c:1:0:
/usr/include/libssh/libssh.h:330:17: note: expected 'ssh_channel' but argument is of type 'ssh_session'
 LIBSSH_API void ssh_channel_free(ssh_channel channel);
                 ^
mkdir -p dist/Debug/GNU-Linux-x86
gcc -lssh    -o dist/Debug/GNU-Linux-x86/ssh_connect build/Debug/GNU-Linux-x86/main.o 
gmake[2]: Leaving directory `/root/NetBeansProjects/SSH connect'
gmake[1]: Leaving directory `/root/NetBeansProjects/SSH connect'

BUILD SUCCESSFUL (total time: 114ms)

我只是登录并执行ls命令......

1 个答案:

答案 0 :(得分:6)

这对我有用:

#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>

void free_channel(ssh_channel channel) {
    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);
}

void free_session(ssh_session session) {
    ssh_disconnect(session);
    ssh_free(session);
}

void error(ssh_session session) {
    fprintf(stderr, "Error: %s\n", ssh_get_error(session));
    free_session(session);
    exit(-1);
}

int main() {

    ssh_session session;
    ssh_channel channel;
    int rc, port = 22;
    char buffer[1024];
    unsigned int nbytes;

    printf("Session...\n");
    session = ssh_new();
    if (session == NULL) exit(-1);

    ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
    ssh_options_set(session, SSH_OPTIONS_PORT, &port);
    ssh_options_set(session, SSH_OPTIONS_USER, "root");

    printf("Connecting...\n");
    rc = ssh_connect(session);
    if (rc != SSH_OK) error(session);

    printf("Password Autentication...\n");
    rc = ssh_userauth_password(session, NULL, "root");
    if (rc != SSH_AUTH_SUCCESS) error(session);

    printf("Channel...\n");
    channel = ssh_channel_new(session);
    if (channel == NULL) exit(-1);

    printf("Opening...\n");
    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK) error(session);

    printf("Executing remote command...\n");
    rc = ssh_channel_request_exec(channel, "ls -l");
    if (rc != SSH_OK) error(session);

    printf("Received:\n");
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0) {
        fwrite(buffer, 1, nbytes, stdout);
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }

    free_channel(channel);
    free_session(session);

    return 0;
}