0

note before starting - This issue is different than: 'NoneType' object has no attribute 'sendall' PYTHON IP addresses removed and replaced with x.x.x.x

It appears that my code is sending the desired command, but then errors out with the 'Nonetype' line as seen in my debug output below: "line 83 in use Attempting to find IP of vlan 10
line 85 Telnet(x.x.x.x ,23): send b'show interface vlan 10 \n' 'NoneType' object has no attribute 'sendall' False script complete"

Wireshark is showing that the telnet connection is reset when sending that command. I'm not seeing anything in the Telnetlib documentation or anything on google that seems to be similar to the issue i'm having: 56054 6494.389198219 x.x.x.x x.x.x.x TCP 56 51110 → 23 [RST, ACK] Seq=86 Ack=3288 Win=63634 Len=0

Has anyone had a similar issue here? I was thinking maybe i'd have to try and keep the session alive by arbitrarily adding write/read commands, but that doesn't seem to work.

Thanks!

Code added below:

#!/usr/bin/env python3

from time import sleep
import telnetlib
from getpass import getpass





# f is the .txt document that lists the IP's we'll be using.
f = open("devicess.txt")
#
username = input("please provide your username:")
sleep(.25)
password = getpass()

#
for line in f:
    device = (line)
    print('Starting to collect information, please wait')

#For those devices in the above list, connect and run the below commands
    def loopstart():
        for device in f:
            tn = telnetlib.Telnet()
            tn.open(device, 23, 60)
            #Remove # in the line below for debug
            tn.set_debuglevel(2000)
            tn.read_until(b"Username:", timeout = 20)
            sleep(.25)
            tn.write(str(username + "\n").encode("ascii"))
            sleep(.25)
            tn.read_until(b"Password: ", timeout = 10)
            sleep(.25)
            tn.write((password + "\n").encode("ascii"))
            sleep(.25)
            #####################################
            #Verify Login attempt below         #
            #####################################
            try:
                enablemode = tn.read_until(b"#")
                if (b"FAIL") in enablemode:
                    print("Bad credentials to " + device)
                    tn.close()
                    sleep(.25)
                elif (b"fail") in enablemode:
                    print("Bad credentials to " + device)
                    tn.close()
                    sleep(.25)
                elif (b"#") in enablemode:
                    print("connection established to " + device)
                    try:
                        tn.write(str("show mac address-table | include 000c.\n").encode('ascii'))
                        sleep(1)
                        MH2 = tn.read_very_eager() 
                        if (b"000c.15") in MH2:
                            print("possible Cyper power device identified")
                            try:
                                mactable = open("mactable.txt", "w+")
                                mactable.seek(0)
                                mactable.write(MH2.decode('utf-8'))
                                mactable.truncate()
                                mactable.seek(0)
                                Output1 = mactable.readlines()
                                for line in Output1:
                                    line = line.strip()
                                    CPMAC = line
                                    print(CPMAC)
                                    try:
                                        sleep(.10)
                                        if ("000c.15") in CPMAC:
                                            vlan = (CPMAC[0:4])
                                            print("line 83 in use")
                                            print(type(vlan))
                                            print("Attempting to find IP of vlan " + vlan)
                                            print("line 85")
                                            tn.write(("show interface vlan " + vlan + "\n").encode("ascii"))
                                            print("line 87")
                                            tn.read_until(b"Internet Address")
                                            tn.close()
                                        elif (str("All")) and (str("CPU")) in (CPMAC):
                                            print ("CPU has matching MAC, checking rest of the output for this device")
                                            tn.close()
                                        else:
                                            print("Moving to next device")
                                            tn.close()
                                    except EOFError as e:
                                        print("could not pull vlan from output")
                            except EOFError as e:
                                print("unidentified issue")
                #Execute the following commands in case of invalid command input
                        elif (b"Invalid") in MH2:
                            sleep(.5)
                            try:
                                tn.write(str("show mac-address-table | in 000c.\n").encode('ascii'))
                                sleep(2)
                                MH3 = tn.read_very_eager()
                                if (b"000c.15") in MH3:
                                    print("Line 90 in use")
                                    try:
                                        sleep(.5)
                                        mactable = open("mactable.txt", "rb+")
                                        mactable.seek(0)
                                        mactable.write(MH3)
                                        Output2 = (bytes(mactable.read()))
                                        print (type(Output2))
                                        mactable.truncate()
                                        for line in Output2():
                                            CPMAC = line.decode
                                            try:  
                                                if ("000c.15") in CPMAC:
                                                    print("line 114")
                                                    print(CPMAC + " this is what vlan the cyber power device should be on")
                                                    tn.write("show interface vlan" + (CPMAC[:6])+ "\n")
                                                    tn.read_until(b"Internet Address")
                                                    tn.close()
                                                elif (str("All")) in (CPMAC):
                                                    print ("CPU has matching MAC, moving to next device")
                                                    tn.close()
                                                else:
                                                    print("No Cyber power device found on " + device)
                                                    tn.close()
                                            except EOFError as e:
                                                print("could not pull vlan from output")
                                    except EOFError as e:
                                        print("unidentified issue")     
                                elif (b"000c.15") not in MH3:
                                    print ("Cyber power device not found, moving to next device.")
                                    tn.close()
                                else:
                                    print("Unknown Error")
                                    tn.close()

    ##############################
    #        Logout commands     #
    ##############################
                            except EOFError as e:
                                print("Connection closed to " + device)
                        else:
                            tn.write(str("exit\n").encode('ascii'))
                            tn.write(str("exit\n").encode('ascii'))
                            tn.close()
                            print(tn.read_all().decode('ascii'))
                    except EOFError as e:
                        print ("unknown error")
                else:
                    tn.close()
            except EOFError as e:
                print("Connection closed to " + device)
            except Exception as exception:
                print('line 165 error')
                print(exception, False)
                tn.close()
    loopstart()
print('script complete') 

Edit: I added a print statement to the "except exception as exception" which is showing the following when run. Invalid file object: False

  • We cannot tell you what is wrong with the code, unless we can see the code. – Karl Knechtel Oct 23 '19 at 19:03
  • I added the code, let me know what you think. My hunch is that when I go to call on mactable.txt its not sending or receiving byte information from the device i'm telnetting to and so the connection times out or something. I tried changing the timeout to 60 seconds as seen in the code and also adding some tn.write commands to keep the connection alive which gets rid of the error, but then doesn't seem to send my show int van 10 command. This just is compounding my confusion because I know the command works as it tries to send it before making that change.... – andrew lee Oct 24 '19 at 11:02
  • Could this be useful? - https://stackoverflow.com/questions/12421799/how-to-disable-telnet-echo-in-python-telnetlib - I'm wondering if I need to send some sort of keepalive to the device on the other end of my telnet session. I don't honestly know enough about telnet to know which combinations of commands to send. will try a combination and see if I have any success. – andrew lee Oct 24 '19 at 13:40
  • I think I found the solution here: stackoverflow.com/questions/8480766/detect-a-closed-connection-in-pythons-telnetlib/42124099 will work on trying to find a solution that fits my particular example but so far I've not had any luck so additional assistance would be appreciated. – andrew lee Oct 25 '19 at 18:11
  • The above seems to be a dead end. I found in wireshark that the RST was an anomaly, I do see that my machine is sending FIN ACK and closing the connection which isn't what I want it to do. – andrew lee Oct 30 '19 at 13:43

0 Answers0