文件传输libssh2 进行 sftp 文件传输

sftp  时间:2021-01-25  阅读:()

libs sh2进行SFTP文件传输

*The sample code has default values for host name,user name,password

*and path to copy,but you can specify them on the command line like:

*sftp 192.168.0.1 user password/tmp/secrets -p|-i|-k

#include libssh2_config.h

#include libssh2.h

#include libssh2_sftp.h

#ifdef HAVE_WINSOCK2_H

#include winsock2.h

#endif

#ifdef HAVE_S YS_SOCKET_H

#include sys/socket.h

#endif

#ifdef HAVE_NETINET_IN_H

#include netinet/in.h

#endif

#ifdef HAVE_UNISTD_H

#inc lude un istd.h

#endif

#ifdef HAVE_ARPA_INET_H

#include arpa/inet.h

#endif

#ifdef HAVE_S YS_TIME_H

#include sys/time.h

#endif

#include sys/types.h

#inc lude fcntl.h

#inc lude errno.h

#include stdio.h

#inc lude ctype.hstatic void kbd_callback(const char*name, int name_len,c onst char*instruction, int instruction_len, int num_prompts,const LIBS S H2_USERAUTH_KBDINT_PROMPT*prompts,LIBS S H2_USERAUT H_KBDINT_RESPONSE*responses,void**abstract)int i;size_t n;char buf[1024];

(void)abstract;printf(Performing keyboard-interactive authentic ation.\nprintf(Authentic ation name: ‘fwrite(name, 1,name_len, stdout);printf( ‘\nprintf(Authentic ation instruction: ‘

fwrite(instruc tion, 1, instruction_len, stdout);printf( ‘\nprintf(Number of prompts:%d\n\n,num_prompts);for(i=0; i num_prompts; i++) {printf(Prompt%d from server: ‘ , i);fwrite(prompts[i].text, 1,prompts[i].length, s tdout);printf( ‘\nprintf(Please type response:fgets(buf, sizeof(buf), stdin);n=strlen(buf);while(n 0 strchr( \r\n,buf[n- 1]))n--;buf[n]=0;responses[i].text=strdup(buf);respons es[i].length=n;printf(Response%d from user is ‘ , i);fwrite(respons es[i].text, 1,respons es[i].length, stdout);printf( ‘\n\nprintf(Done.Sending keyboard-interactive responses to server now.\n

*The application code is responsible for creating the socket

*and establishing the connections oc k=s oc ket(AF_INET,S OCK_S TREAM,0);

sin.sin_family=AF_INET;sin.sin_port=htons(22);s in.s in_addr.s_addr=hostaddr;if(c onnec t(s oc k, (struc t s ockaddr*)( s in),sizeof(struct sockaddr_in)) !=0) {fprintf(stderr,failed to c onnect!\nr eturn-1;

/*Create a session instancesession=libssh2_session_init();if(!session)r eturn-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 layersrc=libssh2_session_handshake(session, sock);if(rc) {fprintf(stderr,Failure establishing SSH session:%d\n, rc);r eturn-1;

/*At this point we havn’t yet authentic ated.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

*us er, that’s your c allfingerprint=libs sh2_hostkey_hash(s ession,LIBS S H2_HOSTKEY_HAS H_S HA1);fprintf(stderr,Fingerprint:for(i=0; i i++) {fprintf(stderr,%02X, (uns igned char)fingerprint[i]);fprintf(s tderr, \n

/*check what authentication methods are available*/us erauthlist=libs sh2_us erauth_list(s ess ion,us ername, strlen(us ername));printf(Authentic ation methods:%s\n,us erauthlis t);if(str str(us erauthlis t,p ass w ord) !=NULL) {auth_p w|=1;if(str str(us erauthlis t,keyb o ard-interac tive) !=NULL) {auth_p w|=2;if(str str(us erauthlis t,pub lic key) !=NUL L) {auth_p w|=4;

/* if we got an 4. argument we set this option if supported*/if(argc 5) {if((auth_pw 1) !strcasecmp(argv[5], -p)) {auth_p w=1;if((auth_pw 2) !strcasecmp(argv[5], -i )) {auth_p w=2;

if((auth_pw 4) !strc as ecmp(argv[5], -k)) {auth_p w=4;if(auth_pw 1) {

/*We c ould authentic ate via pas sword*/if(libssh2_userauth_password(session,username,password)) {fprintf(stderr,Authentic ation by pas sword failed.\ngoto shutdown;

} else if(auth_pw 2) {

/*Or via keyboard-interactive*/if(libs sh2_us erauth_keyboard_interactive(s es s ion,us ername,kbd_c allback) ) {printf( \tAuthentic ation by keyboard-interactive failed!\ngoto shutdown;

} els e {printf( \tAuthentic ation by keyboard-interactive succ eeded.\n

} else if(auth_pw 4) {

/*Or by public key*/if(libs sh2_userauth_publickey_fromfile(ses sion,username,keyfile 1,keyfile2,pas sword)) {printf( \tAuthentic ation by public key failed!\ngoto shutdown;

} els e {printf( \tAuthentication by public key succ eeded.\n

} els e {

printf(No supported authentic ation methods found!\ngoto shutdown;fprintf(stderr, libs sh2_s ftp_init()!\nsftp_session=libssh2_sftp_init(session);fprintf(stderr,Unable to open file with SFTP:%ld\n,libs sh2_s ftp_last_error(s ftp_s ess ion));goto shutdown;fprintf(stderr, libs sh2_sftp_open() is done,now rec eive data!\ndo {char mem[1024];

/* loop until we fail*/fprintf(stderr, libs sh2_s ftp_read()!\nrc=libs sh2_sftp_read(sftp_handle,mem,sizeof(mem));if(rc0) {write(1,m em, rc);

} els e {break;

}while(1);lib s s h2_s ftp_c lo s e(s ftp_handle);libs sh2_s ftp_shutdown(sftp_s es s ion);

RAKsmart便宜美国/日本/中国香港VPS主机 低至月$1.99 可安装Windows

RAKsmart 商家这几年还是在做事情的,虽然他们家顺带做的VPS主机并不是主营业务,毕竟当下的基础云服务器竞争过于激烈,他们家主营业务的独立服务器。包括在去年开始有新增多个数据中心独立服务器,包括有10G带宽的不限流量的独立服务器。当然,如果有需要便宜VPS主机的他们家也是有的,比如有最低月付1.99美元的美国VPS主机,而且可选安装Windows系统。这里商家有提供下面六款六月份的活动便宜V...

月神科技-美国CERA 5折半价倒计时,上新华中100G高防云59起!

官方网站:点击访问月神科技官网优惠码:美国优惠方案:CPU:E5-2696V2,机房:国人热衷的优质 CeraNetworks机房,优惠码:3wuZD43F 【过期时间:5.31,季付年付均可用】活动方案:1、美国机房:洛杉矶CN2-GIA,100%高性能核心:2核CPU内存:2GB硬盘:50GB流量:Unmilited端口:10Mbps架构:KVM折后价:15元/月、150元/年传送:购买链接洛...

妮妮云,美国cera CN2线路,VPS享3折优惠

近期联通CUVIP的线路(AS4837线路)非常火热,妮妮云也推出了这类线路的套餐以及优惠,目前到国内优质线路排行大致如下:电信CN2 GIA>联通AS9929>联通AS4837>电信CN2 GT>普通线路,AS4837线路比起前两的优势就是带宽比较大,相对便宜一些,所以大家才能看到这个线路的带宽都非常高。妮妮云互联目前云服务器开放抽奖活动,每天开通前10台享3折优惠,另外...

sftp为你推荐
燃气热水器和电热水器哪个好燃气热水器和电热水器哪个好少儿英语哪个好少儿英语哪种的好?二手车网站哪个好卖二手车网站哪个好无纺布和熔喷布口罩哪个好表层水刺布,中间层pp无纺布+熔喷布,里层pp无纺布口罩好吗?oppo和vivo哪个好Vivo和OPPO哪个好点啊?看书软件哪个好什么读书app好用云盘哪个好免费的网盘哪个好?更大、更安全、更实用?qq空间登录器QQ空间校友网页自动登陆器辽宁联通网上营业厅网中国联通的初始服务密码一般是多少东莞电信宽带套餐东莞光纤宽带资费
深圳域名注册 in域名注册 游戏服务器租用 我的世界服务器租用 到期域名查询 域名查询系统 免费国际域名 什么是域名解析 国外bt 账号泄露 NetSpeeder 工信部icp备案号 me空间社区 91vps 亚马逊香港官网 新世界服务器 湖南idc 香港博客 fatcow 月付空间 更多