#include "libssh2_config.h"
#include<libssh2.h>
#include<libssh2_sftp.h>

上述为所包含必备头文件。

以下为定义的静态子串常量

const char *keyfile1 = "~/.ssh/id_rsa.pub";
const char *keyfile2 = "~/.ssh/id_rsa";
const char *username = "username";
const char *password = "password";
unsigned long hostaddr;
int rc, sock, i, auth_pw = ;
struct sockaddr_in_sin;
const char *fingerprint;
char * userauthlist;
LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel;

连接到SSH2步骤:

(1)建立socket并连接到远程主机SSH2服务(22端口);

(2)创建一个LIBSSH2_SESSION 实例并启动它。启动动作包括设置欢迎横幅、交换密钥并且设置加密、压缩和MAC层。

session = libssh2_session_init();   //创建一个会话实例
if(libssh2_session_handshake(session, sock))
{
fprintf(stderr, "Failure establishing SSH session");
return -;
}

(3)认证:检查主机密钥指纹并检查可用的认证方式。

fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
userauthlist = libssh2_userauth_list(session, username, strlen(username));
if(strstr(userauthlist, "password") != NULL)
{
auth_pw |= ;
}
if(strstr(userauthlist, "keyboad-interactive") != NULL)
{
auth_pw |= ;
}
if(strstr(userauthlist, "publickey") != NULL)
{
auth_pw |= ;
}

(4)如果在参数列表中设置了认证方式,则将认证方式设为命令中的方式(前提是该方式是通过上个步骤检测可用的)。

if(argc > )
{
if((auth_pw & ) && !strcasecmp(argv[], "-p"))
{
auth_pw = ;
}
if((auth_pw & ) && !strcasecmp(argv[], "-i"))
{
auth_pw = ;
}
if((auth_pw && ) && !strcasecmp(argv[], "-k"))
{
auth_pw = ;
}
}

(5)根据上一步选定的认证方式开始认证。

if (auth_pw & ) {
/* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { fprintf(stderr, "\tAuthentication by password failed!\n");
goto shutdown;
} else {
fprintf(stderr, "\tAuthentication by password succeeded.\n");
}
} else if (auth_pw & ) {
/* Or via keyboard-interactive */
if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) {
fprintf(stderr,
"\tAuthentication by keyboard-interactive failed!\n");
goto shutdown;
} else {
fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
}
} else if (auth_pw & ) {
/* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown;
} else {
fprintf(stderr, "\tAuthentication by public key succeeded.\n");
}
} else {
fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown;
}

(6)请求一个shell

if(!(channel = libssh2_channel_open_session(session)))

(7)设置一些环境变量,并上传给服务器

libssh2_channel_setenv(channel, "F00", "bar");

(8)请求一个vanilla的终端模拟。

libssh2_channel_request_pty(channel, "vanilla")

(9)在上一步请求的pty上开启SHELL。

libssh2_channel_shell(channel)

(10)至此,可以交互使用shell了

libssh2_channel_read();
libssh2_channel_read_stderr();
libssh2_channel_write(); libssh2_channel_write_stderr(); /* 打开或关闭阻塞模式 */ libssh2_channel_set_blocking(); /* 如果服务器发送EOF */ libssh2_channel_eof()返回非0; /* 关闭channel */ libssh2_channel_close(); /* 释放一个channel */ libssh2_channel_free();

(11)ssh交互完成后,关闭会话并释放会话

libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session);

(12)关闭sock并退出libssh2

close(sock);
libssh2_exit();

libssh2--ssh2实例的更多相关文章

  1. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

  2. 使用libssh2库实现支持密码参数的ssh2客户端

    使用libssh2库实现支持密码参数的ssh2客户端 http://blog.chinaunix.net/uid-24382173-id-229823.html libssh2的简单应用 http:/ ...

  3. SSH2框架实现注冊发短信验证码实例

    这两天開始写程序了,让用SSH2框架,曾经没有接触过Java项目更没有接触过SSH2框架,所以用注冊開始了我Java之旅.后来发现,后台代码挺easy理解的,跟.net的差点儿相同.就是层与层之间的调 ...

  4. SSH2 增删查改实例

    (一)引入包 (共73个,不一定都需要,但是我的项目是这么多,经过调试,没有包冲突) (二)创建数据库表 建立数据库octtest,并创建user表,表里面一共4个字段:id,姓,名,年龄. 语句如下 ...

  5. Windows VS2017 编译 libssh2 1.7.0(执行命令、文件上传、下载)

    下载安装 OpenSSL 要编译 libssh2,必须先编译好 OpenSSL 的静态库,直接从 http://slproweb.com/products/Win32OpenSSL.html 下载已经 ...

  6. 使用mapreduce计算环比的实例

    最近做了一个小的mapreduce程序,主要目的是计算环比值最高的前5名,本来打算使用spark计算,可是本人目前spark还只是简单看了下,因此就先改用mapreduce计算了,今天和大家分享下这个 ...

  7. libssh2 的集成与应用

    http://blog.csdn.net/wyc6668205/article/details/9179197 Xmanager Enterprise 4 putty 等工具都功能都是利用libssh ...

  8. SSH框架总结(框架分析+环境搭建+实例源码下载) 《转》

    这篇文章比较易懂,易理解: 首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层W ...

  9. SSH框架总结(框架分析+环境搭建+实例源码下载)

    来源于: http://blog.csdn.net/shan9liang/article/details/8803989 首先,SSH不是一个框架,而是多个框架(struts+spring+hiber ...

随机推荐

  1. react中,用key值来解决一些奇葩问题

    编辑用户信息,角色信息无法加载到值 改进之后:思路:由于值是设置在state里面的,界面编辑时,会重服务器拉去数据,值也设置在state里面了,但是CheckboxGroup依然不会去渲染选中的值, ...

  2. iOS面试经历(个人)

    1.OC的runtime运行机制1> runtime,运行时机制,它是一套C语言库2> 实际上我们编写的所有OC代码,最终都是转成了runtime库的东西,比如类转成了runtime库里面 ...

  3. CEIWEI CommMonitor 串口监控精灵11.0 SDK/OCX 串口过滤驱动

    CommMonitorX 监视精灵SDK,能够嵌入到你的App程序中,从而在你的App中实现串行端口分析.调试串行设备的协议信息,并可以拦截.记录串行端口程序操作串口的TX.Rx数据包.串口置信息如波 ...

  4. 【ARM-Linux开发】Wi-Fi 应用工具wpa_supplicant

    wpa_supplicant是一个跨平台的无线安全管理软件,这里需要用它来对无线网络进行配置,wpa_supplicant相关工具已经移植好,包含在我们提供的文件系统中. 配置无线网络 wpa_sup ...

  5. C#实现动态发布IIS站点帮助类

    准备工作: 1.引用 System.DirectoryServices 系统程序集 2.引用 Microsoft.Web.Administration 程序集,类库位置在 C:\Windows\Sys ...

  6. web系统整体优化

    关于web系统整体优化提速总结   关于web系统整体优化提速总结 一.背景 随着公司业务的拓展,随之而来就是各种系统横向和纵向的增加,PV.UV也都随之增加,原有的系统架构和模式慢慢遇上了瓶颈,需要 ...

  7. linux系统中RAID10磁盘冗余阵列配置

    介绍:RAID10:需要至少四块(含)硬盘,兼具速度和安全性,但成本很高,RAID10用两个磁盘做RAID0,用其他两个做RAID1当备份. 配置流程: 第一步:在原有基础上为磁盘再填入五块磁盘(至少 ...

  8. WCF-初识DEMO

    类库 System.ServiceModle WCF类库 契约IUser1,实现User1 [ServiceContract] public interface IUser1 { [Operation ...

  9. php实现微信小程序登录

    以上是官方的流程介绍,已经说的很详细了,现在简单介绍一下流程 前端通过wx.login生成code传递给后端,后端通过提交Appid + appSecret + code 到微信方服务器 获取 ses ...

  10. golang使用注意事项

    1.可以给类型取别名,但是该类型和别名是两个不同的类型: type myInt int 2.go支持可变参数:args... 0个或多个参数:func sum(args... int) sum int ...