Geeks, please accept the hero post of 2021 Microsoft x Intel hacking contest>>>
Two problems encountered on March 14, 2017. The problem machine is a Windows Server, windows 2008 R2 standard 64 bit, Python 2.7.9
In fact, there are two problems mixed together. The first problem we encounter is:
No handlers could be found for logger "paramiko.transport"
This question is mixed with the second one below
The root of the problem is that the test code uses the logger of tornado. The root logger does not set the handlers, but sets the handlers (streamhandler) for the logger of ‘tornado. Application’. Therefore, the test code needs to be modified to set the default handler of root to nullhandler and the logger of ‘tornado. Application’ to streamhandler. Refer to test. Py in the sysmgt project( The essence is that the root logger does not set handlers, which causes other loggers to inherit it and have no handlers, and only the only set ‘tornado. Application’ logger can output normally.)
The second problem is that it still exists after the first problem is fixed. The error (simulation test) is as follows:
D:\temp\py>python paramiko_test.py
test paramiko ........
time1 : [0.0] - 10.99.201.174 - wcadmin
time1 : [0.0] - 10.122.2.6 - wcadmin
time2 : [0.0] - 10.99.201.174 - wcadmin
time2 : [0.0] - 10.122.2.6 - wcadmin
time3 : [0.0] - 10.99.201.174 - wcadmin
time3 : [0.0] - 10.122.2.6 - wcadmin
time4 : [0.0] - 10.99.201.174 - wcadmin -
time4 : [0.0] - 10.122.2.6 - wcadmin -
--- ip=10.99.201.174, username=wcadmin, password=
--- ip=10.122.2.6, username=wcadmin, password=
Traceback (most recent call last):
File "paramiko_test.py", line 42, in one_connection
sshclient.connect(ip, 22, username, password)
File "C:\app\Python27\lib\site-packages\paramiko\client.py", line 338, in connect
t.start_client(timeout=timeout)
File "C:\app\Python27\lib\site-packages\paramiko\transport.py", line 500, in start_client
raise e
RequirementParseError: Invalid requirement, parse error at "''"
time5 : [0.452000141144] - 10.99.201.174 - wcadmin
time6 : [0.46799993515] - 10.99.201.174 - wcadmin
The above is the test result of the test program (multi thread connection to the remote server). The same program has no problem on my own computer
Requirement parseError: invalid requirement this problem is not that every time you run the test program, you will make an error. The probability of error is 85%
Solution process:
1 suspected the problem of out of date PIP version, and finally uninstalled and installed it. Because the old version of PIP was not found, it was a new version before and after pip. It turned out that it had nothing to do with PIP
2. The version of paramiko is suspected. Upgrade paramiko from 1. * to 2 +. There are still problems
3. According to the similar problem on the Internet, that is to say, it’s a problem with the setup tools library. Uninstall it, then install it, and report that the module appdirs can’t be found. Download and install the module appdirs. Then install a new version of setup tools to solve the problem
The old version of setup tools is 22.0.0, while the new one is 34.3.2. My version is 16.0. So it seems that a version of setuptools conflicts with paramiko. Demote the version of setuptools on the server and solve the problem
On March 15, it was found that although the setup tools on the server were demoted, there was no problem in the test at that time, but in fact there was no in-depth test, and there was still the same problem after more threads were concurrent
So I uninstall and install the latest version of the setup tools on my computer, and the problem on the server is reproduced. When testing on Linux machines, there are also problems with the higher versions of setuptools. Next, I found an article about reducing the version of setuptools to 20.2. So the final version is 19.7, which has no problem after testing
I don’t know if the problem will recur. Let’s see
On March 15, I repeatedly installed and uninstalled setup tools and pip on the server side. I found some problems, but I didn’t understand them very well
1pkg_ The resources module, for reference, should be used to load the module. After Python is installed, it appears in $python_ When installing the latest PIP and setup tools in home/lib/site packages, we sometimes find that PKG in site packages_ Resources is no longer available, but appears in setuptools
2. After installing and uninstalling PIP setuptools for many times, we found that pip.exe and pip-script.py were missing, which made PIP unusable. Because we operated for many times, we didn’t know which time this problem occurred, so we had to use these two files on other machines. I don’t know if other files are missing
To solve the second problem, find a way on the Internet and execute the following command:
python -m pip install --upgrade pip --force-reinstall
This article was published in open source China. At that time, I thought that the problem had been solved, but the problem still existed the next day. As a result, I continued to search on the Internet and found that this article had been reprinted by a website. I estimated that it was automatically published through web crawler, so I updated it today, and I’ll see if it will be crawled in a few days
My test program:
#-*-coding:UTF-8-*-
__author__='zhaoxp'
import traceback
import logging
import time
import threading
import paramiko
logging.basicConfig(level=logging.DEBUG)
logging.getLogger().handlers=[logging.NullHandler()]
logger = logging.getLogger('paramiko.test')
logger.handlers.append(logging.StreamHandler())
logger.setLevel(logging.DEBUG)
def main():
threads = []
threads.append(threading.Thread(target=one_connection, args=('10.99.201.174', 'wcadmin', 'password')))
threads.append(threading.Thread(target=one_connection, args=('10.122.2.6', 'wcadmin', 'password')))
threads.append(threading.Thread(target=one_connection, args=('10.99.201.37', 'wcadmin', 'password')))
threads.append(threading.Thread(target=one_connection, args=('10.99.244.121', 'wcadmin', 'password')))
for th in threads:
th.start()
for th in threads:
th.join()
def one_connection(ip, username, password):
sshclient = None
try:
start_time = time.time()
logger.debug('time1 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
sshclient=paramiko.SSHClient()
logger.debug('time2 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
sshclient.load_system_host_keys()
logger.debug('time3 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
logger.debug('time4 : [%s] - %s - %s - %s'%(time.time()-start_time, ip, username, password))
logger.debug('--- ip=%s, username=%s, password=%s'%(ip, username, password))
sshclient.connect(ip, 22, username, password)
logger.debug('time5 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
stdin, stdout, stderr = sshclient.exec_command('pwd')
#logger.debug('stdout:\n%s \n stderr:\n%s \n'%(stdout.read(), stderr.read()))
logger.debug('time6 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
except BaseException as be:
traceback.print_exc()
finally:
if sshclient is not None:
sshclient.close()
if __name__=='__main__':
print 'test paramiko ........'
main()