python3.8.2 telnet library - cannot get telnet read output on screen - python

I am using the following code with python3.8.2 connecting to Cisco network devices (switch, router), running in an eveNG lab environment. I am connecting from a mac laptop, to a router using telnet on IP 10.9.249.3 and port 32769.
Problem is that when I run this code I do not get any output. I can however see the commands being run and the running config output on the device itself using a separate connection. The code execution just stays put waiting for an event.
from telnetlib import Telnet
HOST = "10.9.249.3"
tn = Telnet(HOST,32769)
tn.write(b"terminal length 0\n")
tn.write(b"show runn\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
tn.exit()
When I do a control-break I get the following output:
^CTraceback (most recent call last):
File "p1.py", line 12, in <module>
print(tn.read_all().decode('ascii'))
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/telnetlib.py", line 335, in read_all
self.fill_rawq()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/telnetlib.py", line 526, in fill_rawq
buf = self.sock.recv(50)
KeyboardInterrupt
I found similar issues posted and tried their suggestions like changing to a different Telnet read_*() method but no change in the output.
One thing I have noticed is that when I connect to the network device, I have to do the following to disconnect the (manual) telnet connection:
ctrl-]
telnet> quit
Connection closed.
Thanks and regards,
Abid Ghufran

Related

Unable to run program via port 443 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am running a program built on Python. When I am running the program on the Wintel server via port 80 I have no issues. Program name - TelegramBot-ASN.py.
Port 80
Running on http://<IP Address>:80/ (Press CTRL+C to quit)
However via port 443 encounter below issue
Traceback (most recent call last):
File "TelegramBot-ASN.py", line 49, in <module>
app.run(host=strHost, port=strPort, debug=True)
File "C:\RPA\Applications\Python\lib\site-packages\flask\app.py", line 990, in run
run_simple(host, port, self, **options)
File "C:\RPA\Applications\Python\lib\site-packages\werkzeug\serving.py", line 987, in run_simple
s.bind(server_address)
socket.gaierror: [Errno 11001] getaddrinfo failed
After studying the error a little, it is seen that the error was thrown while trying to bind the socket.
Usually, a common error in a bind is that the port to be used is already used.
But here is the getaddrinfo failed error.
If you are not given an IP for the host argument in the app.run method, give an IP. (internetwork IP).
Do not give domain names like localhost.
Example : app.run(host="192.168.1.102",
Also find out if 443 was taken by another process.
You can also find it by typing the following command in the powershell.
Get-Process -Id (Get-NetTCPConnection -LocalPort 443).OwningProcess
getaddrinfo failed means the hostname can't be resolved. Make sure the hostname you used is correct. Also check that there isn't another process listening on port 443.
To check which program is listening on port 443
netstat -ano -p tcp |find "443"
The command will return output similar to this
TCP 0.0.0.0:443 0.0.0.0:0 LISTENING 15096
To understand which is the program with that pid use the command(obviously use the pid that returned the previous command to you):
tasklist |find "15096"

Python & Mcpi (minecraft) - connection refused error

I use mcpi: https://github.com/AdventuresInMinecraft/AdventuresInMinecraft-Linux
Starting the local server.
After, run program:
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Hello Minecraft World")
I am facing the below error:
Traceback (most recent call last):
File "/home/home/AdventuresInMinecraft/MyAdventures/HelloMinecraftWorld.py", line 2, in mc = minecraft.Minecraft.create()
File "/home/home/.local/lib/python3.6/site-packages/mcpi/minecraft.py", line 376, in create return Minecraft(Connection(address, port))
File "/home/home/.local/lib/python3.6/site-packages/mcpi/connection.py", line 17, in init self.socket.connect((address, port))
ConnectionRefusedError: [Errno 111] Connection refused
A ConnectionRefusedError means that the address + port combination was unable to be secured for this particular Minecraft server and thus raised an exception. This could be because some other application is already using the port of interest, the port is unavailable because of the OS, or a handful of other networking configuration mishaps.
But perhaps a better series of questions to ask yourself is:
What is the default address and port that minecraft.Minecraft.create() will attempt to launch / listen at?
Do I have access to that server (address + port)?
If I do have access, are there any security issues (AKA Firewall)?
This post has already addressed the root issue of your question, and I hope it gives you a good start at understanding the foundation of your problem.
Notice how their question mentions s.connect((host,port)) and your stack trace has self.socket.connect((address, port)) Looks like the same thing to me!
Some more reading:
- localhost
- check if port is in use
I encountered the same issue. I looked into the code of mcpi and found that the default port is 4711. However, a Minecraft Server's default port is 25565. All you need to do is add 2 parameters on the create() function. Code(Python):
mc = minecraft.Minecraft.create(address="127.0.0.1", port=25565)
btw change "address" in the code to the host of the server (only if you modified the "server.properties" file).
Also, ConnectionRefusedError doesn't mean that it's not secured, I believe it means that either the server is not online, it doesn't exist, or the server refused it for some reason.
EDIT:
Oops sorry I just found out that mcpi actually connects to the RaspberryJam plugin which is hosted on another IP and port. The plugin runs on port 4711. So mcpi has the right port.
So check if you have the RaspberryJam plugin installed. If not, download it from
https://www.spigotmc.org/resources/raspberryjuice.22724/
And put the .jar file inside the plugins folder in your server directory.

How to communicate Python with Siemens PLC using Python-snap7?

I am trying to communicate Python with the CPU1212C PLC (using PLCSIM), but an error always occurs.
Code:
IP = '192.168.100.100'
RACK = 0
SLOT = 1
plc = snap7.client.Client()
plc.connect(IP, RACK, SLOT)
print(plc.get_cpu_state())
No handlers could be found for logger "snap7.common"
Traceback (most recent call last):
File "C:/Python27/Teste_Snap7.py", line 8, in plc.connect(IP, RACK, SLOT)
File "C:\Python27\lib\site-packages\snap7\client.py", line 25, in f check_error(code, context="client")
File "C:\Python27\lib\site-packages\snap7\common.py", line 65, in check_error raise Snap7Exception(error)
Snap7Exception: TCP : Connection timed out
Is this a problem with Windows10?
I am using Windows10 64 bit, Python 2.7.17, Snap7 1.1.0, Python-Snap7 0.10.
I copied and pasted the snap7.dll and snap7.lib file into the System32, Python27, Python27 / site-packages / snap7 folders. And I created for each folder a path in the environment variables in an attempt to work.
I followed this tutorial: https://www.youtube.com/watch?v=BKnK4AT_WKs
It's not a problem related with Windows, in the error message you can see the problem:
Snap7Exception: TCP : Connection timed out.
Verify your physical connection to the machine and then verify the client IP.
Verify:
if PLC actually ping
if Snap7 server is enabled on your PC
if rack and slot are correct, according to your PLC.
The handbook provided with snap7 is very exhaustive, please refer to it
Verify in tia portal:
properties > Protection and security > connection mechanism > and check the "permit acess with PUT/GET.."

Thrift TTransportException in python

I'm getting a weird error while trying to execute an RPC using thrift on python. I have found similar issues online, but none of them really apply to my situation.
Here is the error I'm getting
No handlers could be found for logger "thrift.transport.TSocket"
Traceback (most recent call last):
File "experiment.py", line 71, in <module>
transport.open()
File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TTransport.py", line 152, in open
return self.__trans.open()
File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TSocket.py", line 113, in open
raise TTransportException(TTransportException.NOT_OPEN, msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('192.168.178.44', 9000)]
The following is, I believe, the code which produces it.
TECS_SERVER_IP = "192.168.178.44"
TECS_SERVER_PORT = 9000
transport = TSocket.TSocket(TECS_SERVER_IP, TECS_SERVER_PORT)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = TTSService.Client(protocol)
transport.open()
This happens whenever I try to communicate to another machine, so I tried with the ip "127.0.0.1" and it works. However, using the IP of the localhost "192.168.178.44" which should refer to the same computer also produces the error.
To me it seems like it cannot resolve IP addresses for some reason...
Any ideas on what's causing this and how to fix it?
I'm using Python 2.7.12, thrift 0.9.3 and Ubuntu 16.04, but I also got the error on Windows 10.
This is how my thrift service starts
handler = TTSHandler()
handler.__init__()
transport = TSocket.TServerSocket(host='localhost', port=9000)
processor = TTSService.Processor(handler)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
server.serve()
your server should bind to that address before client could connect to:
TSocket.TServerSocket(host='192.168.178.44', port=9000)
or use host='0.0.0.0' which means bind on all IPv4 addresses on the machine.

Socket stops communicating

I'm running python 2.7 code on a Raspberry Pi that receives serial data from an Arduino, processes it, and sends it to a Windows box over a wifi link. The Pi is wired to a Linksys router running in client bridge mode and that router connects over wifi to another Linksys router to which the Windows box is wired. The code in the Pi runs fine for some (apparently) random interval, and then the Pi becomes unreachable from the Windows box.
I'm running PUTTY on the the Windows machine to connect to the Pi and when the fail occurs I get a message saying there's been a network error and the Pi is not reachable. Pinging the Pi from the Windows machine works fine until the error, at which time it produces "Reply from 192.168.0.129: Destination host unreachable." The client bridge router to which the Pi is connected remains reachable.
I've got the networking code on the Pi wrapped in an exception handler, and when it fails it shows the following:
Ethernet problem:
Traceback (most recent call last):
File "garage.py", line 108, in module
s.connect((host, port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 113] No route to host
None
The relevant python code looks like:
import socket
import traceback
host = '192.168.0.129'
port = 31415
in the setup, and after serial data has been processed:
try:
bline = strline.encode('utf-8')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(bline)
s.close()
except:
print "Ethernet problem: "
print traceback.print_exc()
Where strline contains the processed data. As I said, this runs fine for a few hours more or less before failing. Any ideas?
EDIT: When PUTTY fails its error message is "Network Error: Software caused connection abort."
EDIT: When the interface shuts down I see this in dmesg:
[Wed Nov 6 16:53:15 2013] smsc95xx 1-1.1:1.0: eth0: link down
[Wed Nov 6 16:53:17 2013] smsc95xx 1-1.1:1.0: eth0: link up, 100Mbps, full-duplex, lpa 0x45E1
at about 4 hours after the Pi boots up. This follows sys.exit() on the exception.

Categories