忙活了一个晚上,一个早上,一个中午
做出来了~~~
C代码(需要python_do_cmd解析器,sunduino中有):- #include <stdlib.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <ctype.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdarg.h>
- #define run_in_backstage 0
- #define nolog 0
- int pid = -1;
- void clear(char* txt){
- //txt[0] = '\0';
- int sz = sizeof(txt);
- int i = 0;
- for(i = 0;i < sz;i++){
- txt[i] = '\0';
- }
- }
- int print(char *fmt, ...)
- {
- if(pid != 0||nolog){
- va_list argptr;
- int cnt;
- va_start(argptr, fmt);
- cnt = vprintf(fmt, argptr);
- va_end(argptr);
- return(cnt);
- }else{
- return(0);
- }
- }
- int main()
- {
- int sfp,nfp,recbytes;
- FILE *fl;
- char buffer[1025];
- char text[100] = "Hey guy,you already login to the Cubieboard.\r\nThis is Sunduino shell.\r\n";
- //May not Cb,another board?Than you can edit this.
- char pmt[] = ">";
- char cmd[1024];
- struct sockaddr_in s_add,c_add;
- int sin_size;
- char result[1000] = "";
- int size = sizeof(result)/sizeof(char);
- unsigned short portnum=23;
- char ers[1024] = "\n";
- int rsz = 0;
- char terminfo[50];
- if(run_in_backstage){
- pid = fork();
- if(pid != 0){
- print("Ok,runs in backstage!Backstage program's pid is %d\n",pid);
- return 0;
- }else{
- print("Backstage started!\n");
- }
- }
- setenv("remote","1",0);
- sprintf(text,"%s%s",text,pmt);
- print("Telnetd start\n");
- sfp = socket(AF_INET, SOCK_STREAM, 0);
- if(-1 == sfp)
- {
- print("socket fail!\n");
- return -1;
- }
- print("socket ok!\n");
- bzero(&s_add,sizeof(struct sockaddr_in));
- s_add.sin_family=AF_INET;
- s_add.sin_addr.s_addr=htonl(INADDR_ANY);
- s_add.sin_port=htons(portnum);
- if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
- {
- print("bind fail!\n");
- return -1;
- }
- print("bind ok!\n");
- if(-1 == listen(sfp,5))
- {
- print("listen fail!\n");
- return -1;
- }
- print("listen ok!\n");
- while(1)
- {
- sin_size = sizeof(struct sockaddr_in);
- nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
- if(-1 == nfp)
- {
- print("accept fail!\n");
- return -1;
- }
- print("accept ok!\nServer start get connect from %#x : %#x\n",ntohl(c_add.sin_addr.s_addr),ntohs(c_add.sin_port));
- print("Press any key to close this connection.\n");
- if(-1 == (recbytes = read(nfp,buffer,1024)))
- {
- print("read data1 fail !\n");
- return -1;
- }
- buffer[recbytes]='\0';
- int i;
- print("Source first:\n");
- for(i=0;i<recbytes;i++){
- print("%d:%d -- %c\n",i,buffer[i],buffer[i]);
- }
- char stxt[] = {255,253,24,255,250,24,1,255,240};
- //These character will let server tells the client:
- //"I want to know what term are you using".
- //I get this from Baidu Baike and Baidu Wenku
- print("%s %d\n",stxt,write(nfp,stxt,sizeof(stxt)));
- if(-1 == (recbytes = read(nfp,buffer,1024)))
- {
- print("read term type fail!\n");
- return -1;
- }
- buffer[recbytes]='\0';
- if(buffer[0] == 255 && buffer[1] == 250 && buffer[3] == 0){
- //From baike and wenku,start
- print("Your terminal type:");
- //Many telnet client's terminal type is in "Assigned Numbers RFC" document
- //You may can get what's it means from termcap or terminfo database
- //That's from baike
- i=4;
- while((i+1)<recbytes){
- if(buffer[i] == 255 && buffer[i+1] == 240){
- //If client says "end",then end,baike~
- break;
- }
- if(isalpha(buffer[i])){
- terminfo[i-4] = (buffer[i]+32);
- }else{
- terminfo[i-4] = buffer[i];
- }
- i++;
- }
- terminfo[i] = '\0';
- print("%s\n",terminfo);
- setenv("remote_term",terminfo,0);
- }
- if(-1 == write(nfp,text,sizeof(text)))
- {
- print("write fail!\n");
- return -1;
- }
- print("write ok!\n");
- //getchar();
- //print("sre");
- /*if(-1 == (recbytes = read(nfp,buffer,1024)))
- {
- print("read data fail !\r\n");
- }
- buffer[recbytes]='\0';
- print("Data:%s\r\n",buffer);
- print("Source:\r\n");
- for(i=0;i<recbytes;i++){
- print("%d:%d -- %c\r\n",i,buffer[i],buffer[i]);
- }*/
- while(1){
- //While while while...infinity while
- if(-1 == (recbytes = read(nfp,buffer,1024)))
- {
- print("read data fail!\n");
- }
- //Read data again,again and again
- if(recbytes > 0){
- //If it has some data
- buffer[recbytes]='\0';
- print("Data:%s\n",buffer);
- print("Source:\n");
- for(i=0;i<recbytes;i++){
- print("%d:%d -- %c\n",i,buffer[i],buffer[i]);
- }
- if(sizeof(buffer)/sizeof(char) >= 3){
- if(buffer[0] == 255 && buffer[1] == 251 && buffer[2] == 6){
- //CTRL+C to exit(linux)
- close(nfp);
- break;
- }
- }
- if(sizeof(buffer)/sizeof(char) >= 2){
- if(buffer[0] == 255 && buffer[1] == 248){
- //CTRL+C to exit(linux)
- close(nfp);
- break;
- }
- if(buffer[recbytes-2] == 13 && buffer[recbytes-1] == 10){
- //if has \r\n
- buffer[recbytes-2] = '\0';
- buffer[recbytes-1] = '\0';
- print("Run: %s\n",buffer);
- sprintf(cmd,"python_do_cmd "%s" 2>&1",buffer);
- clear(result);
- fl = popen(cmd,"r");
- print("RES1%s\n",result);
- rsz = fread(result,sizeof(char),sizeof(result),fl);
- //getResultFromSystemCall(cmd,result,size);
- pclose(fl);
- result[rsz] = '\0';
- print("Count:%d\n",rsz);
- print("RES2%s\n",result);
- }else{
- print("Run: %s",buffer);
- sprintf(cmd,"python_do_cmd "%s" 2>&1",buffer);
- clear(result);
- fl = popen(cmd,"r");
- print("RES1%s\n",result);
- rsz = fread(result,sizeof(char),sizeof(result),fl);
- //getResultFromSystemCall(cmd,result,size);
- pclose(fl);
- print("Count:%d\n",rsz);
- result[rsz] = '\0';
- print("RES%s\n",result);
- }
- }
- int p=0,q=0;
- for(i=0;i<sizeof(result)/sizeof(char);i++){
- /*if(result[i] == '\0') break;
- if(result[i] == '\r') result[i] == NULL;*/
- if(result[q] == '\r'){
- q++;
- }else{
- if(q != p){
- result[p] = result[q];
- }
- p++;
- q++;
- }
- if(result[q] == '\0'){
- result[p] = '\0';
- break;
- }
- }
- sprintf(ers,"%s\r%s",result,pmt);
- if(-1 == write(nfp,ers,(rsz+sizeof(pmt)+1)))
- {
- print("write opt fail!\n");
- return -1;
- }
- print("ERS%s\n",ers);
- print("write opt ok!\n");
- clear(ers);
- print("RES%s\n",result);
- }
- }
- /*print("Source from client:\r\n");
- print("%s\r\n",buffer);*/
- close(nfp);
- //break;
- //Will not break and get the next connection
- }
- close(sfp);
- return 0;
- }
复制代码 |