You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
connect() fails once with invalid hosts file, then works #625
Can't think offhand why this would be happening, there's probably some state in SSHClient which is getting filled in prior to the IOError line which is causing the behavior to (right or wrong) be non-idempotent. Needs someone to poke.
The known_hosts file does not exist, so the initial load raises an exception, and the SSHClient has an empty KnownHosts object. When the key is added by the AutoAddPolicy, it initially triggers a save call for the file, which attempts to reload the known_hosts file contents, just in case it was externally changed since it was loaded (https://github.com/paramiko/paramiko/blob/master/paramiko/client.py#L137). Since the file didn't exist at original load time (and still doesn't exist), an exception is raised here as well, bypassing the save code that would normally rewrite/create the file.
The second time the connect() call is made, the host entry is in the HostEntry object (just not saved to disk) so it is found, and not re-added, which would trigger a call to save_host_keys(). The connect succeeds on subsequent calls once the host key is present in the HostEntry memory dict.
Not sure what the most appropriate handling of this ought to be: Intiial load() call could create an empty file if it doesn't already exist although it might be better to defer that until save_host_keys() is called anyway. Potentially more complicated if the path to the file requires parent directories to be created. Might also make sense to translate AutoAdd/AutoSave exceptions to warnings(?).
SSHClient caches known_hosts data in memory in addition to persisting to disk
updates to the cache trigger disk write
disk write performs disk read to ensure no OOB updates are stomped
thus, the first call to connect() for a given host in a given session will except (given an invalid/nonexistent known hosts file setting), but subsequent calls will not (as the cache is now warm)
What I think ought to happen:
First, if the file does not exist, it should be created; this is what the OpenSSH client does. Doing so early enough would prevent most instances of this issue.
In the sub-case where the file cannot be created (due to missing intermediate directories, or permissions issues, etc) OpenSSH emits a warning to stderr (Failed to add the host to the list of known hosts (/path/to/file)) but continues operation.
I reckon us doing sys.stderr.write() would be reasonable here too, though a log entry or using the warning module are other options.
Activity
bitprophet commentedon Dec 2, 2015
Can't think offhand why this would be happening, there's probably some state in
SSHClientwhich is getting filled in prior to theIOErrorline which is causing the behavior to (right or wrong) be non-idempotent. Needs someone to poke.zsjohny commentedon Jan 28, 2016
@bitprophet same to me
radssh commentedon Jan 28, 2016
Poke results:
The known_hosts file does not exist, so the initial load raises an exception, and the SSHClient has an empty KnownHosts object. When the key is added by the AutoAddPolicy, it initially triggers a save call for the file, which attempts to reload the known_hosts file contents, just in case it was externally changed since it was loaded (https://github.com/paramiko/paramiko/blob/master/paramiko/client.py#L137). Since the file didn't exist at original load time (and still doesn't exist), an exception is raised here as well, bypassing the save code that would normally rewrite/create the file.
The second time the connect() call is made, the host entry is in the HostEntry object (just not saved to disk) so it is found, and not re-added, which would trigger a call to save_host_keys(). The connect succeeds on subsequent calls once the host key is present in the HostEntry memory dict.
Not sure what the most appropriate handling of this ought to be: Intiial load() call could create an empty file if it doesn't already exist although it might be better to defer that until save_host_keys() is called anyway. Potentially more complicated if the path to the file requires parent directories to be created. Might also make sense to translate AutoAdd/AutoSave exceptions to warnings(?).
Fixes fatal error when missing known_hosts
bitprophet commentedon Oct 28, 2016
To rephrase/summarize the previous comment:
What I think ought to happen:
Failed to add the host to the list of known hosts (/path/to/file)) but continues operation.sys.stderr.write()would be reasonable here too, though a log entry or using thewarningmodule are other options.