|
From: | Chris Satterthwaite |
Subject: | Re: [Fab-user] command timeouts via Connection.run |
Date: | Tue, 8 Jan 2019 13:40:37 -0600 |
Is this newsgroup active? It’s been a week and I haven’t seen any activity. Since I posted for the first time, I’m not sure if my message just hit bad timing over holidays, or if it’s stumped folks and I should post elsewhere. Thanks! From: Fab-user <address@hidden> On Behalf Of Chris Satterthwaite Fab community, I need some direction on getting command timeouts to work on remote SSH commands using fabric.connection.Connection.run. I’m new to Fabric so I’m assuming I have missed something straightforward; I appreciate any guidance you can provide. I scripted a quick test case below, using Paramiko’s exec_command and Fabric’s Connection.run. The command I’m sending from Windows over to a Linux remote endpoint is 'date; sleep 3; date', and attempting to force a timeout after 1 second. The result of the script shows the following, which illustrates Paramiko timing out as expected, but Fabric not timing out: ================================================= Calling useFabric with timeout 1: {'hide': True, 'env': {'command_timeout': '1'}} result is ok: True STDOUT: Mon Dec 31 12:38:19 CST 2018 STDOUT: Mon Dec 31 12:38:22 CST 2018 useFabric runtime in seconds: 3 Calling useParamiko with timeout 1: STDOUT: Mon Dec 31 12:38:22 CST 2018 Timed out useParamiko runtime in seconds: 1 ================================================= Test script follows: ================================================= import sys import traceback import socket import time from fabric import Connection from paramiko.client import SSHClient, AutoAddPolicy def useFabric(): try: timeout = 1 client = Connection(host='192.168.121.190', user='chris', connect_timeout='10', connect_kwargs={'password': 'changeMe!'}) runtimeArgs = {'hide': True, 'env': {'command_timeout' : str(timeout)}} print(' {}'.format(runtimeArgs)) result = client.run('date; sleep 3; date', **runtimeArgs) print(' result is ok: {}'.format(result.ok)) for line in result.stdout.split('\n'): if len(line) > 1: print(' STDOUT: {}'.format(line)) if len(result.stderr) > 1: print(' STDERR: {}'.format(result.stderr)) except socket.timeout: print(' Timed out: {}'.format(str(sys.exc_info()[2]))) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) print(' Exception: {}'.format(stacktrace)) def useParamiko(): try: timeout = 1 client = SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(AutoAddPolicy) client.connect('192.168.121.190', username='chris', password='changeMe!') (stdin, stdout, stderr) = client.exec_command('date; sleep 3; date', timeout=timeout) for line in stdout: print(' STDOUT: {}'.format(line)) for line in stderr: print(' STDERR: {}'.format(line)) except socket.timeout: print(' Timed out') except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) print(' Exception: {}'.format(stacktrace)) def caller(func): print('Calling {} with timeout 1:'.format(str(func))) start = time.time() eval(func)() stop = time.time() print(' {} runtime in seconds: {}\n'.format(func, int(stop-start))) def main(): caller('useFabric') caller('useParamiko') if __name__ == '__main__': main() ================================================= Lastly, here are my platform versions in case they are needed: Base platform (where I ran the script below): Windows 10 Pro Python version on the base platform: Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] Target platform: RedHat Linux 7.5 Fabric version (from pip): 2.2.1 Paramiko version (from pip): 2.4.1 Thanks! |
[Prev in Thread] | Current Thread | [Next in Thread] |