0

I'm using the following code to log into a server and go to a particular directory (where the logfile I want to search for a string resides). I have accomplished this with the Paramiko module (ssh), fairly straightforward. But the telnetlib module does not have many functions that I see to accomplish this. Does anyone know how I would open the file and search through it for a particular string (The server in question only allows Telnet logins - no ssh) ... Thanks:

import sys
import telnetlib





HOST = "10.28.46.14"
user = raw_input("Enter your username: ")
password = ""

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")

if password == "":
    tn.read_until("Password: ")
    tn.write(password + "\n")
    #print "Login successful!"
else:
    print "Your password is incorrect."


tn.write("cd /var/opt/mylog/log\n")
tn.write("ls\n")
tn.read_until("\n")
#tn.write("exit\n")

my_ls = tn.read_until("my.log")


print my_ls
suffa
  • 3,606
  • 8
  • 46
  • 66

1 Answers1

1

Did you check with the owner of the machine about ssh vs telnet? There aren't many operating systems that ship with telnet out of the box anymore, because telnet is subject to replay attacks.

What if you tell tn to do a grep? You might append an "echo $?" after the grep, to get an exit code - 0 means there was one or more matches, anything else means there weren't any.

user1277476
  • 2,871
  • 12
  • 10
  • Since it does not seem practical to search the file on the server, maybe there is someway to retrieve/download the file from the server to my pc using telnet? I think there is a bash command to transfer files, but I don't remember it straight away. – suffa Jul 30 '12 at 22:19
  • I came up with a solution that works: I just cat the log file on the command line, use tn.read_all function to capture the screen to a log file, and perform a search on the pc side. – suffa Aug 02 '12 at 14:57