文件传输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);

Fiberia.io:$2.9/月KVM-4GB/50GB/2TB/荷兰机房

Fiberia.io是个新站,跟ViridWeb.com同一家公司的,主要提供基于KVM架构的VPS主机,数据中心在荷兰Dronten。商家的主机价格不算贵,比如4GB内存套餐每月2.9美元起,采用SSD硬盘,1Gbps网络端口,提供IPv4+IPv6,支持PayPal付款,有7天退款承诺,感兴趣的可以试一试,年付有优惠但建议月付为宜。下面列出几款主机配置信息。CPU:1core内存:4GB硬盘:...

HostYun(22元/月)全场88折优惠香港原生IP大带宽

在之前的一些文章中有提到HostYun商家的信息,这个商家源头是比较老的,这两年有更换新的品牌域名。在陆续的有新增机房,价格上还是走的低价格路线,所以平时的折扣力度已经是比较低的。在前面我也有介绍到提供九折优惠,这个品牌商家就是走的低价量大为主。中秋节即将到,商家也有推出稍微更低的88折。全场88折优惠码:moon88这里,整理部分HostYun商家的套餐。所有的价格目前都是原价,我们需要用折扣码...

ZJI:520元/月香港服务器-2*E5-2630L/32GB/480G SSD/30M带宽/2IP

ZJI发布了一款7月份特别促销独立服务器:香港邦联四型,提供65折优惠码,限量30台(每用户限购1台),优惠后每月520元起。ZJI是原来Wordpress圈知名主机商家:维翔主机,成立于2011年,2018年9月启用新域名ZJI,提供中国香港、台湾、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册等业务。下面列出这款服务器的配置信息。香港邦联四型CPU:2*E5-2...

sftp为你推荐
免费个人网站制作怎样免费制作个人网站?电脑管家和360哪个好360和电脑管家哪个好炒股软件哪个好股票软件哪个最好用?无纺布和熔喷布口罩哪个好无纺布除了做尿布湿口罩这些,还有其他什么用处吗?手机杀毒哪个好手机杀毒软件哪个最好用空间登录器qq空间登录不了怎么办qq空间登录电脑手机怎么登qq空间电脑版?辽宁联通营业厅请问辽宁鞍山最大的联通营业厅在哪dns服务器未响应DNS服务器未响应是什么上海dns服务器地址上海市青浦区的电信DNS地址是多少
域名备案信息查询 罗马假日广场 香港加速器 bandwagonhost info域名 web服务器架设软件 主机合租 linux空间 howfile 秒杀预告 亚马逊香港官网 免费美国空间 免费申请个人网站 metalink 免费私人服务器 raid10 中国电信网络测速 数据库空间 贵阳电信 万网注册 更多