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

Hostigger不限流量VPS年20美元

Hostigger 主机商在前面的文章中也有介绍过几次,这个商家运营时间是有一些年份,只不过在我们圈内好像之前出现的次数不多。最近这段时间商家有提供不限流量的VPS主机,逐渐的慢慢被人认识到。在前面的介绍到他们提供的机房还是比较多的,比如土耳其、美国等。今天看到Hostigger 商家居然改动挺大的,原来蛮好的域名居然这次连带官方域名都更换掉去掉一个G(Hostiger )。估摸着这个域名也是之前...

DMIT$10.9/月,日本VPS/三网直连/1核1.5G内存/20GB存储/1Gbps端口

优惠码年付一次性5折优惠码:TYO-Lite-Open-Beta-1y-50OFF永久8折优惠码:TYO-Lite-Open-Beta-Recur-20OFF日本vpsCPU内存SSD流量带宽价格购买1核1.5G20 GB4 TB1Gbps$10.9/月购买2核2 G40 GB6 TB1Gbps$16.9/月购买2核4 G60 GB8 TB1Gbps$21.9/月购买4核4 G80 GB12 TB...

Friendhosting(月1.35欧元),不限流量,9机房可选

今天9月10日是教师节,我们今天有没有让孩子带礼物和花送给老师?我们这边不允许带礼物进学校,直接有校长在门口遇到有带礼物的直接拦截下来。今天有看到Friendhosting最近推出了教师节优惠,VPS全场45折,全球多机房可选,有需要的可以看看。Friendhosting是一家成立于2009年的保加利亚主机商,主要提供销售VPS和独立服务器出租业务,数据中心分布在:荷兰、保加利亚、立陶宛、捷克、乌...

sftp为你推荐
天气预报哪个好用哪个最准确一般查看天气预报哪个软件好用?天气预报哪个好用哪个最准确最准天气预报软件排行是怎样的?小说软件哪个好用免费看小说,哪个软件好,要免费的小说软件哪个好用免费有什么好用的免费小说软件华为p40和mate30哪个好mate30 5G和p40 5G有什么区别?石英表和机械表哪个好石英表好还是机械表好?二手车网站哪个好买二手车去哪里买比较划算?网页传奇哪个好玩哪个传奇类网页游戏即好玩又不花钱??网页传奇哪个好玩网页传奇哪个好玩 现在的传奇跟以前玩的都不一样了 总感觉没以前的有激情了杰士邦和杜蕾斯哪个好杜蕾斯好用还是杰士邦好要?
免费com域名申请 flashfxp怎么用 adman 外国域名 网站监控 info域名 xfce 云全民 100x100头像 linux空间 softbank邮箱 双十一秒杀 秒杀汇 qq云端 美国网站服务器 in域名 美国独立日 google台湾 四川电信商城 网页提速 更多