This is an automated email from the git hooks/post-receive script. x2go pushed a commit to branch master in repository x2gobroker. commit 01be4facc37f05abe9dd87e99b7c4c0ed15a9f20 Author: Mike Gabriel <mike.gabriel@das-netzwerkteam.de> Date: Sun Dec 7 00:05:25 2014 +0100 Do an ICMP ping before querying a remote agent via SSH. --- debian/changelog | 1 + x2gobroker/agent.py | 93 ++++++++++++++++++++++++++------------------------- 2 files changed, 48 insertions(+), 46 deletions(-) diff --git a/debian/changelog b/debian/changelog index a0cc46b..9b765eb 100644 --- a/debian/changelog +++ b/debian/changelog @@ -202,6 +202,7 @@ x2gobroker (0.0.3.0-0x2go1) UNRELEASED; urgency=low - Enable basic/random load-balancing for UCCS broker frontend. Make UCCS frontend aware of host session profile options of the form "host=<fqdn> (<ipaddr>:<port>). + - Do an ICMP ping before querying a remote agent via SSH. * debian/control: + Provide separate bin:package for SSH brokerage: x2gobroker-ssh. + Replace LDAP support with session brokerage support in LONG_DESCRIPTION. diff --git a/x2gobroker/agent.py b/x2gobroker/agent.py index e4bef4d..531c407 100644 --- a/x2gobroker/agent.py +++ b/x2gobroker/agent.py @@ -167,55 +167,56 @@ def _call_remote_broker_agent(username, task, cmdline_args=[], remote_agent=None elif not remote_agent.has_key('host_key_policy'): remote_agent['host_key_policy'] = paramiko.WarningPolicy() - cmd_line = [ - '{x2gobroker_agent_binary}'.format(x2gobroker_agent_binary=x2gobroker.defaults.X2GOBROKER_AGENT_CMD), - '{username}'.format(username=username), - '{task}'.format(task=task), - ] - - for cmdline_arg in cmdline_args: - cmd_line.append('"{arg}"'.format(arg=cmdline_arg)) - - remote_username = x2gobroker.defaults.X2GOBROKER_AGENT_USER remote_hostname = remote_agent[u'hostname'] - if remote_agent.has_key(u'port'): - remote_port = int(remote_agent[u'port']) - else: - remote_port = 22 - - # now, connect and use paramiko Client to negotiate SSH2 across the connection - try: - client = paramiko.SSHClient() - client.load_system_host_keys() - if os.path.exists(os.path.expanduser("~/.ssh/known_hosts")): - client.load_host_keys(os.path.expanduser("~/.ssh/known_hosts")) - client.set_missing_host_key_policy(remote_agent['host_key_policy']) - client.connect(remote_hostname, remote_port, remote_username, look_for_keys=True, allow_agent=True) - - result = [] - ssh_transport = client.get_transport() - if ssh_transport and ssh_transport.is_authenticated(): - cmd = ' '.join(cmd_line) - cmd = 'sh -c \'{cmd}\''.format(cmd=cmd) - logger_broker.info('Executing agent command on remote host ({remote_agent}): {cmd}'.format(remote_agent=remote_agent['hostname'], cmd=cmd)) - (stdin, stdout, stderr) = client.exec_command(cmd) - result = stdout.read().split('\n') - err = stderr.read().replace('\n', ' ') - if err: - logger_broker.warning('Remote agent command (host: {remote_agent}) reported an error: {err}'.format(remote_agent=remote_agent['hostname'], err=err)) - client.close() - if result: - logger_broker.info('Broker agent answered: {answer}'.format(answer="; ".join(result))) - if result and result[0].startswith('OK'): - return (True, [ r for r in result[1:] if r ]) + if icmp_ping(remote_hostname): + cmd_line = [ + '{x2gobroker_agent_binary}'.format(x2gobroker_agent_binary=x2gobroker.defaults.X2GOBROKER_AGENT_CMD), + '{username}'.format(username=username), + '{task}'.format(task=task), + ] + + for cmdline_arg in cmdline_args: + cmd_line.append('"{arg}"'.format(arg=cmdline_arg)) + + remote_username = x2gobroker.defaults.X2GOBROKER_AGENT_USER + if remote_agent.has_key(u'port'): + remote_port = int(remote_agent[u'port']) else: - return (False, []) - except paramiko.AuthenticationException: - raise x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException('Authentication to remote X2Go Broker Agent Host failed (user: {user}, hostname: {hostname}, port: {port}) failed'.format(user=remote_username, hostname=remote_hostname, port=remote_port)) - except (paramiko.SSHException, paramiko.BadHostKeyException, socket.error): - raise x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException('Query to remote X2Go Broker Agent (user: {user}, hostname: {hostname}, port: {port}) failed'.format(user=remote_username, hostname=remote_hostname, port=remote_port)) + remote_port = 22 - raise x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException('Query to remote X2Go Broker Agent failed with no response') + # now, connect and use paramiko Client to negotiate SSH2 across the connection + try: + client = paramiko.SSHClient() + client.load_system_host_keys() + if os.path.exists(os.path.expanduser("~/.ssh/known_hosts")): + client.load_host_keys(os.path.expanduser("~/.ssh/known_hosts")) + client.set_missing_host_key_policy(remote_agent['host_key_policy']) + client.connect(remote_hostname, remote_port, remote_username, look_for_keys=True, allow_agent=True) + + result = [] + ssh_transport = client.get_transport() + if ssh_transport and ssh_transport.is_authenticated(): + cmd = ' '.join(cmd_line) + cmd = 'sh -c \'{cmd}\''.format(cmd=cmd) + logger_broker.info('Executing agent command on remote host ({remote_agent}): {cmd}'.format(remote_agent=remote_agent['hostname'], cmd=cmd)) + (stdin, stdout, stderr) = client.exec_command(cmd) + result = stdout.read().split('\n') + err = stderr.read().replace('\n', ' ') + if err: + logger_broker.warning('Remote agent command (host: {remote_agent}) reported an error: {err}'.format(remote_agent=remote_agent['hostname'], err=err)) + client.close() + if result: + logger_broker.info('Broker agent answered: {answer}'.format(answer="; ".join(result))) + if result and result[0].startswith('OK'): + return (True, [ r for r in result[1:] if r ]) + else: + return (False, []) + except paramiko.AuthenticationException: + raise x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException('Authentication to remote X2Go Broker Agent Host failed (user: {user}, hostname: {hostname}, port: {port}) failed'.format(user=remote_username, hostname=remote_hostname, port=remote_port)) + except (paramiko.SSHException, paramiko.BadHostKeyException, socket.error): + raise x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException('Query to remote X2Go Broker Agent (user: {user}, hostname: {hostname}, port: {port}) failed'.format(user=remote_username, hostname=remote_hostname, port=remote_port)) + else: + raise x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException('Could not ping remote X2Go Broker Agent host ({remote_hostname})'.format(remote_hostname=remote_hostname)) def icmp_ping(hostname): -- Alioth's /srv/git/_hooks_/post-receive-email on /srv/git/code.x2go.org/x2gobroker.git