Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Perl Net Applications: Telnet, FTP, E-Mail and SMTP, Study notes of Information Technology

An overview of common perl net applications including telnet, file transfer protocol (ftp), e-mail, and simple mail transfer protocol (smtp). It covers the functionality of each application, their respective commands, and examples using perl. Telnet is used for command line access to remote systems, ftp for file transfer, e-mail for exchanging messages, and smtp for sending e-mail messages.

Typology: Study notes

Pre 2010

Uploaded on 08/04/2009

koofers-user-4jf-2
koofers-user-4jf-2 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Common Perl Net:: Applications
Perl Net:: is one of the standard methods for IP application interface in Perl. It is more fully documented in the
book - Network Programming with Perl by Lincoln B. Stein.
Telnet
Telnet (Telecommunication network) is used for command line access to a remote system - UNIX, Windows,
router, switch etc - to execute system commands remotely on that system. Because of security issues with
Telnet (like no encryption), use of SSH has increased for remote access.
Telnet can open interactive raw-TCP sessions on any port for diagnosing network problems or manually "talk"
to other services as a simple way to send commands to the server and examine the responses. Other software
such as nc (netcat) on Unix (or PuTTY on Windows) are increasingly used for this as they can be called with
arguments not to send any terminal control handshaking data unlike standard Telnet client software
.
You can run telnet without parameters in order to enter the telnet command prompt (telnet>). From the Telnet
prompt, use the following commands to manage a computer running Telnet Client.
Telnet commands:
telnet [RemoteServer] [port#]
Without parameters, telnet provides a Telnet Client prompt:
Telnet prompt commands:
quit or q.
open or o [RemoteServer] [Port] Default port 23 is assumed.
close or c [RemoteServer]
.set [RemoteServer] [ntlm] [localecho] [term {ansi | vt100 | vt52 | vtnt}] [escape Character] [logfile FileName] [logging]
[bsasdel] [crlf] [delasbs] [mode {console | stream}] [?]
unset [Option]: To turn off an option that was previously set
To send Telnet Client commands
send [RemoteServer] [ao: Abort] [ayt: "Are you there?" ] [esc] [ip: Interrupts the process] [synch] [?]
Display: To view the current settings for the Telnet client
Net::Telnet
The new() method creates a new Net::Telnet object with a single argument containing the name of the host to
connect to, or with a series of option/value pairs that provide finer control over the session:
$telnet = Net::Telnet->new($host)
$telnet = Net::Telnet->new(Option1=>$value1,Option2=>$value2 ..)
Add’l Options: Host, Port (default 23), Timeout (10 seconds), Binmode (Suppress CRLF translation-
true/false), Cmd_remove_mode, Errmode ("die"), Input_log, Fhopen, Prompt (/[\$%#>] $/)
Exercise160 (Telnet example):
#!/usr/bin/perl
use strict;
use Net::Telnet;
use constant HOST => '("63.164.54.114)';
use constant USER => 'testuser';
use constant PASS => 'testuser;
my $telnet = Net::Telnet->new(HOST);
$telnet->login(USER,PASS);
my @lines = $telnet->cmd('ps -ef');
print @lines;
pf3
pf4
pf5

Partial preview of the text

Download Perl Net Applications: Telnet, FTP, E-Mail and SMTP and more Study notes Information Technology in PDF only on Docsity!

Common Perl Net:: Applications

Perl Net:: is one of the standard methods for IP application interface in Perl. It is more fully documented in the book - Network Programming with Perl by Lincoln B. Stein.

Telnet

Telnet ( Tel ecommunication net work) is used for command line access to a remote system - UNIX, Windows, router, switch etc - to execute system commands remotely on that system. Because of security issues with Telnet (like no encryption), use of SSH has increased for remote access. Telnet can open interactive raw-TCP sessions on any port for diagnosing network problems or manually "talk" to other services as a simple way to send commands to the server and examine the responses. Other software such as nc (netcat) on Unix (or PuTTY on Windows) are increasingly used for this as they can be called with arguments not to send any terminal control handshaking data unlike standard Telnet client software . You can run telnet without parameters in order to enter the telnet command prompt ( telnet> ). From the Telnet prompt, use the following commands to manage a computer running Telnet Client. Telnet commands: telnet [ RemoteServer ] [port#] Without parameters, telnet provides a Telnet Client prompt: Telnet prompt commands:  quit or q.  open or o [ RemoteServer ] [ Port ] Default port 23 is assumed.  close or c [R emoteServer ] . set [ RemoteServer ] [ ntlm ] [ localecho ] [ term { ansi | vt100 | vt52 | vtnt }] [ escape Character ] [ logfile FileName ] [ logging ] [ bsasdel ] [ crlf ] [ delasbs ] [ mode { console | stream }] [? ]  unset [ Option ]: To turn off an option that was previously set To send Telnet Client commands  send [R emoteServer ] [ ao: Abort] [ ayt: "Are you there?" ] [ esc ] [ ip: Interrupts the process] [ synch ] [? ]  Display: To view the current settings for the Telnet client Net::Telnet The new() method creates a new Net::Telnet object with a single argument containing the name of the host to connect to, or with a series of option/value pairs that provide finer control over the session: $telnet = Net::Telnet->new($host) $telnet = Net::Telnet->new(Option1=>$value1,Option2=>$value2 ..) Add’l Options: Host, Port (default 23 ), Timeout ( 10 seconds), Binmode (Suppress CRLF translation- true/false), Cmd_remove_mode, Errmode ("die"), Input_log, Fhopen, Prompt (/[$%#>] $/) Exercise160 (Telnet example): #!/usr/bin/perl use strict; use Net::Telnet; use constant HOST => '("63.164.54.114)'; use constant USER => 'testuser'; use constant PASS => 'testuser; my $telnet = Net::Telnet->new(HOST); $telnet->login(USER,PASS); my @lines = $telnet->cmd('ps -ef'); print @lines;

File Transfer Protocol ( FTP ) FTP is a file transfer protocol for exchanging and manipulating files over an TCP/IP. It’s precursor Unix-Unix Copy (UUCP) was used to transfer files between UNIX systems before IP became standard. An FTP client connects to an FTP server to manipulate files on that server; typically for uploading and downloading files. As with most server specific applications programs, a hostname and user id and password are needed to connnect to a server. FTP runs over TCP port 21 for incoming connections from FTP clients; referred to as the control session on which commands are passed to/from the FTP client to the FTP server. FTP uses a separate connection data exchange, TCP port 20 by default. For standard FTP, the process of setting up the data stream is port 21 for session control, port 20 for data. In active mode , the FTP client opens a dynamic port, sends the FTP server the dynamic port number using the sends a PORT command. When the FTP server initiates the data connection to the FTP client it binds the source port to port 20 on the FTP server. In passive mode , the FTP server opens a dynamic port, sends the FTP client the server's IP address to connect to using the PASV command and the port on which it is listening (a 16-bit value broken into a high and low byte, as explained above) over the control session and waits for a connection from the FTP client. Common FTP commands are: get (recv), put(send), ascii, binary (suppresses CRLF, code conversion), cwd, and quit. Net::FTP The new() method creates a Net::FTP object. The mandatory first argument is the domain name of the FTP server you wish to contact. $ftp = Net::FTP->new($host [,%options]) Additional options: Firewall (FTP proxy), BlockSize (default 10240), Port (default 21), Timeout (default 120 seconds), Debug (Set to greater than zero), Passive, Hash. Exercise161 (FTP example): #!/usr/bin/perl -w use Net::FTP; $ftp = Net::FTP->new("63.164.54.114") or die "Could not connect: $!";

$ftp->login('anonymous', 'me@foo.com'); # Guest User; email as passwd

$ftp->login('testuser', 'testuser'); # real account #$ftp->cwd('/'); # cwd:Change Working Directory $ftp->put('exercise160'); $ftp->quit();

Net::SMTP

The new() method creates a new Net::SMTP session object to a relay host. It is used for platform independent E-Mail transmission (Windows, UNIX, Mac OS). $smtp = Net::SMTP->new($host_relay); Commands are issues within the SMTP function call. The most commonly used options are: mail (‘from’), to, data (begins e-mail body), datasend (sends E-Mail body) datend (ends e-mail body) and quit. Below is an example: Exercise163: #!/usr/bin/perl -w use strict;

Function for sending mail with an MTA like sendmail

sub send_mail { my($to, $from, $subject, @body)=@_; use Net::SMTP;

You will need to change the following line

to your mail relay host

my $relay="acs.oakton.edu"; my $smtp = Net::SMTP->new($relay); die "Could not open connection: $!" if (! defined $smtp); $smtp->mail($from); $smtp->to($to); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); foreach(@body) { $smtp->datasend("$_\n"); } $smtp->dataend(); # Note the spelling: no "s" $smtp->quit; } my @body=("Is it $50 for an A or $100?", "Thanks"); send_mail('rjtaylor@oakton.edu', 'anonymous@csc.oakton.edu', 'Grade', @body);

Net::POP

The CPAN module Net::POP3 is a POP client that lets your Perl program act as an MUA. The new () Net::POP3 creates session object to the POP server passed as a parameter: $pop = Net::POP3->new( "pop.myisp.com" ) or die "Can't connect to pop.myisp.com: $!\n"; If an error occurs, $! may or may not contain a meaningful error message. Timeout is an optional arguments when establishing a session. $pop = Net::POP3->new( "pop.someisp.com", Timeout => 30 ) or die "POP session failed to pop.someisp.com: $!\n"; Authenticate yourself to the POP3 server with the login() method. It takes two arguments, username and password. defined ($pop->login("userid", "password")) or die "Username and password Login failed: $!\n"; Once authenticated, access the mail spool with list(), get(), and delete(). The list method gives you a list of undeleted messages in the spool. It returns a hash, where each key is a message number and each value is the size of the corresponding message in bytes: %undeleted = $pop->list(); foreach $msgnum (keys %undeleted) {print "Message $msgnum is $undeleted{$msgnum} bytes long.\n"; } To retrieve a message, call get() with the message number. It returns a reference an array of lines in the message: print "Retrieving $msgnum : "; $message = $pop->get($msgnum); if ($message) { # succeeded print "\n"; print @$message; # print the message } else { # failed print "failed ($!)\n"; } $pop->delete($msgid); The optional delete() method marks a message as deleted. When you call quit to terminate your POP3 session, the messages marked as deleted are removed from the mailbox. The reset method undoes any delete calls made during the session. If the session is terminated by the Net::POP3 object being destroyed because it went out of scope, the reset will be called automatically. Remember POP is a receive only mail client; there's no way to send mail from POP3, you have to use programs like mail or sendmail , or Net::SMTP.