123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- // example.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "example.h"
- #include "libssh2_config.h"
- #include <libssh2.h>
- #include "libssh2_sftp.h"
- #include <winsock2.h>
- #include <sys/types.h>
- #ifdef HAVE_STDLIB_H
- #include <stdlib.h>
- #endif
- #include <fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- #include <ctype.h>
- #pragma comment(lib,"libssh2.lib")
- #pragma comment(lib, "ws2_32.lib")
- #include "WinSsh2Proc.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 唯一的应用程序对象
- CWinApp theApp;
- using namespace std;
- int download(std::string ip, unsigned short port, std::string username, std::string password, std::string sftppath, std::string localpath);
- static int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
- {
- struct timeval timeout;
- int rc;
- fd_set fd;
- fd_set *writefd = NULL;
- fd_set *readfd = NULL;
- int dir;
- timeout.tv_sec = 10;
- timeout.tv_usec = 0;
- FD_ZERO(&fd);
- FD_SET(socket_fd, &fd);
- /* now make sure we wait in the correct direction */
- dir = libssh2_session_block_directions(session);
- if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
- readfd = &fd;
- if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
- writefd = &fd;
- rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
- return rc;
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- #ifdef WIN32
- WSADATA wsadata;
- int err;
- err = WSAStartup(MAKEWORD(2, 0), &wsadata);
- if(err != 0) {
- fprintf(stderr, "WSAStartup failed with error: %d\n", err);
- return 1;
- }
- #endif
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: MFC 初始化失败\n"));
- nRetCode = 1;
- }
- else
- {
- // TODO: 在此处为应用程序的行为编写代码。
- /*
- download("10.201.251.254", 22,
- "wjf",
- "wjf2019",
- "/scbc_data/wjf/rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/V8-T841T01-LF1V001.img",
- "D:\\sat\\a.img");
- */
- CWinSsh2Proc winssh2;
- if ( winssh2.InitSocket() )
- {
- if ( winssh2.ssh2_connect("10.201.251.254", "wjf", "wjf2019") )
- {
- std::string str;
- // sha256sum
- winssh2.ssh2_execute_command("ls", str);
- winssh2.ssh2_execute_command("md5sum rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/V8-T841T01-LF1V001.img", str, 8000);
- winssh2.ssh2_execute_command("sha1sum rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/V8-T841T01-LF1V001.img", str, 10000);
- winssh2.ssh2_execute_command("sha256sum rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/V8-T841T01-LF1V001.img", str, 12000);
- str.size();
- //printf("MD5=%s\n", str.c_str());
- }
- winssh2.ssh2_sftp_download("rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/build.prop", "D:\\sat\\b.img");
- }
- }
- system("pause");
- return nRetCode;
- }
- int download(std::string ip, unsigned short port, std::string username, std::string password, std::string sftppath, std::string localpath)
- {
- unsigned long hostaddr;
- int sock, i, auth_pw = 0;
- struct sockaddr_in sin;
- const char *fingerprint;
- char *userauthlist;
- LIBSSH2_SESSION *session;
- int rc;
- LIBSSH2_SFTP *sftp_session;
- LIBSSH2_SFTP_HANDLE *sftp_handle;
- hostaddr = inet_addr(ip.c_str()); //hostaddr = htonl(0x7F000001);
- /*
- * The application code is responsible for creating the socket
- * and establishing the connection
- */
- sock = socket(AF_INET, SOCK_STREAM, 0);
- sin.sin_family = AF_INET;
- sin.sin_port = htons(port);
- sin.sin_addr.s_addr = hostaddr;
- if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
- return -1;
- }
- /* Create a session instance
- */
- session = libssh2_session_init();
- if (!session)
- return -1;
- /* Since we have set non-blocking, tell libssh2 we are blocking */
- libssh2_session_set_blocking(session, 1);
- /* ... start it up. This will trade welcome banners, exchange keys,
- * and setup crypto, compression, and MAC layers
- */
- rc = libssh2_session_handshake(session, sock);
- if (rc) {
- return -1;
- }
- /* At this point we havn't yet authenticated. The first thing to do
- * is check the hostkey's fingerprint against our known hosts Your app
- * may have it hard coded, may go to a file, may present it to the
- * user, that's your call
- */
- fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
- for (int i = 0; i < 20; i++) {
- unsigned char c = fingerprint[i];
- int nT = c;
- }
- /* check what authentication methods are available */
- userauthlist = libssh2_userauth_list(session, username.c_str(), username.length());
- if (strstr(userauthlist, "password") == NULL)
- {
- goto shutdown;
- }
- /* We could authenticate via password */
- if (libssh2_userauth_password(session, username.c_str(), password.c_str())) {
- goto shutdown;
- }
- sftp_session = libssh2_sftp_init(session);
- if (!sftp_session) {
- goto shutdown;
- }
- /* Request a file via SFTP */
- sftp_handle = libssh2_sftp_open(sftp_session, sftppath.c_str(), LIBSSH2_FXF_READ, 0);
- if (!sftp_handle) {
- goto shutdown;
- }
- FILE *stream;
- if (fopen_s(&stream, localpath.c_str(), "wb") == 0)
- {
- do {
- char mem[1024];
- /* loop until we fail */
- rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
- if (rc > 0) {
- //从内存到磁盘
- fwrite(mem, 1, rc, stream);
- }
- else {
- break;
- }
- } while (1);
- fclose(stream);
- }
- else {
-
- }
- libssh2_sftp_close(sftp_handle);
- libssh2_sftp_shutdown(sftp_session);
- shutdown:
- libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
- libssh2_session_free(session);
- closesocket(sock);//INVALID_SOCKET
- return 0;
- }
|