pydbus reconnection after target is rebooted - python

I'm facing the following problem.
I'm using pydbus python package to test API by connecting to a target linux machine which is in the same network by using the environment variable DBUS_SYSTEM_BUS_ADDRESS
Under normal circumstances this works well, I'm able to call any valid API over the proxy object created.
Eg:
import os
import pydbus
os.environ['DBUS_SYSTEM_BUS_ADDRESS'] = \
"tcp:host=192.168.1.100,port=55556,family=ipv4"
bus = pydbus.SystemBus()
proxy = bus.get("busname", "object_path")
return_value = proxy.method-name(args)
As part of my testing, my target linux machine gets rebooted in between and when I re-use the proxy object, I get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/harman/.local/lib/python2.7/site-packages/pydbus/proxy.py", line 47, in get
0, timeout_to_glib(timeout), None)
GLib.Error: g-io-error-quark: The connection is closed (18)
I realize that the previous connection no longer exists, so I tried re-initialising the connection with the following
bus = pydbus.SystemBus()
proxy = bus.get("busname", "object_path")
I get the same error on the bus.get() statement.
Is there a solution/possible workaround to this problem?

Related

connect_kwargs not attribute of open

I wanted to connect to a server, but it gave me an error that I need a method of SSH athentication, so I simply made a key-value pair with "open()" and "connect_kwargs".
As I specified earlier this is my code:
from fabric import Connection
import socket
uinaaaa = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
uinaaaa.connect(("8.8.8.8", 80))
xin = uinaaaa.getsockname()[0]
PERSONA = Connection(f"user#{xin}")
PERSONA.open(connect_kwargs={'password': "LOSER"})
print("Ok, now you will learn all the server's commands!")
YUIA = PERSONA.run("help")
print(YUIA.standout)
print("If you want to learn more, type man, after that, a space, and then the command to learn more!")
but I got:
Traceback (most recent call last):
File "main.py", line 7, in <module>
PERSONA.open(connect_kwargs={'password': "LOSER"})
TypeError: open() got an unexpected keyword argument 'connect_kwargs'
I don't know how to fix this. Is it an entirely differnet function then "open()" or just a different argument than connect_kwargs? Please help!

Python/P2P - Unable to connect to rendezvous server

I am trying to create a P2P node using python (pyp2p) but I am getting this error:
Eamons-MacBook-Pro:blockchain eamonwhite$ python3 serveralice.py
HTTP Error 404: Not Found
HTTP Error 404: Not Found
HTTP Error 404: Not Found
HTTP Error 404: Not Found
Traceback (most recent call last):
File "/Users/eamonwhite/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pyp2p/net.py", line 732, in start
rendezvous_con = self.rendezvous.server_connect()
File "/Users/eamonwhite/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pyp2p/rendezvous_client.py", line 92, in server_connect
con.connect(server["addr"], server["port"])
File "/Users/eamonwhite/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pyp2p/sock.py", line 189, in connect
self.s.bind((src_ip, 0))
TypeError: str, bytes or bytearray expected, not NoneType
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "serveralice.py", line 10, in <module>
alice.start()
File "/Users/eamonwhite/.pyenv/versions/3.6.3/lib/python3.6/site-packages/pyp2p/net.py", line 735, in start
raise Exception("Unable to connect to rendezvous server.")
Exception: Unable to connect to rendezvous server.
My relevant code looks like this:
from uuid import uuid4
from blockchain import Blockchain
from flask import Flask, jsonify, request
from pyp2p.net import *
import time
#Setup Alice's p2p node.
alice = Net(passive_bind="192.168.1.131", passive_port=44444, interface="en0", node_type="passive", debug=1)
alice.start()
alice.bootstrap()
alice.advertise()
while 1:
for con in alice:
for reply in con:
print(reply)
time.sleep(1)
...
It is getting stuck on the Net function right at the beginning - something to do with the rendezvous package. The IP is my IP on the my network, and I port forwarded 44444 although I'm not sure if I need to do that or not. Thanks.
I am new to this, apparently with the way the server code was configured, it needed a rendezvous server to work (a node that handles all the other nodes). It is in net.py of the pyp2p package:
# Bootstrapping + TCP hole punching server.
rendezvous_servers = [
{
"addr": "162.243.213.95",
"port": 8000
}
]
The address was the problem, obviously it is just a placeholder IP. So then I realized I needed my own rendezvous server, and I used this code - https://raw.githubusercontent.com/StorjOld/pyp2p/master/pyp2p/rendezvous_server.py.
However I had to debug this file a little, it ended up needing to have import sys, import time and import re statements at the top before it would work. Now I am going to host it on my raspberry pi so that it is always up to handle nodes :)

Python Pysftp Error

My code:
import pysftp
s = pysftp.Connection(host='test.rebex.net', username='demo', password='password')
data = s.listdir()
s.close()
for i in data:
print i
I'm getting an error trying to connect to a SFTP server using pysftp.
This should be straight forward enough but I get the error below:
Traceback (most recent call last):
File "/Users/gavinhinfey/Documents/Python Files/sftp_test.py", line 3, in <module>
s = pysftp.Connection(host='test.rebex.net', username='demo', password='password')
File "build/bdist.macosx-10.6-intel/egg/pysftp.py", line 55, in __init__
File "build/bdist.macosx-10.5-intel/egg/paramiko/transport.py", line 303, in __init__
paramiko.SSHException: Unable to connect to test.rebex.net: [Errno 60] Operation timed out
Exception AttributeError: "'Connection' object has no attribute '_tranport_live'" in <bound method Connection.__del__ of <pysftp.Connection object at 0x101a5a810>> ignored
I've tried using different versions of python (mostly 2.7), I have all dependencies installed and I tried numerous sftp connections.
I'm using OS X 10.9.1.
updating the package didn't work for me, as it was already up-to-date (latest for python 2.7 at least)
Found a better aproach here.
1) You can manualy add the ssh key to the known_hosts file
ssh test.rebex.net
2) Or you can set a flag to ignore it
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None # disable host key checking.
with pysftp.Connection('host', username='me',private_key=private_key,
private_key_pass=private_key_password,
cnopts=cnopts) as sftp
# do stuff here
That initial error appears to be a problem connecting with the remote server (SSHException). The second (AttributeError), is from a bug in the code that occurs when the connection fails. It is fixed in the latest version of pysftp
https://pypi.python.org/pypi/pysftp
pip install -U pysftp
is your friend.
#Martin.Prikryl: setting hostkeys = None is very useful in the initial stage of coding with pysftp. Debugging a program that keeps failing for a known exception hides other problems that need attention--like making an actual connection. I can deal with the 'man in the middle' problem later once I know my code is actually working correctly.
#All: The current pysftp.CnOpts() object appears to have a bug:
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
The above code does not prevent host key checking.
python getfile_v3.py --help
Traceback (most recent call last):
File "getfile_v3.py", line 9, in
cnopts = pysftp.CnOpts()
File "c:\Program Files\Python\Python38\lib\site-packages\pysftp_init_.py", line 64, in init
raise HostKeysException('No Host Keys Found')
pysftp.exceptions.HostKeysException: No Host Keys Found
The second line doesn't get executed because the first does the host key check by default. If I set the key with:
cnopts = pysftp.CnOpts(hostkeys=None)
the same error results.
It appears that 'hostkeys' has been deprecated, and there is no way to disable the host key check.
Joe White
Workaround for SSHException: No hostkey for host test.rebex.net found
just to add it manually via ssh
ssh demo#test.rebex.net
it will ask for password, you need to enter "password"
it will show you the message
The authenticity of host 'test.rebex.net (ip-adress)' can't be established.
ECDSA key fingerprint is SHA256:OzvpQxxxV9F/ECMXbQ7B7zbKxxxxUno65c.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
confirm action -> type "yes"
done, you will get a message, since now your sftp connection to 'test.rebex.net' should work
Warning: Permanently added 'test.rebex.net,ip' (ECDSA) to the list of known hosts.

TorCtl connection refused error

While browsing about changing identity with tor I have the following script:
from TorCtl import TorCtl
conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123")
TorCtl.Connection.send_signal(conn, "NEWNYM")
But I get this error:
Connection refused. Is the ControlPort enabled?
Traceback (most recent call last):
File "python_tor.py", line 18, in <module>
TorCtl.Connection.send_signal(conn, "NEWNYM")
TypeError: unbound method send_signal() must be called with Connection instance as first argument (got NoneType instance instead)
And What shoul be the passphrase? I have tried without passphrase the same error occurs.
I think you should do
conn.send_signal("NEWNYM")
You could try printing conn in between the calls. See if it's None. Maybe the connection failed.
Look at vivaldia settings, in the advanced tab, look at the address and the port number below controlport, it may be 9151 instead of 9050. Then set another password (uncheck the box random password).

Python Telnetlib and connecting timeout

I am trying to make a script, which connects to a server by telnet. I am using python and telnetlib. I have problems getting timeout to work. I want to use the optional timeout when connecting to host, so that I get an exception if the host isn't online.
I have read the python telnetlib documentation but I have no idea what's wrong with my code.
Here is my simple code:
import telnetlib
host = 'hostname'
tn = telnetlib.Telnet(host, 23, 5) # port 23, timeout 45secs
print 'Connecting to', host
tn.close()
And here is my error message:
Exception exceptions.AttributeError: "Telnet instance has no attribute 'sock'" in <bound method Telnet.__del__ of <telnetlib.Telnet instance at 0x7f269864b8c0>> ignored
Traceback (most recent call last):
File "test2.py", line 7, in <module>
tn = telnetlib.Telnet(host, 23, 5)
TypeError: __init__() takes at most 3 arguments (4 given)
You probably aren't using python 2.6 or above. Quote from the docs:
Changed in version 2.6: timeout was added.

Categories

Resources