Perl shell as Telnet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • casybay
    New Member
    • Oct 2007
    • 35

    Perl shell as Telnet

    Hi all,

    I am a newbie in Perl. I am wondering if anyone has ever written a perl script which can be run as telnet. Could you please give me some hint or where to start to look at?

    Thanks!!

    Casy
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    I don't understand what you mean by "run as telnet" but see if anything here helps you:

    Comment

    • casybay
      New Member
      • Oct 2007
      • 35

      #3
      Sorry for the confusion. I mean a command that establish a connection to a server as telnet does.
      Originally posted by KevinADC
      I don't understand what you mean by "run as telnet" but see if anything here helps you:

      http://search.cpan.org/search?query=telnet&mode=all

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        You use sockets to connect to remote computers. Look into IO::Socket, it comes with perl.

        Comment

        • casybay
          New Member
          • Oct 2007
          • 35

          #5
          Thanks.
          Just finished the coding. I did this because I would like to see what will happen if I keep establishing new socket with a server and keep sending it requests. Will I be blocked or any other consequence? Or what are the reasons a server limits connections to it.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by casybay
            Thanks.
            Just finished the coding. I did this because I would like to see what will happen if I keep establishing new socket with a server and keep sending it requests. Will I be blocked or any other consequence? Or what are the reasons a server limits connections to it.
            I don't know the answer to those questions.

            Comment

            • Icecrack
              Recognized Expert New Member
              • Sep 2008
              • 174

              #7
              Originally posted by casybay
              Thanks.
              Just finished the coding. I did this because I would like to see what will happen if I keep establishing new socket with a server and keep sending it requests. Will I be blocked or any other consequence? Or what are the reasons a server limits connections to it.
              the answer is ask the server provider, they are the ones who know if there is a limit on a server connection.

              Comment

              • casybay
                New Member
                • Oct 2007
                • 35

                #8
                Okay, then let's go back to perl. How do I generate multiple (i) threads that each binds to a TCP connection to the same server (I already have this code done)? The value of i won't know until runtime pass at argument.
                Thanks!

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by casybay
                  Okay, then let's go back to perl. How do I generate multiple (i) threads that each binds to a TCP connection to the same server (I already have this code done)? The value of i won't know until runtime pass at argument.
                  Thanks!
                  I have no idea how to do that, sorry, maybe Icecrack has a suggestion or further advise.

                  Comment

                  • Icecrack
                    Recognized Expert New Member
                    • Sep 2008
                    • 174

                    #10
                    Originally posted by casybay
                    Okay, then let's go back to perl. How do I generate multiple (i) threads that each binds to a TCP connection to the same server (I already have this code done)? The value of i won't know until runtime pass at argument.
                    Thanks!

                    as we can't see any code how are we suppose to help answer that question and if you show code then i can see how to do it with your code.

                    Comment

                    • casybay
                      New Member
                      • Oct 2007
                      • 35

                      #11
                      Em..I don't have it with me now. But let's just say I have a sub A, and I want to bind this sub to multiple threads. The number of threads won't know until I pass an argument in run time.

                      Comment

                      • Icecrack
                        Recognized Expert New Member
                        • Sep 2008
                        • 174

                        #12
                        Originally posted by casybay
                        Em..I don't have it with me now. But let's just say I have a sub A, and I want to bind this sub to multiple threads. The number of threads won't know until I pass an argument in run time.
                        we will wait till you get code posted.

                        Comment

                        • casybay
                          New Member
                          • Oct 2007
                          • 35

                          #13
                          The br_tcp.pl code following is the one that I want to binds to multiple threads.
                          It may be dumb to ask, if I put the following code in a loop, since the file handle is the same (F) each time, does that mean the socket is the same?
                          Thanks.
                          Code:
                          #!/usr/bin/perl
                          #test.pl
                          use Socket;
                          require "br_tcp.pl";
                          
                          my $ser_nam;
                          
                          # connect to server, F is the file handler, first argument is server name, second is port number
                          if (open_TCP(F, $ARGV[0], $ARGV[1]) == undef) {
                            print "Error connecting to server at $ARGV[0] port $ARGV[1].\n";
                            exit(-1);
                          }else {
                          	$ser_nam = $ARGV[0];
                          }
                          
                          $req = <STDIN>;
                          $hos = <STDIN>;
                          print F "$req$hos\n\n";
                          print $_ while (<F>);
                           
                          close(F);
                          Code:
                          #!/usr/bin/perl -w
                          #br_tcp.pl
                          use Socket;
                          
                          ##############################################################
                          
                          # socket connection
                          # Given ($f, $dest, $port) return 1 if successful, undef when unsuccessful.
                          
                          ###############################################################
                          sub open_TCP{
                          	#get parameters: file(which file handle to associate with the socket),destination(a host name or IP address), port
                          	#specify wether the socket should be stream-oriented or record-oriented. For HTTP transactions, scokets are stream-oriented connections
                          	#running TCP over IP, so HTTP-based applications must associate these characteristics with a newly created socket.
                          	#put f, dest and port into variable 
                          	my ($f,$dest,$port) = @_;
                          	
                          	#PF_INET indicates the Internet Protocol while getprotobyname('tcp') indicates that the TCP runs on top of IP.
                          	#SOCK_STREAM indicates that the sockets is stream-oriented
                              #my $proto = getprotobyname('tcp');
                          	socket($f,PF_INET,SOCK_STREAM,getprotobyname('tcp'));
                          	
                          	#The Socket::sockaddr_in() routine accpets a port number and a 32-bit IP address; Socket::inet_aton() translates a hostname string or 
                          	#dotted decimal string to a 32-bit IP address.
                          	my $sin = sockaddr_in($port,inet_aton($dest));
                          	connect($f,$sin) || return undef;
                          	
                          	my $old_fh = select($f);
                          	#don't buffer output
                          	$| = 1;   
                          	select($old_fh);
                          	1;
                          }
                          1;

                          Comment

                          • Icecrack
                            Recognized Expert New Member
                            • Sep 2008
                            • 174

                            #14
                            Originally posted by casybay
                            The br_tcp.pl code following is the one that I want to binds to multiple threads.
                            It may be dumb to ask, if I put the following code in a loop, since the file handle is the same (F) each time, does that mean the socket is the same?
                            Thanks.
                            Code:
                            #!/usr/bin/perl
                            #test.pl
                            use Socket;
                            require "br_tcp.pl";
                            
                            my $ser_nam;
                            
                            # connect to server, F is the file handler, first argument is server name, second is port number
                            if (open_TCP(F, $ARGV[0], $ARGV[1]) == undef) {
                              print "Error connecting to server at $ARGV[0] port $ARGV[1].\n";
                              exit(-1);
                            }else {
                            	$ser_nam = $ARGV[0];
                            }
                            
                            $req = <STDIN>;
                            $hos = <STDIN>;
                            print F "$req$hos\n\n";
                            print $_ while (<F>);
                             
                            close(F);

                            try changing:
                            Code:
                            print $_ while (<F>);
                            to :
                            Code:
                            while (<F>)
                            {
                            print $_;
                            }

                            Comment

                            Working...