Script for Telnetting with PERL and Python

75 logo Script for Telnetting with PERL and PythonLets just say that PERL made me a crazy programmer. Then came Python, it made me an insane one. In my previous job, it was all PERL, PERL and PERL. But this current job has made me pick up Python.

Python was picked up because the STAF/STAX automation work required scripts and Python was the candidate. So one fine day i found myself writing this -

python logo Script for Telnetting with PERL and Python#!/usr/bin/python
print "Hello world"

instead of this -

#!/usr/bin/perl
print "Hello World";

Then i actually looked back and thought, what was the first real purpose for which i wrote a PERL script – telnetting. Even today i find that telnetting to workstations to retrieve stats, is one thing that i do a zillions times day. The usual telnet script with PERL/Python has to be used along with the Expect module. That guarantees that the session time-outs and failure scenarios are properly taken care of.

But there is one place where you can just avoid Expect module all together. Its when you use the script within the same network and you are 70% sure that the pain-in-the-butt scenario rarely happens. So here it is a minimal telnet script that i have made in both PERL and Python.

PERL

#!/usr/bin/perl
use Telnet;
$host = "192.168.1.100";
$port = "2300";
$uid = "jerry";
$pwd = "password";
open $inputLog,  ">in.log";
$box = new Net::Telnet();
$box->open(     Host => $host,
                Port => $port,
        );
$iLog = $box->input_log($inputLog);
$flag = $box->login(    Name => $uid,
                        Password => $pwd,
                );
$box->print("show log");
$box->waitfor('/# $/i');
$box->close;
exit

PYTHON

#!/usr/bin/python
import os, telnetlib
HOST = "192.168.1.100"
PORT = "2300"
user = "jerry"
password = "password"
f = open(sLog, "in.log")
tn = telnetlib.Telnet(HOST, PORT)
tn.read_until("login: ")
tn.write(uid + "\n")
tn.read_until("Password: ")
tn.write(pwd + "\n")
tn.read_until("]#")
tn.write("show log\n")
f.write(tn.read_until("]#"))
f.close()
tn.write("exit\n")
tn.read_until("#")
tn.close()

Happy scripting. icon smile Script for Telnetting with PERL and Python