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