- 论坛徽章:
- 0
|
本帖最后由 baidu9833 于 2010-05-11 14:30 编辑
快要崩溃了... 想破头 用了各种办法都无法解决 希望各位大侠不吝赐教啊- /* purpose: to check out the functions of libssh2 */
- #include "libssh2_config.h"
- #include <libssh2.h>
- #include <libssh2_sftp.h>
- #ifdef HAVE_NETINET_IN_H
- #include <netinet/in.h>
- #endif
- #ifdef HAVE_SYS_SOCKET_H
- #include <sys/socket.h>
- #endif
- #ifdef HVAE_UNISTD_H
- #include <unistd.h>
- #endif
- #ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
- #endif
- #include <sys/types.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- #include <ctype.h>
- const char * username = "username";
- const char * password = "password";
- const char * keyfile1 = "~/.ssh/id_rsa.pub";
- const char * keyfile2 = "~/.ssh/id_rsa";
- int main(int argc, char *argv[])
- {
- /*初始化信息,包括主机地址,socket 临时变量等*/
- unsigned long hostaddr; /* to save host address */
- int sock, i, rc, auth_pw = 0; /* socket, temporary save_id, and the type of authority */
- struct sockaddr_in sin;
- /*command 的初始值*/
- char * command = "ls\n";
- /*用于存放读取到的buf的变量*/
- char buf[1024];
- memset(buf,0,1024);
- const char * fingerprint ; /* I don't know what's this */
- /*用于存放检测到的认证方式的变量*/
- char *userauthlist;
- /*会话*/
- LIBSSH2_SESSION *session;
- /*建立起的channel*/
- LIBSSH2_CHANNEL *channel;
- /*变量检测*/
- if (argc > 1)
- {
- hostaddr = inet_addr(argv[1]);
- }
- else
- {
- hostaddr = htonl(0x7F000001);
- }
- if (argc > 2)
- {
- username = argv[2];
- }
- if (argc > 3)
- {
- password = argv[3];
- }
- if (argc > 4)
- {
- command = argv[4];
- }
-
- /*创建socket*/
- sock = socket (AF_INET, SOCK_STREAM, 0);
- #ifndef WIN32
- fcntl(sock,F_SETFL, 0);
- #endif
- /*赋值ip信息*/
- sin.sin_family = AF_INET;
- sin.sin_port = htons(22);
- sin.sin_addr.s_addr = hostaddr;
-
- /*连接服务器*/
- if (connect(sock, (struct sockaddr*)(&sin),
- sizeof(struct sockaddr_in)) != 0)
- {
- fprintf(stderr, "failed to connect to the server!\n");
- return -1;
- }
- /*初始化会话*/
- session = libssh2_session_init();
-
- /*创建会话*/
- if (libssh2_session_startup(session, sock))
- {
- fprintf(stderr, "Failure establishing SSH session\n");
- return -1;
- }
-
- fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
- printf("Fingerprint: ");
- for(i = 0; i < 16;i++)
- {
- printf("%02X ", (unsigned char )fingerprint[i]);
- }
- printf("\n");
- */
- /*获得服务器端鉴权方式-- 一共三种 :密码,文件,键盘*/
- userauthlist = libssh2_userauth_list(session, username, strlen(username));
- printf("Authentication methods: %s\n",userauthlist);
-
- /*存在密码鉴权*/
- if (strstr(userauthlist, "password") != NULL)
- {
- auth_pw |= 1;
- }
- /*存在键盘鉴权*/
- if (strstr(userauthlist, "keyboard-interactive") != NULL)
- {
- auth_pw |= 2;
- }
- /*存在文件鉴权*/
- if (strstr(userauthlist, "publickey") != NULL)
- {
- auth_pw |= 4;
- }
- /*密码鉴权*/
- if (auth_pw & 1)
- {
- if (libssh2_userauth_password(session, username, password))
- {
- printf("\tAuthentication by password failed!\n");
- goto shutdown;
- }
- else
- {
- printf("\tAuthentication by password succeeded.\n");
- }
- }
- /*文件鉴权,由于键盘方式比较麻烦,此处略去不计*/
- else if (auth_pw & 4)
- {
- if (libssh2_userauth_publickey_fromfile(session,username,keyfile1,keyfile2,password))
- {
- printf("\tAuthentication by public key failed! \n");
- goto shutdown;
- }
- else
- {
- printf("\tAuthentication by public key succeeded.\n");
- }
- }
- else
- {
- printf("no supported authentication method founded!\n");
- goto shutdown;
- }
- /*创建一个通道*/
- if (!(channel = libssh2_channel_open_session(session)))
- {
- fprintf(stderr, "Unable to open a session\n");
- goto shutdown;
- }
- /*此处进行的虚拟终端初始化,需要*/
- libssh2_channel_setenv(channel,"FOO","bar");
- if (libssh2_channel_request_pty(channel,"vanilla"))
- {
- fprintf(stderr,"Failed requesting pty\n");
- goto skip_shell;
- }
- /*此处隐去不用,经验证,不用它才能进行命令的发送,如果用它,那么就可以用读写的方式来进行交互*/
-
- if (libssh2_channel_shell(channel))
- {
- fprintf(stderr,"Unable to request shell on allocated pty");
- goto shutdown;
- }
-
-
- /* now do our jobs */
-
- /*发送命令到对端*/
- //rc = libssh2_channel_exec(channel,command);
- rc = libssh2_channel_write(channel,command,strlen(command));
-
-
- /*
- if (rc != 0)
- {
- fprintf(stderr,"Failed to execute the command pty\n");
- goto shutdown;
- }
- */
- /*rc = libssh2_channel_read(channel,buf,1023);*/
- /*
- rc = libssh2_session_get_blocking(session);
- printf("before set ,status is %d\n",rc);
- libssh2_session_set_blocking(session, 0);
- rc = libssh2_session_get_blocking(session);
- printf("after set ,status is %d\n",rc);
- */
- /*以上是用来进行测试blocking*/
- /*读出通道里的buf(可能需要多次读取),并将其打印出来*/
-
- rc = 1;
- while (rc > 0)
- {
- /* 如何对channel的buf进行检测 */
-
- rc = libssh2_channel_read(channel,buf,1023); /*就是此处,当ls命令执行完毕后,也就是channel中再无数据时,这时再去读取数据时会导致阻塞*/
- if (strstr(command,buf))
- continue;
- fprintf(stderr,"%s\n",buf);
- memset(buf,0,1024);
- }
- fprintf(stderr,"Did I do read the buf? %d\n",rc);
-
-
- skip_shell:
- if (channel)
- {
- libssh2_channel_free(channel);
- channel = NULL;
- }
- shutdown:
- libssh2_session_disconnect(session, "Normal shutdown, Thank you \n");
-
- libssh2_session_free(session);
-
- close(sock);
- }
复制代码 各位,情况是这样的,我写了个测试程序,用来往服务器端发送对端可识别的命令 如代码中的 char * command = "ls\n"; 目的是当执行完毕后,服务端打印出执行结果,并且退出循环,结束工作。现在变成了 服务端打印完结果后,程序就阻塞在该读取的地方,不能返回,希望各位帮我看看 帮我解决下啊。 |
|