问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

libssh2的参考用例

发布网友 发布时间:2022-05-09 23:27

我来回答

1个回答

热心网友 时间:2023-10-08 19:51

/**SampleshowinghowtodoSSH2connect.**Thesamplecodehasdefaultvaluesforhostname,username,password*andpathtocopy,butyoucanspecifythemonthecommandlinelike:**ssh2hostuserpassword[-p|-i|-k]*/#includelibssh2_config.h#include<libssh2.h>#include<libssh2_sftp.h>#ifdefHAVE_WINDOWS_H#include<windows.h>#endif#ifdefHAVE_WINSOCK2_H#include<winsock2.h>#endif#ifdefHAVE_SYS_SOCKET_H#include<sys/socket.h>#endif#ifdefHAVE_NETINET_IN_H#include<netinet/in.h>#endif#ifdefHAVE_UNISTD_H#include<unistd.h>#endif#ifdefHAVE_ARPA_INET_H#include<arpa/inet.h>#endif#include<sys/types.h>#include<fcntl.h>#include<errno.h>#include<stdio.h>#include<ctype.h>constchar*keyfile1=~/.ssh/id_rsa.pub;constchar*keyfile2=~/.ssh/id_rsa;constchar*username=username;constchar*password=password;staticvoidkbd_callback(constchar*name,intname_len,constchar*instruction,intinstruction_len,intnum_prompts,constLIBSSH2_USERAUTH_KBDINT_PROMPT*prompts,LIBSSH2_USERAUTH_KBDINT_RESPONSE*responses,void**abstract){(void)name;(void)name_len;(void)instruction;(void)instruction_len;if(num_prompts==1){responses[0].text=strp(password);responses[0].length=strlen(password);}(void)prompts;(void)abstract;}/*kbd_callback*/intmain(intargc,char*argv[]){unsignedlonghostaddr;intrc,sock,i,auth_pw=0;structsockaddr_insin;constchar*fingerprint;char*userauthlist;LIBSSH2_SESSION*session;LIBSSH2_CHANNEL*channel;#ifdefWIN32WSADATAwsadata;WSAStartup(MAKEWORD(2,0),&wsadata);#endifif(argc>1){hostaddr=inet_addr(argv[1]);}else{hostaddr=htonl(0x7F000001);}if(argc>2){username=argv[2];}if(argc>3){password=argv[3];}rc=libssh2_init(0);if(rc!=0){fprintf(stderr,libssh2initializationfailed(%d)\n,rc);return1;}/*Ultrabasicconnecttoport22onlocalhost.Yourcodeis*responsibleforcreatingthesocketestablishingtheconnection*/sock=socket(AF_INET,SOCK_STREAM,0);sin.sin_family=AF_INET;sin.sin_port=htons(22);sin.sin_addr.s_addr=hostaddr;if(connect(sock,(structsockaddr*)(&sin),sizeof(structsockaddr_in))!=0){fprintf(stderr,failedtoconnect!\n);return-1;}/*Createasessioninstanceandstartitup.Thiswilltradewelcome*banners,exchangekeys,andsetupcrypto,compression,andMAClayers*/session=libssh2_session_init();if(libssh2_session_handshake(session,sock)){fprintf(stderr,FailureestablishingSSHsession\n);return-1;}/*Atthispointwehavn'tauthenticated.Thefirstthingtodoischeck*thehostkey'sfingerprintagainstourknownhostsYourappmayhaveit*hardcoded,maygotoafile,maypresentittotheuser,that'syour*call*/fingerprint=libssh2_hostkey_hash(session,LIBSSH2_HOSTKEY_HASH_SHA1);fprintf(stderr,Fingerprint:);for(i=0;i<20;i++){fprintf(stderr,%02X,(unsignedchar)fingerprint[i]);}fprintf(stderr,\n);/*checkwhatauthenticationmethodsareavailable*/userauthlist=libssh2_userauth_list(session,username,strlen(username));fprintf(stderr,Authenticationmethods:%s\n,userauthlist);if(strstr(userauthlist,password)!=NULL){auth_pw|=1;}if(strstr(userauthlist,keyboard-interactive)!=NULL){auth_pw|=2;}if(strstr(userauthlist,publickey)!=NULL){auth_pw|=4;}/*ifwegotan4.argumentwesetthisoptionifsupported*/if(argc>4){if((auth_pw&1)&&!strcasecmp(argv[4],-p)){auth_pw=1;}if((auth_pw&2)&&!strcasecmp(argv[4],-i)){auth_pw=2;}if((auth_pw&4)&&!strcasecmp(argv[4],-k)){auth_pw=4;}}if(auth_pw&1){/*Wecouldauthenticateviapassword*/if(libssh2_userauth_password(session,username,password)){fprintf(stderr,\tAuthenticationbypasswordfailed!\n);gotoshutdown;}else{fprintf(stderr,\tAuthenticationbypasswordsucceeded.\n);}}elseif(auth_pw&2){/*Orviakeyboard-interactive*/if(libssh2_userauth_keyboard_interactive(session,username,&kbd_callback)){fprintf(stderr,\tAuthenticationbykeyboard-interactivefailed!\n);gotoshutdown;}else{fprintf(stderr,\tAuthenticationbykeyboard-interactivesucceeded.\n);}}elseif(auth_pw&4){/*Orbypublickey*/if(libssh2_userauth_publickey_fromfile(session,username,keyfile1,keyfile2,password)){fprintf(stderr,\tAuthenticationbypublickeyfailed!\n);gotoshutdown;}else{fprintf(stderr,\tAuthenticationbypublickeysucceeded.\n);}}else{fprintf(stderr,Nosupportedauthenticationmethodsfound!\n);gotoshutdown;}/*Requestashell*/if(!(channel=libssh2_channel_open_session(session))){fprintf(stderr,Unabletoopenasession\n);gotoshutdown;}/*Someenvironmentvariablesmaybeset,*It'suptotheserverwhichonesit'llallowthough*/libssh2_channel_setenv(channel,FOO,bar);/*Requestaterminalwith'vanilla'terminalemulation*See/etc/termcapformoreoptions*/if(libssh2_channel_request_pty(channel,vanilla)){fprintf(stderr,Failedrequestingpty\n);gotoskip_shell;}/*OpenaSHELLonthatpty*/if(libssh2_channel_shell(channel)){fprintf(stderr,Unabletorequestshellonallocatedpty\n);gotoshutdown;}/*Atthispointtheshellcanbeinteractedwithusing*libssh2_channel_read()*libssh2_channel_read_stderr()*libssh2_channel_write()*libssh2_channel_write_stderr()**Blockingmodemaybe(en|dis)abledwith:libssh2_channel_set_blocking()*IftheserversendEOF,libssh2_channel_eof()willreturnnon-0*TosendEOFtotheserveruse:libssh2_channel_send_eof()*Achannelcanbeclosedwith:libssh2_channel_close()*Achannelcanbefreedwith:libssh2_channel_free()*/skip_shell:if(channel){libssh2_channel_free(channel);channel=NULL;}/*Otherchanneltypesaresupportedvia:*libssh2_scp_send()*libssh2_scp_recv()*libssh2_channel_direct_tcpip()*/shutdown:libssh2_session_disconnect(session,NormalShutdown,Thankyouforplaying);libssh2_session_free(session);#ifdefWIN32closesocket(sock);#elseclose(sock);#endiffprintf(stderr,alldone!\n);libssh2_exit();return0;}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
牡丹叶子枯了怎么办 牡丹枯叶怎么回事 "他同情冤魂、惩办恶人,既有同情心又主持公道,既可爱又恐怖” 冯宝成传销诈骗邪教组织 冯保成团队,传销诈骗,家人深陷其中,求助。 南朝词臣北朝客,归来唯见秦淮碧。 池台竹树三亩馀至今人道江家宅的意思 池台竹树三亩馀至今人道江家宅是什... 高效、加分学习工具——小小答题错题本上线了 如何将三张合在一起的图片合并 砂锅红烧肉的做法 最正宗的做法_砂锅红烧肉怎么做 砂锅版红烧肉做法: 天方夜谭为何又称为一千另一夜? 新婚第一夜 第二夜 第三夜 第四夜 第五夜 第六夜 第七夜 第八夜第九夜第十夜各打一个中国的城市地名 word中画了竖线为什么第二页中也有+怎么删除 山田凉介的アジアの夜(亚洲之夜) 沈阳亚洲第一大夜市有去过得帮忙介绍一下特色小吃 亚洲第一夜晚【送钟声】【随风飘颂】长裙哥东北森林中唤醒爷爷心海中只看到了棉絮草根要疯的相思梦 斜翘的意思是什么请回答 拌翘是什么意思呢? 悄悄翘的意思? 翘楚可以读楚翘吗?意思是否也一样呢? 翘是什么意思? 手机上面怎么修改图片里面的数字?什么软件可以? 照片可以p数字键里面可以打文字的是什么软件 请问什么手机P图软件可以修改一个图中的文字数字?必要的话麻烦你帮我P一下~ 唯美的作文题目 有哪些唯美的作文题目 好听唯美的作文题目有哪些 什么是倾销与反倾销? 中国产品频繁遭受反倾销的原因以及应对措施? 如何对国际贸易中的反规避措施有效的控制! 《一千零一夜》里面扮演坏女孩周岑炎的女星是谁?她还有哪些影视作品? 亚洲最大毒枭,一年赚取1200亿,一夜输光3.6亿毫不在乎,他是谁呢? 我在word里面画了一条线不能删除了怎么办 亚洲最大毒枭,一年收入1200亿,一夜输掉3.6亿面不改色,他是谁? WORD里自己画的波浪线怎么删除。怎么都删不掉 Minecraft 1.7.2 服务器 安装工业2实验班和格雷5 就无法开服了怎么破 怎么看待做手游cocos前端开发,lua用的多,c++用的少面试会被鄙视 开机不登陆的情况下启动软件 我的世界服务器一直显示两个信息,登录一直显示登录中 win10系统怎么录制屏幕?录制屏幕时能连同声音一起录进去吗? 吃香蕉可以么 安卓手机上有没有可以将已经安装好的软件反向打包成APK文件的软件?注:手机未ROOT etc借记卡可以欠账吗 中国医药50强 张家口金赛制药有限公司怎么样? 河北省前十大的制药大公司,写全者奖励200分!谢谢! 怎样做狗肉才好吃 请问?狗肉怎么做才好吃?爱狗人士,请你不要回答我。 王者荣耀高渐离专属ID? 豆瓣酱炒肉的做法