summaryrefslogtreecommitdiffhomepage
path: root/paramiko/transport.py
diff options
context:
space:
mode:
Diffstat (limited to 'paramiko/transport.py')
-rw-r--r--paramiko/transport.py112
1 files changed, 60 insertions, 52 deletions
diff --git a/paramiko/transport.py b/paramiko/transport.py
index c2ef4fcc..ddcb2912 100644
--- a/paramiko/transport.py
+++ b/paramiko/transport.py
@@ -100,7 +100,7 @@ class Transport(threading.Thread, ClosingContextManager):
_DECRYPT = object()
_PROTO_ID = '2.0'
- _CLIENT_ID = 'paramiko_%s' % paramiko.__version__
+ _CLIENT_ID = 'paramiko_{}'.format(paramiko.__version__)
# These tuples of algorithm identifiers are in preference order; do not
# reorder without reason!
@@ -329,7 +329,7 @@ class Transport(threading.Thread, ClosingContextManager):
break
else:
raise SSHException(
- 'Unable to connect to %s: %s' % (hostname, reason))
+ 'Unable to connect to {}: {}'.format(hostname, reason))
# okay, normal socket-ish flow here...
threading.Thread.__init__(self)
self.setDaemon(True)
@@ -415,17 +415,19 @@ class Transport(threading.Thread, ClosingContextManager):
"""
Returns a string representation of this object, for debugging.
"""
- out = '<paramiko.Transport at %s' % hex(long(id(self)) & xffffffff)
+ id_ = hex(long(id(self)) & xffffffff)
+ out = '<paramiko.Transport at {}'.format(id_)
if not self.active:
out += ' (unconnected)'
else:
if self.local_cipher != '':
- out += ' (cipher %s, %d bits)' % (
+ out += ' (cipher {}, {:d} bits)'.format(
self.local_cipher,
self._cipher_info[self.local_cipher]['key-size'] * 8
)
if self.is_authenticated():
- out += ' (active; %d open channel(s))' % len(self._channels)
+ out += ' (active; {} open channel(s))'.format(
+ len(self._channels))
elif self.initial_kex_done:
out += ' (connected; awaiting auth)'
else:
@@ -1061,7 +1063,7 @@ class Transport(threading.Thread, ClosingContextManager):
m.add_boolean(wait)
if data is not None:
m.add(*data)
- self._log(DEBUG, 'Sending global request "%s"' % kind)
+ self._log(DEBUG, 'Sending global request "{}"'.format(kind))
self._send_user_message(m)
if not wait:
return None
@@ -1179,14 +1181,15 @@ class Transport(threading.Thread, ClosingContextManager):
key.asbytes() != hostkey.asbytes()
):
self._log(DEBUG, 'Bad host key from server')
- self._log(DEBUG, 'Expected: %s: %s' % (
- hostkey.get_name(), repr(hostkey.asbytes()))
- )
- self._log(DEBUG, 'Got : %s: %s' % (
- key.get_name(), repr(key.asbytes()))
- )
+ self._log(DEBUG, 'Expected: {}: {}'.format(
+ hostkey.get_name(), repr(hostkey.asbytes()),
+ ))
+ self._log(DEBUG, 'Got : {}: {}'.format(
+ key.get_name(), repr(key.asbytes()),
+ ))
raise SSHException('Bad host key from server')
- self._log(DEBUG, 'Host key verified (%s)' % hostkey.get_name())
+ self._log(DEBUG, 'Host key verified ({})'.format(
+ hostkey.get_name()))
if (pkey is not None) or (password is not None) or gss_auth or gss_kex:
if gss_auth:
@@ -1295,7 +1298,7 @@ class Transport(threading.Thread, ClosingContextManager):
:param str username: the username to authenticate as
:return:
- `list` of auth types permissible for the next stage of
+ list of auth types permissible for the next stage of
authentication (normally empty)
:raises:
@@ -1350,7 +1353,7 @@ class Transport(threading.Thread, ClosingContextManager):
``True`` if an attempt at an automated "interactive" password auth
should be made if the server doesn't support normal password auth
:return:
- `list` of auth types permissible for the next stage of
+ list of auth types permissible for the next stage of
authentication (normally empty)
:raises:
@@ -1421,7 +1424,7 @@ class Transport(threading.Thread, ClosingContextManager):
an event to trigger when the authentication attempt is complete
(whether it was successful or not)
:return:
- `list` of auth types permissible for the next stage of
+ list of auth types permissible for the next stage of
authentication (normally empty)
:raises:
@@ -1479,7 +1482,7 @@ class Transport(threading.Thread, ClosingContextManager):
:param callable handler: a handler for responding to server questions
:param str submethods: a string list of desired submethods (optional)
:return:
- `list` of auth types permissible for the next stage of
+ list of auth types permissible for the next stage of
authentication (normally empty).
:raises: `.BadAuthenticationType` -- if public-key authentication isn't
@@ -1529,7 +1532,6 @@ class Transport(threading.Thread, ClosingContextManager):
:param bool gss_deleg_creds: Delegate credentials or not
:return: list of auth types permissible for the next stage of
authentication (normally empty)
- :rtype: list
:raises: `.BadAuthenticationType` -- if gssapi-with-mic isn't
allowed by the server (and no event was passed in)
:raises:
@@ -1553,7 +1555,7 @@ class Transport(threading.Thread, ClosingContextManager):
:param str username: The username to authenticate as.
:returns:
- a `list` of auth types permissible for the next stage of
+ a list of auth types permissible for the next stage of
authentication (normally empty)
:raises: `.BadAuthenticationType` --
if GSS-API Key Exchange was not performed (and no event was passed
@@ -1746,7 +1748,7 @@ class Transport(threading.Thread, ClosingContextManager):
if key is None:
raise SSHException('Unknown host key type')
if not key.verify_ssh_sig(self.H, Message(sig)):
- raise SSHException('Signature verification (%s) failed.' % self.host_key_type) # noqa
+ raise SSHException('Signature verification ({}) failed.'.format(self.host_key_type)) # noqa
self.host_key = key
def _compute_key(self, id, nbytes):
@@ -1759,8 +1761,8 @@ class Transport(threading.Thread, ClosingContextManager):
# Fallback to SHA1 for kex engines that fail to specify a hex
# algorithm, or for e.g. transport tests that don't run kexinit.
hash_algo = getattr(self.kex_engine, 'hash_algo', None)
- hash_select_msg = "kex engine %s specified hash_algo %r" % (
- self.kex_engine.__class__.__name__, hash_algo
+ hash_select_msg = "kex engine {} specified hash_algo {!r}".format(
+ self.kex_engine.__class__.__name__, hash_algo,
)
if hash_algo is None:
hash_algo = sha1
@@ -1881,13 +1883,13 @@ class Transport(threading.Thread, ClosingContextManager):
_active_threads.append(self)
tid = hex(long(id(self)) & xffffffff)
if self.server_mode:
- self._log(DEBUG, 'starting thread (server mode): %s' % tid)
+ self._log(DEBUG, 'starting thread (server mode): {}'.format(tid))
else:
- self._log(DEBUG, 'starting thread (client mode): %s' % tid)
+ self._log(DEBUG, 'starting thread (client mode): {}'.format(tid))
try:
try:
self.packetizer.write_all(b(self.local_version + '\r\n'))
- self._log(DEBUG, 'Local version/idstring: %s' % self.local_version) # noqa
+ self._log(DEBUG, 'Local version/idstring: {}'.format(self.local_version)) # noqa
self._check_banner()
# The above is actually very much part of the handshake, but
# sometimes the banner can be read but the machine is not
@@ -1917,7 +1919,7 @@ class Transport(threading.Thread, ClosingContextManager):
continue
if len(self._expected_packet) > 0:
if ptype not in self._expected_packet:
- raise SSHException('Expecting packet from %r, got %d' % (self._expected_packet, ptype)) # noqa
+ raise SSHException('Expecting packet from {!r}, got {:d}'.format(self._expected_packet, ptype)) # noqa
self._expected_packet = tuple()
if (ptype >= 30) and (ptype <= 41):
self.kex_engine.parse_next(ptype, m)
@@ -1935,9 +1937,9 @@ class Transport(threading.Thread, ClosingContextManager):
if chan is not None:
self._channel_handler_table[ptype](chan, m)
elif chanid in self.channels_seen:
- self._log(DEBUG, 'Ignoring message for dead channel %d' % chanid) # noqa
+ self._log(DEBUG, 'Ignoring message for dead channel {:d}'.format(chanid)) # noqa
else:
- self._log(ERROR, 'Channel request for unknown channel %d' % chanid) # noqa
+ self._log(ERROR, 'Channel request for unknown channel {:d}'.format(chanid)) # noqa
break
elif (
self.auth_handler is not None and
@@ -1948,7 +1950,8 @@ class Transport(threading.Thread, ClosingContextManager):
if len(self._expected_packet) > 0:
continue
else:
- self._log(WARNING, 'Oops, unhandled type %d' % ptype)
+ err = 'Oops, unhandled type {:d}'.format(ptype)
+ self._log(WARNING, err)
msg = Message()
msg.add_byte(cMSG_UNIMPLEMENTED)
msg.add_int(m.seqno)
@@ -1964,7 +1967,7 @@ class Transport(threading.Thread, ClosingContextManager):
except socket.error as e:
if type(e.args) is tuple:
if e.args:
- emsg = '%s (%d)' % (e.args[1], e.args[0])
+ emsg = '{} ({:d})'.format(e.args[1], e.args[0])
else: # empty tuple, e.g. socket.timeout
emsg = str(e) or repr(e)
else:
@@ -2006,11 +2009,11 @@ class Transport(threading.Thread, ClosingContextManager):
# Log useful, non-duplicative line re: an agreed-upon algorithm.
# Old code implied algorithms could be asymmetrical (different for
# inbound vs outbound) so we preserve that possibility.
- msg = "{0} agreed: ".format(which)
+ msg = "{} agreed: ".format(which)
if local == remote:
msg += local
else:
- msg += "local={0}, remote={1}".format(local, remote)
+ msg += "local={}, remote={}".format(local, remote)
self._log(DEBUG, msg)
# protocol stages
@@ -2052,7 +2055,7 @@ class Transport(threading.Thread, ClosingContextManager):
raise SSHException('Indecipherable protocol version "' + buf + '"')
# save this server version string for later
self.remote_version = buf
- self._log(DEBUG, 'Remote version/idstring: %s' % buf)
+ self._log(DEBUG, 'Remote version/idstring: {}'.format(buf))
# pull off any attached comment
# NOTE: comment used to be stored in a variable and then...never used.
# since 2003. ca 877cd974b8182d26fa76d566072917ea67b64e67
@@ -2066,9 +2069,9 @@ class Transport(threading.Thread, ClosingContextManager):
version = segs[1]
client = segs[2]
if version != '1.99' and version != '2.0':
- msg = 'Incompatible version ({0} instead of 2.0)'
+ msg = 'Incompatible version ({} instead of 2.0)'
raise SSHException(msg.format(version))
- msg = 'Connected (version {0}, client {1})'.format(version, client)
+ msg = 'Connected (version {}, client {})'.format(version, client)
self._log(INFO, msg)
def _send_kex_init(self):
@@ -2171,7 +2174,7 @@ class Transport(threading.Thread, ClosingContextManager):
if len(agreed_kex) == 0:
raise SSHException('Incompatible ssh peer (no acceptable kex algorithm)') # noqa
self.kex_engine = self._kex_info[agreed_kex[0]](self)
- self._log(DEBUG, "Kex agreed: %s" % agreed_kex[0])
+ self._log(DEBUG, "Kex agreed: {}".format(agreed_kex[0]))
if self.server_mode:
available_server_keys = list(filter(
@@ -2264,7 +2267,7 @@ class Transport(threading.Thread, ClosingContextManager):
len(agreed_local_compression) == 0 or
len(agreed_remote_compression) == 0
):
- msg = 'Incompatible ssh server (no acceptable compression) {0!r} {1!r} {2!r}' # noqa
+ msg = 'Incompatible ssh server (no acceptable compression) {!r} {!r} {!r}' # noqa
raise SSHException(msg.format(
agreed_local_compression, agreed_remote_compression,
self._preferred_compression,
@@ -2407,16 +2410,16 @@ class Transport(threading.Thread, ClosingContextManager):
def _parse_disconnect(self, m):
code = m.get_int()
desc = m.get_text()
- self._log(INFO, 'Disconnect (code %d): %s' % (code, desc))
+ self._log(INFO, 'Disconnect (code {:d}): {}'.format(code, desc))
def _parse_global_request(self, m):
kind = m.get_text()
- self._log(DEBUG, 'Received global request "%s"' % kind)
+ self._log(DEBUG, 'Received global request "{}"'.format(kind))
want_reply = m.get_boolean()
if not self.server_mode:
self._log(
DEBUG,
- 'Rejecting "%s" global request from server.' % kind
+ 'Rejecting "{}" global request from server.'.format(kind)
)
ok = False
elif kind == 'tcpip-forward':
@@ -2470,7 +2473,7 @@ class Transport(threading.Thread, ClosingContextManager):
try:
chan._set_remote_channel(
server_chanid, server_window_size, server_max_packet_size)
- self._log(DEBUG, 'Secsh channel %d opened.' % chanid)
+ self._log(DEBUG, 'Secsh channel {:d} opened.'.format(chanid))
if chanid in self.channel_events:
self.channel_events[chanid].set()
del self.channel_events[chanid]
@@ -2486,8 +2489,9 @@ class Transport(threading.Thread, ClosingContextManager):
reason_text = CONNECTION_FAILED_CODE.get(reason, '(unknown code)')
self._log(
ERROR,
- 'Secsh channel %d open FAILED: %s: %s' % (
- chanid, reason_str, reason_text)
+ 'Secsh channel {:d} open FAILED: {}: {}'.format(
+ chanid, reason_str, reason_text,
+ )
)
self.lock.acquire()
try:
@@ -2522,8 +2526,9 @@ class Transport(threading.Thread, ClosingContextManager):
origin_port = m.get_int()
self._log(
DEBUG,
- 'Incoming x11 connection from %s:%d' % (
- origin_addr, origin_port)
+ 'Incoming x11 connection from {}:{:d}'.format(
+ origin_addr, origin_port,
+ )
)
self.lock.acquire()
try:
@@ -2537,8 +2542,9 @@ class Transport(threading.Thread, ClosingContextManager):
origin_port = m.get_int()
self._log(
DEBUG,
- 'Incoming tcp forwarded connection from %s:%d' % (
- origin_addr, origin_port)
+ 'Incoming tcp forwarded connection from {}:{:d}'.format(
+ origin_addr, origin_port,
+ )
)
self.lock.acquire()
try:
@@ -2548,7 +2554,7 @@ class Transport(threading.Thread, ClosingContextManager):
elif not self.server_mode:
self._log(
DEBUG,
- 'Rejecting "%s" channel request from server.' % kind)
+ 'Rejecting "{}" channel request from server.'.format(kind))
reject = True
reason = OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
else:
@@ -2574,7 +2580,7 @@ class Transport(threading.Thread, ClosingContextManager):
if reason != OPEN_SUCCEEDED:
self._log(
DEBUG,
- 'Rejecting "%s" channel request from client.' % kind)
+ 'Rejecting "{}" channel request from client.'.format(kind))
reject = True
if reject:
msg = Message()
@@ -2605,7 +2611,9 @@ class Transport(threading.Thread, ClosingContextManager):
m.add_int(self.default_window_size)
m.add_int(self.default_max_packet_size)
self._send_message(m)
- self._log(DEBUG, 'Secsh channel %d (%s) opened.', my_chanid, kind)
+ self._log(DEBUG,
+ 'Secsh channel {:d} ({}) opened.'.format(my_chanid, kind)
+ )
if kind == 'auth-agent@openssh.com':
self._forward_agent_handler(chan)
elif kind == 'x11':
@@ -2624,7 +2632,7 @@ class Transport(threading.Thread, ClosingContextManager):
m.get_boolean() # always_display
msg = m.get_string()
m.get_string() # language
- self._log(DEBUG, 'Debug msg: {0}'.format(util.safe_string(msg)))
+ self._log(DEBUG, 'Debug msg: {}'.format(util.safe_string(msg)))
def _get_subsystem_handler(self, name):
try:
@@ -2679,7 +2687,7 @@ class SecurityOptions (object):
"""
Returns a string representation of this object, for debugging.
"""
- return '<paramiko.SecurityOptions for %s>' % repr(self._transport)
+ return '<paramiko.SecurityOptions for {!r}>'.format(self._transport)
def _set(self, name, orig, x):
if type(x) is list: