LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-31-2008, 01:45 PM   #1
monteo
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Rep: Reputation: 0
testing telnet: howto


Hello, I'm trying to test a tcp connection using telnet, I can't use expect or perl. I'm running telnet in the background (seeing how it's input oriented) then chekcing netstat for an established connection, and if found killing the telnet process, then displaying results. Any help would be greatly appreciated. Note, if you're going to use my script for testing please change the x.x.x.x with a valid ip address and possibly 1500 with a valid port. Also, these commands work just fine when running from the command line, however I always get a connection unseccessful when running in a script.

Code:
#!/bin/ksh
#variable definitions
  export SERVER_IP=x.x.x.x
  export SERVER_PORT=1500
  export results=./results

  if [[ -e "$results" ]]; then
    cat /dev/null > $results
  else
    touch $results
  fi

  #clear screen
  clear

  #initiate connection
  telnet $SERVER_IP $SERVER_PORT 1> /dev/null 2> $results &


  #check for established connection
  if [[ -n "`netstat -an|grep $SERVER_IP|grep -i $SERVER_PORT|grep -i established`" ]];then
    echo "\tconnection successful
    
    #close connection
    ps -ef|grep "telnet $SERVER_IP $SERVER_PORT"|grep -v grep|awk '{print $2}' | xargs -i kill -9 {}
    rm -f $results
    results=""
  else
    echo "\tconnection unsuccessful"
    echo "\t`cat $results`"
    rm -f $results
    results=""
  fi
 
Old 01-31-2008, 03:12 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,891

Rep: Reputation: 68
In your script, telnet goes to background and you check output nearly immediately, before anything useful has time to occur via network. Try putting sleep 1.
 
Old 01-31-2008, 03:40 PM   #3
monteo
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Original Poster
Rep: Reputation: 0
I've tried sleeping for up to 10 seconds with no change in status. Also, more often then not, the script won't report a successful connection. Though sometimes it does, leaving me to believe it's a timing issue as well...but having it sleep doesn't change anything. And, when running directly from the command line I get established from netstat immediately.
 
Old 02-01-2008, 05:31 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,891

Rep: Reputation: 68
Looks like telnet actually uses that its input is a terminal. Because when I wrapped telnet invocation in a simple wrapper
Code:
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

int main(int argc, char ** argv, char ** envp)
{
        int fd, fd2, ptn, bit;
        char ptname[100];
        char readbuf[1025], writebuf[1025];
        size_t readc, writec;
        pid_t readpid,writepid,shellpid;
        int exitstatus;

        fd=open("/dev/ptmx", O_RDWR);

        ioctl(fd, TIOCGPTN, &ptn);

        bit=0;
        ioctl(fd, TIOCSPTLCK, &bit);

        snprintf(ptname, 99, "/dev/pts/%d", ptn);

        fd2=open(ptname, O_RDWR);

        if (!(readpid=fork())){
                while(1) {
                        readc=read(0, readbuf, 1024);
                        write(fd, readbuf, readc);
                }
        }

        if (!(writepid=fork())){
                while(1) {
                        writec=read(fd, writebuf, 1024);
                        write(1, writebuf, writec);
                }
        }

        if (!(shellpid=fork())){
                dup2(fd2,0);
                dup2(fd2,1);
                dup2(fd2,2);

                setpgrp();

                execvp(argv[1], argv+1);
        }

        waitpid(shellpid, &exitstatus, 0);

        kill (readpid, SIGTERM);
        kill (writepid, SIGTERM);

        return exitstatus;
}
- basically it does the main thing expect gives - everything works.
 
  


Reply

Tags
linux, networking, telnet


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace 'etch' with 'testing' in /etc/apt/sources.list to track 'testing' branch? Akhran Debian 3 04-09-2007 10:45 AM
LXer: Updates on Testing Dell and a Linksys WRT 54G Howto LXer Syndicated Linux News 0 12-12-2005 01:16 PM
Telnet - HOWTO koodoo Linux - Newbie 9 02-22-2005 07:37 PM
Setup as getting debian testing files from ftp - will it stay with testing BrianHenderson Debian 2 09-02-2004 06:06 PM
howto install gnome 2.6 on Debian testing zero79 Debian 3 06-07-2004 05:27 PM


All times are GMT -5. The time now is 06:36 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration