By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,695 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,695 IT Pros & Developers. It's quick & easy.

Perl shell as Telnet

P: 35
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
Oct 20 '08 #1
Share this Question
Share on Google+
13 Replies


KevinADC
Expert 2.5K+
P: 4,059
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
Oct 20 '08 #2

P: 35
Sorry for the confusion. I mean a command that establish a connection to a server as telnet does.
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
Oct 20 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
You use sockets to connect to remote computers. Look into IO::Socket, it comes with perl.
Oct 20 '08 #4

P: 35
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.
Oct 20 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
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.
Oct 20 '08 #6

Icecrack
Expert 100+
P: 174
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.
Oct 20 '08 #7

P: 35
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!
Oct 21 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
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.
Oct 21 '08 #9

Icecrack
Expert 100+
P: 174
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.
Oct 21 '08 #10

P: 35
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.
Oct 21 '08 #11

Icecrack
Expert 100+
P: 174
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.
Oct 21 '08 #12

P: 35
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.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #test.pl
  3. use Socket;
  4. require "br_tcp.pl";
  5.  
  6. my $ser_nam;
  7.  
  8. # connect to server, F is the file handler, first argument is server name, second is port number
  9. if (open_TCP(F, $ARGV[0], $ARGV[1]) == undef) {
  10.   print "Error connecting to server at $ARGV[0] port $ARGV[1].\n";
  11.   exit(-1);
  12. }else {
  13.     $ser_nam = $ARGV[0];
  14. }
  15.  
  16. $req = <STDIN>;
  17. $hos = <STDIN>;
  18. print F "$req$hos\n\n";
  19. print $_ while (<F>);
  20.  
  21. close(F);
  22.  
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. #br_tcp.pl
  3. use Socket;
  4.  
  5. ##############################################################
  6.  
  7. # socket connection
  8. # Given ($f, $dest, $port) return 1 if successful, undef when unsuccessful.
  9.  
  10. ###############################################################
  11. sub open_TCP{
  12.     #get parameters: file(which file handle to associate with the socket),destination(a host name or IP address), port
  13.     #specify wether the socket should be stream-oriented or record-oriented. For HTTP transactions, scokets are stream-oriented connections
  14.     #running TCP over IP, so HTTP-based applications must associate these characteristics with a newly created socket.
  15.     #put f, dest and port into variable 
  16.     my ($f,$dest,$port) = @_;
  17.  
  18.     #PF_INET indicates the Internet Protocol while getprotobyname('tcp') indicates that the TCP runs on top of IP.
  19.     #SOCK_STREAM indicates that the sockets is stream-oriented
  20.     #my $proto = getprotobyname('tcp');
  21.     socket($f,PF_INET,SOCK_STREAM,getprotobyname('tcp'));
  22.  
  23.     #The Socket::sockaddr_in() routine accpets a port number and a 32-bit IP address; Socket::inet_aton() translates a hostname string or 
  24.     #dotted decimal string to a 32-bit IP address.
  25.     my $sin = sockaddr_in($port,inet_aton($dest));
  26.     connect($f,$sin) || return undef;
  27.  
  28.     my $old_fh = select($f);
  29.     #don't buffer output
  30.     $| = 1;   
  31.     select($old_fh);
  32.     1;
  33. }
  34. 1;
  35.  
Oct 21 '08 #13

Icecrack
Expert 100+
P: 174
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.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #test.pl
  3. use Socket;
  4. require "br_tcp.pl";
  5.  
  6. my $ser_nam;
  7.  
  8. # connect to server, F is the file handler, first argument is server name, second is port number
  9. if (open_TCP(F, $ARGV[0], $ARGV[1]) == undef) {
  10.   print "Error connecting to server at $ARGV[0] port $ARGV[1].\n";
  11.   exit(-1);
  12. }else {
  13.     $ser_nam = $ARGV[0];
  14. }
  15.  
  16. $req = <STDIN>;
  17. $hos = <STDIN>;
  18. print F "$req$hos\n\n";
  19. print $_ while (<F>);
  20.  
  21. close(F);
  22.  

try changing:
Expand|Select|Wrap|Line Numbers
  1. print $_ while (<F>);
to :
Expand|Select|Wrap|Line Numbers
  1. while (<F>)
  2. {
  3. print $_;
  4. }
Oct 21 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.