[MediaWiki-commits] [Gerrit] mod - refactor ssh_command to use SSHClient. - change (sartoris)
Rfaulk has uploaded a new change for review. https://gerrit.wikimedia.org/r/84733 Change subject: mod - refactor ssh_command to use SSHClient. .. mod - refactor ssh_command to use SSHClient. Change-Id: Ifeceab356fdec9609f9813716e5c2627bdae3ea2 --- M sartoris/sartoris.py 1 file changed, 16 insertions(+), 32 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/sartoris refs/changes/33/84733/1 diff --git a/sartoris/sartoris.py b/sartoris/sartoris.py index ef45676..c9d4979 100755 --- a/sartoris/sartoris.py +++ b/sartoris/sartoris.py @@ -384,7 +384,7 @@ filter(lambda x: x, proc.communicate( @staticmethod -def scp_file(source, target, user, password, host, port=22): +def scp_file(source, target, user, host, port=22): SCP files via paramiko. @@ -418,40 +418,24 @@ t.close() sock.close() -@staticmethod -def ssh_command(cmd, host, user, port=22, nbytes=4096): +def ssh_command_target(self, cmd): + +Talk to the target via SSHClient + -# Initialize connection -client = paramiko.Transport((host, port)) - -rsa_key = paramiko.RSAKey.from_private_key_file(PKEY) -client.connect(username=user, pkey=rsa_key) - -# Prepare stream lists, open session, exec command -stdout_data = [] -stderr_data = [] - -session = client.open_channel(kind='session') -session.exec_command(cmd) - -# Read output -while True: -if session.recv_ready(): -stdout_data.append(session.recv(nbytes)) -if session.recv_stderr_ready(): -stderr_data.append(session.recv_stderr(nbytes)) -if session.exit_status_ready(): -break - -# Get exit status and close sessions -exit_status = session.recv_exit_status() -session.close() -client.close() +ssh = paramiko.SSHClient() +ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) +ssh.connect( +self.config['user.name'], +username=self.config['target'], +key_filename=PKEY) +stdin, stdout, stderr = ssh.exec_command(cmd) +ssh.close() return { -'exit_status': exit_status, -'stdout': ''.join(stdout_data), -'stderr': ''.join(stderr_data), +'stdin': stdin, +'stdout': '\n'.join(stdout.readlines()), +'stderr': '\n'.join(stderr.readlines()), } def resync(self, args): -- To view, visit https://gerrit.wikimedia.org/r/84733 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ifeceab356fdec9609f9813716e5c2627bdae3ea2 Gerrit-PatchSet: 1 Gerrit-Project: sartoris Gerrit-Branch: master Gerrit-Owner: Rfaulk rfaulk...@wikimedia.org ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
[MediaWiki-commits] [Gerrit] mod - refactor ssh_command to use SSHClient. - change (sartoris)
Rfaulk has submitted this change and it was merged. Change subject: mod - refactor ssh_command to use SSHClient. .. mod - refactor ssh_command to use SSHClient. Change-Id: Ifeceab356fdec9609f9813716e5c2627bdae3ea2 --- M sartoris/sartoris.py 1 file changed, 16 insertions(+), 32 deletions(-) Approvals: Rfaulk: Verified; Looks good to me, approved jenkins-bot: Verified diff --git a/sartoris/sartoris.py b/sartoris/sartoris.py index ef45676..c9d4979 100755 --- a/sartoris/sartoris.py +++ b/sartoris/sartoris.py @@ -384,7 +384,7 @@ filter(lambda x: x, proc.communicate( @staticmethod -def scp_file(source, target, user, password, host, port=22): +def scp_file(source, target, user, host, port=22): SCP files via paramiko. @@ -418,40 +418,24 @@ t.close() sock.close() -@staticmethod -def ssh_command(cmd, host, user, port=22, nbytes=4096): +def ssh_command_target(self, cmd): + +Talk to the target via SSHClient + -# Initialize connection -client = paramiko.Transport((host, port)) - -rsa_key = paramiko.RSAKey.from_private_key_file(PKEY) -client.connect(username=user, pkey=rsa_key) - -# Prepare stream lists, open session, exec command -stdout_data = [] -stderr_data = [] - -session = client.open_channel(kind='session') -session.exec_command(cmd) - -# Read output -while True: -if session.recv_ready(): -stdout_data.append(session.recv(nbytes)) -if session.recv_stderr_ready(): -stderr_data.append(session.recv_stderr(nbytes)) -if session.exit_status_ready(): -break - -# Get exit status and close sessions -exit_status = session.recv_exit_status() -session.close() -client.close() +ssh = paramiko.SSHClient() +ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) +ssh.connect( +self.config['user.name'], +username=self.config['target'], +key_filename=PKEY) +stdin, stdout, stderr = ssh.exec_command(cmd) +ssh.close() return { -'exit_status': exit_status, -'stdout': ''.join(stdout_data), -'stderr': ''.join(stderr_data), +'stdin': stdin, +'stdout': '\n'.join(stdout.readlines()), +'stderr': '\n'.join(stderr.readlines()), } def resync(self, args): -- To view, visit https://gerrit.wikimedia.org/r/84733 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ifeceab356fdec9609f9813716e5c2627bdae3ea2 Gerrit-PatchSet: 1 Gerrit-Project: sartoris Gerrit-Branch: master Gerrit-Owner: Rfaulk rfaulk...@wikimedia.org Gerrit-Reviewer: Rfaulk rfaulk...@wikimedia.org Gerrit-Reviewer: jenkins-bot ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits