发布网友 发布时间: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;}