#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "server.h"
int main()
{
int sockfd;
int fd_in[2];
int fd_out[2];
pipe(fd_in);
pipe(fd_out);
sockfd = creat_server(5556);
if(fork()==0) //run shell
{
close(fd_out[0]);
close(fd_in[1]);
dup2(fd_in[0],0);
dup2(fd_out[1],1);
dup2(fd_out[1],2);
execl("/bin/bash","bash",NULL);
perror("bash");
while(1);
}
close(fd_out[1]);
close(fd_in[0]);
if(fork()==0) //read cmd from socket
{
close(fd_out[0]);
while(1)
{
char cmd_buf[100];
int i = 0;
char ch;
while(1)
{
i = 0;
memset(cmd_buf, 0, 100);
write(sockfd, "Telnet>#", 9);
read(sockfd, &ch, 1);
while(ch!='\n')
{
if(ch==8)
{
cmd_buf[i-1]='\0';
i--;
}
else
{
cmd_buf[i++]=ch;
}
read(sockfd, &ch, 1);
}
cmd_buf[i-1]='\0';
printf("get cmd: #%s#\n",cmd_buf);
if(cmd_buf[0]=='1')
close(sockfd);
if(!exe_telnet_fun(sockfd, cmd_buf))
{
write(fd_in[1], cmd_buf, strlen(cmd_buf));
write(fd_in[1], "\n", 2);
}
usleep(300*1000);
}
}
}
close(fd_in[1]);
while(1) //send cmd result to socket
{
char buf[200]="";
dup2(fd_out[0],0);
fgets(buf, 100, stdin);
printf("gets buf=%s\n",buf);
buf[strlen(buf)+1]='\r';
write(sockfd, buf, strlen(buf)+2);
}
return 0;
}
|