Package ClusterShell :: Package Worker :: Module Ssh
[hide private]
[frames] | no frames]

Source Code for Module ClusterShell.Worker.Ssh

  1  # 
  2  # Copyright (C) 2008-2015 CEA/DAM 
  3  # 
  4  # This file is part of ClusterShell. 
  5  # 
  6  # ClusterShell is free software; you can redistribute it and/or 
  7  # modify it under the terms of the GNU Lesser General Public 
  8  # License as published by the Free Software Foundation; either 
  9  # version 2.1 of the License, or (at your option) any later version. 
 10  # 
 11  # ClusterShell is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 14  # Lesser General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public 
 17  # License along with ClusterShell; if not, write to the Free Software 
 18  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
 19   
 20  """ 
 21  ClusterShell Ssh/Scp support 
 22   
 23  This module implements OpenSSH engine client and task's worker. 
 24  """ 
 25   
 26  import os 
 27  # Older versions of shlex can not handle unicode correctly. 
 28  # Consider using ushlex instead. 
 29  import shlex 
 30   
 31  from ClusterShell.Worker.Exec import ExecClient, CopyClient, ExecWorker 
 32   
 33   
34 -class SshClient(ExecClient):
35 """ 36 Ssh EngineClient. 37 """ 38
39 - def _build_cmd(self):
40 """ 41 Build the shell command line to start the ssh commmand. 42 Return an array of command and arguments. 43 """ 44 45 task = self.worker.task 46 path = task.info("ssh_path") or "ssh" 47 user = task.info("ssh_user") 48 options = task.info("ssh_options") 49 50 # Build ssh command 51 cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)] 52 53 # Add custom ssh options first as the first obtained value is 54 # used. Thus all options are overridable by custom options. 55 if options: 56 # use expanduser() for options like '-i ~/.ssh/my_id_rsa' 57 cmd_l += [os.path.expanduser(opt) for opt in shlex.split(options)] 58 59 # Hardwired options (overridable by ssh_options) 60 cmd_l += [ "-a", "-x" ] 61 62 if user: 63 cmd_l.append("-l") 64 cmd_l.append(user) 65 66 connect_timeout = task.info("connect_timeout", 0) 67 if connect_timeout > 0: 68 cmd_l.append("-oConnectTimeout=%d" % connect_timeout) 69 70 # Disable passphrase/password querying 71 # When used together with sshpass this must be overwritten 72 # by a custom option to "-oBatchMode=no". 73 cmd_l.append("-oBatchMode=yes") 74 75 cmd_l.append("%s" % self.key) 76 cmd_l.append("%s" % self.command) 77 78 return (cmd_l, None)
79
80 -class ScpClient(CopyClient):
81 """ 82 Scp EngineClient. 83 """ 84
85 - def _build_cmd(self):
86 """ 87 Build the shell command line to start the scp commmand. 88 Return an array of command and arguments. 89 """ 90 91 task = self.worker.task 92 path = task.info("scp_path") or "scp" 93 user = task.info("scp_user") or task.info("ssh_user") 94 95 # If defined exclusively use scp_options. If no scp_options 96 # given use ssh_options instead. 97 options = task.info("scp_options") or task.info("ssh_options") 98 99 # Build scp command 100 cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)] 101 102 # Add custom ssh options first as the first obtained value is 103 # used. Thus all options are overridable by custom options. 104 if options: 105 # use expanduser() for options like '-i ~/.ssh/my_id_rsa' 106 cmd_l += [os.path.expanduser(opt) for opt in shlex.split(options)] 107 108 # Hardwired options (overridable by ssh_options) 109 if self.isdir: 110 cmd_l.append("-r") 111 112 if self.preserve: 113 cmd_l.append("-p") 114 115 connect_timeout = task.info("connect_timeout", 0) 116 if connect_timeout > 0: 117 cmd_l.append("-oConnectTimeout=%d" % connect_timeout) 118 119 # Disable passphrase/password querying 120 # When used together with sshpass this must be overwritten 121 # by a custom option to "-oBatchMode=no". 122 cmd_l.append("-oBatchMode=yes") 123 124 125 if self.reverse: 126 if user: 127 cmd_l.append("%s@%s:%s" % (user, self.key, self.source)) 128 else: 129 cmd_l.append("%s:%s" % (self.key, self.source)) 130 131 cmd_l.append(os.path.join(self.dest, "%s.%s" % \ 132 (os.path.basename(self.source), self.key))) 133 else: 134 cmd_l.append(self.source) 135 if user: 136 cmd_l.append("%s@%s:%s" % (user, self.key, self.dest)) 137 else: 138 cmd_l.append("%s:%s" % (self.key, self.dest)) 139 140 return (cmd_l, None)
141
142 -class WorkerSsh(ExecWorker):
143 """ 144 ClusterShell ssh-based worker Class. 145 146 Remote Shell (ssh) usage example: 147 >>> worker = WorkerSsh(nodeset, handler=MyEventHandler(), 148 ... timeout=30, command="/bin/hostname") 149 >>> task.schedule(worker) # schedule worker for execution 150 >>> task.resume() # run 151 152 Remote Copy (scp) usage example: 153 >>> worker = WorkerSsh(nodeset, handler=MyEventHandler(), 154 ... timeout=30, source="/etc/my.conf", 155 ... dest="/etc/my.conf") 156 >>> task.schedule(worker) # schedule worker for execution 157 >>> task.resume() # run 158 """ 159 160 SHELL_CLASS = SshClient 161 COPY_CLASS = ScpClient
162 163 WORKER_CLASS=WorkerSsh 164