I have 3 source code files (main and imported from it).
test.py - just import file
import test_child
test_child.py - call function defined in imported file
import test_grandchild
test_grandchild.check_sshd()
test_grandchild.py - define function
import paramiko
def check_sshd():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print('checking ssh connection to local host')
client.connect('127.0.0.1')
client.close()
print('finished')
check_sshd()
is a function just for checking sshd is running on localhost. So, I expect that authentication exception occurs when sshd is not running, otherwise there wold be a socket error.
When I run test_child.py, the result matches my expectation. However, the paramiko client gets stuck on connect()
and I have to terminate the process.
$ python test.py
checking ssh connection to local host
^CTraceback (most recent call last):
File "test.py", line 4, in <module>
import test_child
File "/root/tests/test_child.py", line 6, in <module>
test_grandchild.check_sshd()
File "/root/tests/test_grandchild.py", line 10, in check_sshd
client.connect('127.0.0.1')
File "build/bdist.linux-x86_64/egg/paramiko/client.py", line 242, in connect
File "build/bdist.linux-x86_64/egg/paramiko/transport.py", line 342, in start_client
File "/usr/lib64/python2.7/threading.py", line 621, in wait
self.__cond.wait(timeout, balancing)
File "/usr/lib64/python2.7/threading.py", line 361, in wait
_sleep(delay)
KeyboardInterrupt
how can i solve this problem?
Paramiko 1.12.4 on CentOS 7
Thanks.
Paramiko uses Threading library to distribute tasks to different threads, and event is set then they communicate with each other and return output. The problem here is in test.py you are just importing the test_child not calling any of its function specifically.
So the execution point to call check_sshd() is test_child.py not test.py. Hence threading event is not set. So for this either you call function from same file or you need to specifically call function from test.py rather than just importing file.
test.py
import test_child
test_child.check_ssh_connection()
test_child.py
import test_grandchild
def check_ssh_connection():
test_grandchild.check_sshd()
test_grandchild.py
import paramiko
def check_sshd():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print('checking ssh connection to local host')
client.connect('127.0.0.1')
client.close()
print('finished')
Hopefully this would solve your problem.