- 论坛徽章:
- 0
|
我写了一个测试telnet的小程序
./tntest 127.0.0.1
程序会交互式地要求输入,并将反馈打印出来
先输入: ff fc 18 ff fc 1f ff fc 23 ff fc 27 ff fc 24
打印为
IAC WONT TERMINAL TYPE IAC WONT NAWS IAC WONT XDISPLOC IAC WONT NEW-ENVIRON IAC WONT OLD-ENVIRON
反馈:IAC DONT AUTHENTICATION IAC DO TERMINAL TYPE
再输入 ff fe 01 ff fe 03 ff fc 01 vvvlogin 0d 0a
注意vvvlogin要求程序一直读到"login"为止
反馈:
AIX Version 5
(C) Copyrights by IBM and by others 1982, 2000.
login:
prompt matched
selread, ret = 178
再输入用户 william
vvvPassword william 0d 0a
如此一起下去,直到 vvv$ exit 0d 0a退出
typedef struct {
int n;
char p[32];
} prompt_t;
static void telnet_print(unsigned char *buf, size_t n);
static const char sep[] = " \r\n";
static ssize_t selread(int fd, void *vptr, size_t n, int *pflag, prompt_t *pattern);
static char *bstrstr(char *bstr, prompt_t *prompt, size_t nmax);
char line[256], buf[16384];
int main(int argc, char *argv[])
{
char *p, *key, *next, *pe;
struct sockaddr_in sa;
prompt_t pattern;
int sock, bconnected = 0;
int n, value;
int lastflag;
unsigned char c;
if(argc == 1) {
return -1;
}
bzero(&sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(argv[1]);
if(2 == argc)
sa.sin_port = htons(23);
else
sa.sin_port = htons((short)atoi(argv[2]));
sock = socket(AF_INET, SOCK_STREAM, 0);
for(c=15; c<=19; c++)
printf("%.2x: %s\t", c+236, telcmds[c]);
printf("\ntelnet options");
for(c=0; c<=39; c++) {
if(c % 5 == 0) printf("\n");
printf("%.2x: %s\t", c, telopts[c]);
}
printf("\n");
pattern.n = 0;
while(1) {
printf("\nplease input: ");
fflush(stdout);
fgets(line, 256, stdin);
if(line[0] == 'q') break;
next = line;
while(' ' == *next)
next++;
p = buf;
pattern.n = 0;
while(1)
if('\'' == (c = *next) || (c == '\"')) {
pe = strchr(++next, c);
*pe = '\0';
n = pe - next;
telnet_print(buf, n);
if(bconnected == 0) {
if(connect(sock, (SA *)&sa, sizeof(sa)) < 0) {
printf("connect fail\n");
close(sock);
return -1;
}
bconnected = 1;
}
write(sock, buf, n);
n = selread(sock, buf, 16384, &lastflag, &pattern);
printf("selread, ret = %d\n", n);
if(lastflag == 1)
break;
}
if(bconnected)
close(sock);
return 0;
}
void telnet_print(unsigned char *buf, size_t n)
{
int i;
for(i=0; i<n; i++, buf++)
if(*buf == 0xff && *(buf+1) != 0xff) {
printf("IAC %s %s ",
telcmds[*(buf+1)-236], telopts[*(buf+2)]);
i += 2;
buf += 2;
} else
putchar(*buf);
printf("\n");
}
ssize_t selread(int fd, void *vptr, size_t n, int *pflag, prompt_t *pattern)
{
fd_set rfds;
struct timeval tv;
size_t count, nleft;
ssize_t nread;
char *ptr;
int c;
ptr = vptr;
nleft = n;
*pflag = 0;
count = 0;
while(nleft > 0) {
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
if((c = select(fd+1, &rfds, NULL, NULL, &tv)) < 0) {
if(errno == EINTR)
continue;
syslog(LOG_INFO, "selread: %s\n", strerror(errno));
*pflag = -1;
break;
}
if(c == 0) {
*pflag = -2;
break;
}
if(FD_ISSET(fd, &rfds)) {
nread = read(fd, ptr, nleft);
if(nread < 0) {
if(errno == EINTR)
continue;
syslog(LOG_INFO, "selread: %s\n", strerror(errno));
*pflag = -3;
break;
}
if(nread == 0) {
*pflag = 1;
break;
}
telnet_print(ptr, nread);
nleft -= nread;
count += nread;
ptr += nread;
if(pattern->n == 0 || bstrstr((char *)vptr, pattern, count)) {
break;
}
}
}
return(n - nleft);
}
char *bstrstr(char *bstr, prompt_t *prompt, size_t nmax)
{
char *s, *p, *q;
int i;
for(s=bstr; s<=bstr+nmax-prompt->n; s++)
if(*s == *prompt->p) {
for(i=1, p=s+1, q=prompt->p+1; i<prompt->n; i++, p++, q++)
if(*p != *q)
break;
if(i == prompt->n) {
printf("prompt matched\n");
return s;
}
}
return NULL;
} |
|