vi poi_bmwccc_post_login_4_new2.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/select.h>
#define LEN 800000L
#define SHORT_LEN 100L
//#define LEN 1024
#define HIS_PORT 80
#define HIS_IPADDR "160.48.x.x"
//cookie
char Cookie_JSESSIONID[SHORT_LEN];
//get user and data number
char loggedInUsersCount[SHORT_LEN];
char requestCountAll[SHORT_LEN];
char requestCountUnassigned[SHORT_LEN];
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"};
char status_information[LEN];
char performance_data[LEN];
//---
int get_url(char url_request[LEN],char url_response[LEN]) {
int ret,i;
// char readbuf[40960];
char readbuf[LEN];
char *readbuf1;
char readbuf2[LEN];
int mark=0;
char *p,*str;
fd_set rfds;
struct timeval time;
int sd;
struct sockaddr_in his_end;
int ret1,ret2,ret3,j=0;
// printf("------------------------------\n");
// printf("%s\n",url_request);
sd=socket(AF_INET,SOCK_STREAM,0);
// printf("------------------------sd=%d\n",sd);
if(sd==-1) {
fprintf(stderr,"socket error.\n");
return -1;
}
his_end.sin_family=AF_INET;
his_end.sin_port=htons(HIS_PORT);
his_end.sin_addr.s_addr=inet_addr(HIS_IPADDR);
ret=connect(sd,(struct sockaddr *)&his_end,sizeof(his_end));
if(ret==-1) {
perror("connect()");
return -1;
}
//send data
ret=write(sd,url_request,strlen(url_request));
if(ret<0) {
perror("write() error");
return -1;
}
else {
// printf("client write %d bytes .\n",ret);
}
while(1) {
memset(readbuf,0,LEN);
FD_ZERO(&rfds);
FD_SET(sd,&rfds);
time.tv_sec=1;
time.tv_usec=0;
ret=select(sd+1,&rfds,NULL,NULL,&time);
if(ret<0) {
fprintf(stderr,"select() error,ret=%d.\n",ret);
return -1;
// continue;
}
else if(ret>0) {
ret=read(sd,readbuf,LEN);
if(ret<0){
fprintf(stderr,"read() error.\n");
return -1;
// close(sd);
return -1;
}
else if(ret>0) {
// printf("readbuf start--------------------\n");
// printf("%s\n",readbuf);
// strcpy(url_response,readbuf);
strcat(url_response,readbuf);
// printf("readbuf end--------------------\n");
// * 跳过数据不读入
// %*s 过滤掉带空格的第一个字符串(%s遇到空格停止,加*号为了忽略读到的第一个字符串)
// [^字符] 匹配非字符,遇到停止读入
// %*[^>] 过滤掉到>前的第二个字符串
// >%[^<] 取><之间的字符串存到tmp中
// get related count
if(str=strstr(readbuf,"<span id=\"headerForm-loggedInUsersCount")) {
// printf("==================================str1=%s\n",str);
ret1=sscanf(str,"%*s%*[^>]>%[^<]",loggedInUsersCount);
if(ret1==-1) {
fprintf(stderr,"sscanf(ret1) error.\n");
}
}
// if(str=strstr(readbuf,"<span id=\"headerForm-requestCountAll")) {
if(str=strstr(readbuf,"id=\"headerForm-requestCountAll")) {
// printf("==================================str2=%s\n",str);
// ret2=sscanf(str,"%*s%*[^>]>%[^<]",requestCountAll);
ret2=sscanf(str,"%*[^>]>%[^<]",requestCountAll);
if(ret2==-1) {
fprintf(stderr,"sscanf(ret2) error.\n");
}
}
if(str=strstr(readbuf,"<span id=\"headerForm-requestCountUnassigned")) {
// printf("==================================str3=%s\n",str);
ret3=sscanf(str,"%*s%*[^>]>%[^<]",requestCountUnassigned);
if(ret3==-1) {
fprintf(stderr,"sscanf(ret3) error.\n");
}
}
}
else {
break;
}
// printf("------------------test---------------------\n");
/*
// for(p=strtok(readbuf,"^M$\r\n");p;p=strtok(NULL,"^M$\r\n")) {
for(p=strtok(readbuf,"\r\n");p;p=strtok(NULL,"\r\n")) {
str=p;
mark++;
// if(mark==13)
if(mark==7)
break;
}
// printf("%s\n",str);
strcpy(url_response,str);
*/
// break;
// }
}
else {
break;
}
// sleep(2);
}
ret=close(sd);
if(ret==-1) {
fprintf(stderr,"close() error.\n");
return -1;
}
return 0;
}
//---
int parse_url_get_cookie(char get_url_respons[LEN]) {
int ret;
int mark=0;
char *p,*str;
char readbuf[LEN];
char readbuf_cookie[LEN];
char JSESSIONID[LEN];
strcpy(readbuf,get_url_respons);
/*
for(p=strtok(readbuf,"\r\n");p;p=strtok(NULL,"\r\n")) {
str=p;
mark++;
*/
if(str=strstr(readbuf,"Set-Cookie: JSESSIONID=")) {
// printf("1111111111111111111111\n");
ret=sscanf(str,"Set-Cookie: JSESSIONID=%30s;",JSESSIONID);
// printf("ret=%d\n",ret);
memset(Cookie_JSESSIONID,0,sizeof(Cookie_JSESSIONID));
strcpy(Cookie_JSESSIONID,JSESSIONID);
// sscanf(str,"Set-Cookie: JSESSIONID=%[^;]",JSESSIONID);
// for(p=strtok(get_url_respons,"=;");p;p=strtok(NULL,"=;")) {
}
/*
// if(mark==13)
if(mark==7) {
strcpy(readbuf_cookie,str);
for(p=strtok(get_url_respons,"=;");p;p=strtok(NULL,"=;")) {
}
break;
}
*/
// }
// printf("JSESSIONID=%s\n",JSESSIONID);
/*
// if(strstr(get_url_respons,"JSESSIONID")) {
for(p=strtok(get_url_respons,"=;");p;p=strtok(NULL,"=;")) {
// printf("----------%s\n",get_url_respons);
str=p;
mark++;
if(mark==6) {
strcpy(JSESSIONID,str);
break;
}
printf("-----------------------\n");
}
printf("JSESSIONID=%s\n",JSESSIONID);
// }
*/
//
return 0;
}
int main(int argc, char *argv[]) {
int ret,i;
char http_get_url_request[LEN];
char http_get_url_response[LEN];
//1-loginScreen.xhtml ---------------------------
memset(http_get_url_request,0,LEN);
memset(http_get_url_response,0,LEN);
strcat(http_get_url_request,"GET /ccc/views/loginScreen.xhtml?ccname=cu HTTP/1.1\n");
strcat(http_get_url_request,"Host: 160.48.236.147\n");
strcat(http_get_url_request,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
strcat(http_get_url_request,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\n");
strcat(http_get_url_request,"Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4\n\r\n");
strcat(http_get_url_request,"Accept-Encoding: gzip, deflate, sdch\n");
strcat(http_get_url_request,"Connection: keep-alive\n");
// printf("main request=%s",http_get_url_request);
// printf("-----------------------\n");
ret=get_url(http_get_url_request,http_get_url_response);
if(ret!=0) {
fprintf(stderr,"get_url() error.\n");
}
// printf("main response=%s\n",http_get_url_response);
ret=parse_url_get_cookie(http_get_url_response);
if(ret!=0) {
fprintf(stderr,"parse_url_get_cookie() error.\n");
}
// printf("JSESSIONID=%s\n",Cookie_JSESSIONID);
// printf("\n----------------------------------------------------------\n");
// printf("\n----------------------------------------------------------\n");
//2-loginScreen.xhtml ----------------------------------------------------------
memset(http_get_url_request,0,LEN);
memset(http_get_url_response,0,LEN);
strcat(http_get_url_request,"POST /ccc/views/loginScreen.xhtml;jsessionid=");
strcat(http_get_url_request,Cookie_JSESSIONID);
strcat(http_get_url_request," HTTP/1.1\n");
strcat(http_get_url_request,"Host: 160.48.236.147\n");
strcat(http_get_url_request,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\n");
strcat(http_get_url_request,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\n");
strcat(http_get_url_request,"Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4\n");
strcat(http_get_url_request,"Accept-Encoding: gzip, deflate\n");
strcat(http_get_url_request,"Connection: keep-alive\n");
strcat(http_get_url_request,"Referer: http://160.48.236.147/ccc/views/loginScreen.xhtml?ccname=cu\n");
strcat(http_get_url_request,"Content-Type: application/x-www-form-urlencoded\n");
strcat(http_get_url_request,"Content-Length: 151\n");
strcat(http_get_url_request,"Cookie: ccname=cu; JSESSIONID=");
strcat(http_get_url_request,Cookie_JSESSIONID);
strcat(http_get_url_request,"\n\r\n");
strcat(http_get_url_request,"loginForm=loginForm&loginForm-loginUsernameInput=CUTSP&loginForm-loginPasswordInput=bmw%2C123%21&loginForm-login=Login&javax.fac
es.ViewState=stateless");
// printf("main request=%s",http_get_url_request);
// printf("-----------------------\n");
ret=get_url(http_get_url_request,http_get_url_response);
if(ret!=0) {
fprintf(stderr,"get_url() error.\n");
}
// printf("main response=%s\n",http_get_url_response);
// printf("\n----------------------------------------------------------\n");
//3-homeScreen.xhtml -----------------------------------------------------------------
memset(http_get_url_request,0,LEN);
memset(http_get_url_response,0,LEN);
strcat(http_get_url_request,"GET /ccc/views/homeScreen.xhtml HTTP/1.1\n");
strcat(http_get_url_request,"Host: 160.48.236.147\n");
strcat(http_get_url_request,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\n");
strcat(http_get_url_request,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\n");
strcat(http_get_url_request,"Referer: http://160.48.236.147/ccc/views/loginScreen.xhtml\n");
strcat(http_get_url_request,"Accept-Encoding: gzip, deflate, sdch\n");
strcat(http_get_url_request,"Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4\n");
strcat(http_get_url_request,"Connection: keep-alive\n");
strcat(http_get_url_request,"Cookie: ccname=cu; JSESSIONID=");
strcat(http_get_url_request,Cookie_JSESSIONID);
strcat(http_get_url_request,"\n\r\n");
// printf("main request=%s",http_get_url_request);
// printf("-----------------------\n");
// clean char array
memset(loggedInUsersCount,0,SHORT_LEN);
memset(requestCountAll,0,SHORT_LEN);
memset(requestCountUnassigned,0,SHORT_LEN);
ret=get_url(http_get_url_request,http_get_url_response);
if(ret!=0) {
fprintf(stderr,"get_url() error.\n");
}
// printf("main response=%s\n",http_get_url_response);
// printf("\n----------------------------------------------------------\n");
// printf count
// printf("loggedInUsersCount=%s\n",loggedInUsersCount);
// printf("requestCountAll=%s\n",requestCountAll);
// printf("requestCountUnassigned=%s\n",requestCountUnassigned);
//
sprintf(status_information,"loggedInUsersCount=%s,requestCountAll=%s,requestCountUnassigned=%s",loggedInUsersCount,requestCountAll,requestCountUnassigned);
// sprintf(performance_data,"loggedInUsersCount=%s;;;0;100 requestCountAll=%s;;;0;100 requestCountUnassigned=%s;;;0;100",loggedInUsersCount,requestCountAll,reque
stCountUnassigned);
sprintf(performance_data,"loggedInUsersCount=%s;;;0; requestCountAll=%s;;;0; requestCountUnassigned=%s;;;0;",loggedInUsersCount,requestCountAll,requestCountUn
assigned);
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
//3.1-----------------------------------------------------------------
// memset(http_get_url_request,0,LEN);
// memset(http_get_url_response,0,LEN);
// strcat(http_get_url_request,"GET /ccc/views/homeScreen.xhtml HTTP/1.1\n");
// strcat(http_get_url_request,"Host: 160.48.236.147\n");
// strcat(http_get_url_request,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\n");
// strcat(http_get_url_request,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\n");
// strcat(http_get_url_request,"Accept-Encoding: gzip, deflate, sdch\n");
// strcat(http_get_url_request,"Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4\n");
// strcat(http_get_url_request,"Connection: keep-alive\n");
// strcat(http_get_url_request,"Faces-Request: partial/ajax\n");
// strcat(http_get_url_request,"Content-Type: application/x-www-form-urlencoded;charset=UTF-8\n");
// strcat(http_get_url_request,"Referer: http://160.48.236.147/ccc/views/loginScreen.xhtml\n");
// strcat(http_get_url_request,"Content-Length: 306\n");
// strcat(http_get_url_request,"Cookie: ccname=cu; JSESSIONID=");
// strcat(http_get_url_request,Cookie_JSESSIONID);
// strcat(http_get_url_request,"\n\r\n");
strcat(http_get_url_request,"Pragma: no-cache\n");
strcat(http_get_url_request,"Cache-Control: no-cache\n\r\n");
// strcat(http_get_url_request,"headerForm=headerForm&javax.faces.ViewState=-1945610166057734755%3A-9043230496343990415&javax.faces.source=headerForm-refreshStat
s&javax.faces.partial.event=click&javax.faces.partial.render=headerForm-headerAgent%20headerForm-criticalMessages&javax.faces.behavior.event=action&javax.faces.partia
l.ajax=true");
/* printf("main request=%s",http_get_url_request);
printf("-----------------------\n");
ret=get_url(http_get_url_request,http_get_url_response);
if(ret!=0) {
fprintf(stderr,"get_url() error.\n");
}
printf("main response=%s\n",http_get_url_response);
*/
//4-----------------------------------------------------------------
// memset(http_get_url_request,0,LEN);
// memset(http_get_url_response,0,LEN);
// strcat(http_get_url_request,"GET /ccc/resources/javascript/openLayer/OpenLayers-2.1.2.debug.js HTTP/1.1\n");
// strcat(http_get_url_request,"Host: 160.48.236.147\n");
// strcat(http_get_url_request,"User-Agent: ipad\n");
// strcat(http_get_url_request,"Accept: */*\n");
// strcat(http_get_url_request,"Accept-Language: en-us,en;q=0.5\n");
// strcat(http_get_url_request,"Accept-Encoding: gzip, deflate\n");
// strcat(http_get_url_request,"Connection: keep-alive\n");
// strcat(http_get_url_request,"Referer: http://160.48.236.147/ccc/views/homeScreen.xhtml\n");
// strcat(http_get_url_request,"Cookie: ccname=cu; JSESSIONID=");
// strcat(http_get_url_request,Cookie_JSESSIONID);
// strcat(http_get_url_request,"\n");
// strcat(http_get_url_request,"If-Modified-Since: Mon, 02 Feb 2015 17:18:03 GMT\n");
// strcat(http_get_url_request,"If-None-Match: W/\"766780-1422897483000\"\n\r\n");
// strcat(http_get_url_request,"\n\r\n");
// printf("main request=%s",http_get_url_request);
// printf("-----------------------\n");
// ret=get_url(http_get_url_request,http_get_url_response);
// if(ret!=0) {
// fprintf(stderr,"get_url() error.\n");
// }
// printf("main response=%s\n",http_get_url_response);
// printf("-----------------------\n");
//5-----------------------------------------------------------------
// memset(http_get_url_request,0,LEN);
// memset(http_get_url_response,0,LEN);
// strcat(http_get_url_request,"GET /ccc/pubsub/homeScreen?agentId=1755&ccId=CU&X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=1.0.13&X-Atmosphere-Transport=l
ong-polling&X-Atmosphere-TrackMessageSize=true&X-Cache-Date=0&_=1423316987021 HTTP/1.1\n");
// strcat(http_get_url_request,"Host: 160.48.236.147\n");
// strcat(http_get_url_request,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\n");
// strcat(http_get_url_request,"Accept: image/png,image/*;q=0.8,*/*;q=0.5\n");
// strcat(http_get_url_request,"Accept-Encoding: gzip, deflate, sdch\n");
// strcat(http_get_url_request,"Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4\n");
// strcat(http_get_url_request,"Connection: keep-alive\n");
// strcat(http_get_url_request,"Referer: http://160.48.236.147/ccc/views/homeScreen.xhtml\n");
// strcat(http_get_url_request,"Cookie: ccname=cu; JSESSIONID=");
// strcat(http_get_url_request,Cookie_JSESSIONID);
// strcat(http_get_url_request,"\n\r\n");
// printf("main request=%s",http_get_url_request);
// printf("-----------------------\n");
// ret=get_url(http_get_url_request,http_get_url_response);
// if(ret!=0) {
// fprintf(stderr,"get_url() error.\n");
// }
// printf("main response=%s\n",http_get_url_response);
// printf("-----------------------\n");
/*
// printf("=======================================================================================================\n");
strcat(status_information,url_status_information);
strcat(performance_data,url_performance_data);
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
*/
}