-1
有人可以幫我登錄到SSH併發送簡單的ls
命令?這裏是我的代碼:如何登錄遠程SSH服務器併發送命令與libssh和[C]
你能幫我嗎?這裏是我的代碼:
#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命令......
什麼是您的問題? –
你知道你可以用ssh遠程執行命令嗎?例如'ssh 123.456.789.123 ls'? – vault
我的代碼不起作用。保險箱我知道我可以用tash bash來做,但我需要C程序 –