// example.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "example.h" #include "libssh2_config.h" #include #include "libssh2_sftp.h" #include #include #ifdef HAVE_STDLIB_H #include #endif #include #include #include #include #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; }