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

Sharktech:鲨鱼机房1Gbps无限流量美国服务器;丹佛$49/月起,洛杉矶$59/月起

sharktech怎么样?sharktech鲨鱼机房(Sharktech)我们也叫它SK机房,是一家成立于2003年的老牌国外主机商,提供的产品包括独立服务器租用、VPS主机等,自营机房在美国洛杉矶、丹佛、芝加哥和荷兰阿姆斯特丹等,主打高防产品,独立服务器免费提供60Gbps/48Mpps攻击防御。机房提供1-10Gbps带宽不限流量服务器,最低丹佛/荷兰机房每月49美元起,洛杉矶机房最低59美元...

弘速云香港VPSVPS线路有CN2+BGP、CN2 GIA,KVM虚拟化架构,裸金属月付564元

弘速云怎么样?弘速云是创建于2021年的品牌,运营该品牌的公司HOSU LIMITED(中文名称弘速科技有限公司)公司成立于2021年国内公司注册于2019年。HOSU LIMITED主要从事出售香港vps、美国VPS、香港独立服务器、香港站群服务器等,目前在售VPS线路有CN2+BGP、CN2 GIA,该公司旗下产品均采用KVM虚拟化架构。可联系商家代安装iso系统。点击进入:弘速云官方网站地址...

恒创科技SonderCloud,美国VPS综合性能测评报告,美国洛杉矶机房,CN2+BGP优质线路,2核4G内存10Mbps带宽,适用于稳定建站业务需求

最近主机参考拿到了一台恒创科技的美国VPS云服务器测试机器,那具体恒创科技美国云服务器性能到底怎么样呢?主机参考进行了一番VPS测评,大家可以参考一下,总体来说还是非常不错的,是值得购买的。非常适用于稳定建站业务需求。恒创科技服务器怎么样?恒创科技服务器好不好?henghost怎么样?henghost值不值得购买?SonderCloud服务器好不好?恒创科技henghost值不值得购买?恒创科技是...

sftp为你推荐
软银收购wework软银联合 GungHo 注资控股芬兰游戏公司 Supercell意味着甚么?腾讯空间首页qq空间主页怎么每个都看不见租车平台哪个好共享汽车现在哪个平台的比较好?杀毒软件哪个好杀毒软件哪个最好莫代尔和纯棉哪个好莫代尔好还是棉好传奇类手游哪个好传奇哪个版本比较好玩 最好玩的传奇手游排行榜手动挡和自动挡哪个好手动挡和自动挡哪个好核芯显卡与独立显卡哪个好核芯显卡和独立显卡有什么区别?最好的是哪个?网络机顶盒哪个好什么牌子的网络机顶盒最好51空间登录怎样登51个人空间
虚拟主机服务商 yaokan永久域名经常更换 韩国服务器租用 warez idc评测 google镜像 主机点评 cdn服务器 宕机监控 2017年黑色星期五 win8.1企业版升级win10 大容量存储 免费smtp服务器 京东商城双十一活动 个人域名 鲁诺 最漂亮的qq空间 海外空间 学生服务器 asp空间 更多