/*
VS2012 远程编译程序
VS2012 SSH远程编译Linux程序,自动识别是否是UTF8编码
mengxp works
2013/08/17
*/
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include "libssh2.h"
#include "libssh2_config.h"
#include "libssh2_sftp.h"
#include "sftp.h"
#include "gccflt.h"
#include "express.h"
#pragma comment(lib,"libssh2.lib")
#pragma comment(lib,"ws2_32.lib")
CHAR szRemoteIp[16];
ULONG dwRemotePort;
CHAR szLocalWorkDir[128];
CHAR szRemoteWorkDir[128];
CHAR szUsername[64];
CHAR szPassword[64];
CHAR szDownloadFilename[128];
CHAR szDownloadSaveAs[128];
SOCKET hSocket;
LIBSSH2_SESSION *pSession;
LIBSSH2_CHANNEL *pChannel;
LIBSSH2_SFTP *pSftp;
LIBSSH2_SFTP_HANDLE *hSftp;
BOOL IsProbablyUTF8(PCHAR pszSrc)
{
BOOL bRet = FALSE;
BOOL bFirstByte = TRUE;
ULONG dwCharBytes = 1;
PUCHAR p = (PUCHAR)pszSrc;
while(*p)
{
if(bFirstByte)
{
if((p[0] >> 7) == 0) // 最高位为0, ANSI 兼容的.
dwCharBytes = 1;
else if((p[0] >> 5) == 0x06) // 右移5位后是 110 -> 2字节编码的UTF8字符的首字节
dwCharBytes = 2;
else if((p[0] >> 4) == 0x0E) // 右移4位后是 1110 -> 3字节编码的UTF8字符的首字节
dwCharBytes = 3;
else if((p[0] >> 3) == 0x1E) // 右移3位后是 11110 -> 4字节编码的UTF8字符的首字节
dwCharBytes = 4;
else if((p[0] >> 2) == 0x3E) // 右移2位后是 111110 -> 5字节编码的UTF8字符的首字节
dwCharBytes = 5;
else if((p[0] >> 1) == 0x7E) // 右移1位后是 1111110 -> 6字节编码的UTF8字符的首字节
dwCharBytes = 6;
else
dwCharBytes = -1; // 非法UTF8首字节
if(dwCharBytes != 1)
{
bRet = TRUE; // 有可能是UTF8
bFirstByte = FALSE;
}
if(dwCharBytes == -1) // 非法UTF8首字节
{
bRet = FALSE;
break;
}
}
else
{
if((p[0] >> 6) == 0x02) // 后续字节必须以 10xxx 开头
{
dwCharBytes--;
if(dwCharBytes == 1)
bFirstByte = TRUE; // 下一个字节是下一个字符的开始
}
else
{
bRet = FALSE; // 后续字节不符要求
break;
}
}
p++;
}
if(dwCharBytes != 1)
bRet = FALSE;
return bRet;
}
BOOL ReadBuildSetting(PCHAR pszConfigFile)
{
CHAR szConfigFile[64];
GetCurrentDirectory(sizeof(szLocalWorkDir),szLocalWorkDir);
sprintf(szConfigFile,".\\%s",pszConfigFile);
if(!GetPrivateProfileString("Setting","RemoteIp","",szRemoteIp,sizeof(szRemoteIp),szConfigFile))
return FALSE;
if((dwRemotePort = GetPrivateProfileInt("Setting","RemotePort",0,szConfigFile)) == 0)
return FALSE;
if(!GetPrivateProfileString("Setting","RemoteWorkDir","",szRemoteWorkDir,sizeof(szRemoteWorkDir),szConfigFile))
return FALSE;
if(!GetPrivateProfileString("Setting","Username","",szUsername,sizeof(szUsername),szConfigFile))
return FALSE;
if(!GetPrivateProfileString("Setting","Password","",szPassword,sizeof(szPassword),szConfigFile))
return FALSE;
// if(!GetPrivateProfileString("Setting","LocalWorkDir","",szLocalWorkDir,sizeof(szLocalWorkDir),szConfigFile))
// return TRUE;
GetPrivateProfileString("Setting","DownloadFile","",szDownloadFilename,sizeof(szDownloadFilename),szConfigFile);
GetPrivateProfileString("Setting","DownloadSaveAs","",szDownloadSaveAs,sizeof(szDownloadSaveAs),szConfigFile);
return TRUE;
}
ULONG ToAnsi(PCHAR pszDst,ULONG dwDstLen,PCHAR pszSrc)
{
WCHAR szUnicodeBuf[1024];
if(strlen(pszSrc) > 512)
return 0;
MultiByteToWideChar(CP_UTF8,0,pszSrc,-1,szUnicodeBuf,sizeof(szUnicodeBuf) / 2);
return WideCharToMultiByte(CP_ACP,0,szUnicodeBuf,-1,pszDst,dwDstLen,NULL,FALSE);
}
BOOL ConnectToRemote()
{
BOOL bRet = FALSE;
int nRet;
do {
WSADATA wsadata;
SOCKADDR_IN sin;
WSAStartup(MAKEWORD(2,0), &wsadata);
nRet = libssh2_init(0);
if(nRet)
{
printf("libssh2_init failed. %d\n",nRet);
break;
}
//连接远程服务器
printf("Connecting to remote host...\n");
hSocket = socket(AF_INET,SOCK_STREAM,0);
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons((USHORT)dwRemotePort);
sin.sin_addr.s_addr = inet_addr(szRemoteIp);
nRet = connect(hSocket,(struct sockaddr*)&sin,sizeof(sin));
if(nRet)
{
printf(" Connect %s:%d failed. %d\n",szRemoteIp,dwRemotePort,nRet);
break;
}
//初始化SSH
printf("Initializing SSH session...\n");
pSession = libssh2_session_init();
if(!pSession)
{
printf(" failed\n");
break;
}
libssh2_session_set_blocking(pSession,TRUE);
//握手
nRet = libssh2_session_handshake(pSession,hSocket);
if(nRet)
{
printf(" handshake failed. %d\n",nRet);
break;
}
//登陆
printf("Login...\n");
nRet = libssh2_userauth_password(pSession,szUsername,szPassword);
if(nRet)
{
printf(" failed. %d\n",nRet);
break;
}
//初始化SFTP
printf("Initializing SFTP...\n");
pSftp = libssh2_sftp_init(pSession);
if(!pSftp)
{
printf(" failed\n");
break;
}
//初始化通道
printf("Open channel...\n");
pChannel = libssh2_channel_open_session(pSession);
if(!pChannel)
{
printf(" failed\n");
break;
}
//打开SHELL
nRet = libssh2_channel_shell(pChannel);
if(nRet)
{
printf(" Shell failed. %d\n",nRet);
break;
}
bRet = TRUE;
} while(FALSE);
return bRet;
}
VOID ReadStdOutThread()
{
int nRet;
CHAR szLineBuf[512];
PCHAR pszLineBuf = szLineBuf;
ULONG dwLineBufLen = 0;
CHAR szBuf[512];
//接收STDOUT循环
while(1)
{
nRet = libssh2_channel_read(pChannel,pszLineBuf,1);
if(nRet > 0)
{
if(*pszLineBuf == '\n')
{
PCHAR pGccFltBuf;
//收到了一行
szLineBuf[dwLineBufLen + 1] = 0;
OutputDebugString(szLineBuf);
if(IsProbablyUTF8(szLineBuf))
{
ToAnsi(szBuf,512,szLineBuf);
}
else
{
strcpy(szBuf,szLineBuf);
}
//Gcc格式变换
pGccFltBuf = GccFilter(szBuf,szLocalWorkDir,szRemoteWorkDir);
if(pGccFltBuf)
{
printf(pGccFltBuf);
FormatFree(pGccFltBuf);
}
else
{
printf(szBuf);
}
//收到结束行,跳出接收循环
if(!strcmp(szLineBuf,"**FINISHED**\n"))
{
break;
}
pszLineBuf = szLineBuf;
dwLineBufLen = 0;
}
else if(dwLineBufLen == sizeof(szLineBuf) - 1)
{
PCHAR pGccFltBuf;
//缓冲满了但是没收到行尾
szLineBuf[dwLineBufLen + 1] = 0;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本人的项目需求代码跨平台包含Linux solaris hpux aix 等诸多平台,使用该工具可以帮助visual studio做跨平台开发 使用vs创建生成文件项目 编译命令行 remotebuild build_hpux.ini build Makefile.hpux ini文件样例 [Setting] RemoteIp = 192.168.0.119 RemotePort = 22 Username = root Password = rootpassword RemoteWorkDir = /usr/testtool DownloadFile = testtool.tar DownloadSaveAs = testtool.hpux.tar
资源推荐
资源详情
资源评论
收起资源包目录
RemoteBuild_src.rar (22个子文件)
libeay32.dll 986KB
libssh2.lib 30KB
sftp.h 364B
main.c 14KB
sftp.c 7KB
libssh2.dll 249KB
gccflt.h 160B
RemoteBuild.vcxproj.user 851B
express.lib 4KB
ssleay32.dll 184KB
express.h 5KB
libssh2_publickey.h 5KB
RemoteBuild.vcxproj 5KB
build_linux.ini 122B
RemoteBuild.vcxproj.filters 1KB
RemoteBuild.sln 888B
RemoteBuild.v11.suo 36KB
gccflt.c 7KB
libssh2_sftp.h 15KB
zlib1.dll 59KB
libssh2.h 46KB
libssh2_config.h 871B
共 22 条
- 1
资源评论
MengXP
- 粉丝: 156
- 资源: 63
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- base(1).apk.1
- K618977005_2012-12-6_beforeP_000.txt.PRM
- 秋招信息获取与处理基础教程
- 程序员面试笔试面经技巧基础教程
- Python实例-21个自动办公源码-数据处理技术+Excel+自动化脚本+资源管理
- 全球前8GDP数据图(python动态柱状图)
- 汽车检测7-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- 检测高压线电线-YOLO(v5至v9)、COCO、Darknet、VOC数据集合集.rar
- 检测行路中的人脸-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、VOC数据集合集.rar
- Image_17083039753012.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功