Debian Codegraph @

lib/ClusterShell/Worker/Ssh.py

raw symbols SCIP
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 command.
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 # note: you should use only long-format options here
61 cmd_l += ["-oForwardAgent=no", "-oForwardX11=no"]
62
63 if user:
64 cmd_l.append("-l")
65 cmd_l.append(user)
66
67 connect_timeout = task.info("connect_timeout", 0)
68 if connect_timeout > 0:
69 cmd_l.append("-oConnectTimeout=%d" % connect_timeout)
70
71 # Disable passphrase/password querying
72 # When used together with sshpass this must be overwritten
73 # by a custom option to "-oBatchMode=no".
74 cmd_l.append("-oBatchMode=yes")
75
76 cmd_l.append("%s" % self.key)
77 cmd_l.append("%s" % self.command)
78
79 return (cmd_l, None)
80
81 class ScpClient(CopyClient):
82 """
83 Scp EngineClient.
84 """
85
86 def _build_cmd(self):
87 """
88 Build the shell command line to start the scp command.
89 Return an array of command and arguments.
90 """
91
92 task = self.worker.task
93 path = task.info("scp_path") or "scp"
94 user = task.info("scp_user") or task.info("ssh_user")
95
96 # If defined exclusively use scp_options. If no scp_options
97 # given use ssh_options instead.
98 options = task.info("scp_options") or task.info("ssh_options")
99
100 # Build scp command
101 cmd_l = [os.path.expanduser(pathc) for pathc in shlex.split(path)]
102
103 # Add custom ssh options first as the first obtained value is
104 # used. Thus all options are overridable by custom options.
105 if options:
106 # use expanduser() for options like '-i ~/.ssh/my_id_rsa'
107 cmd_l += [os.path.expanduser(opt) for opt in shlex.split(options)]
108
109 # Hardwired options
110 if self.isdir:
111 cmd_l.append("-r")
112
113 if self.preserve:
114 cmd_l.append("-p")
115
116 connect_timeout = task.info("connect_timeout", 0)
117 if connect_timeout > 0:
118 cmd_l.append("-oConnectTimeout=%d" % connect_timeout)
119
120 # Disable passphrase/password querying
121 # When used together with sshpass this must be overwritten
122 # by a custom option to "-oBatchMode=no".
123 #cmd_l.append("-oBatchMode=yes")
124
125
126 if self.reverse:
127 if user:
128 cmd_l.append("%s@[%s]:%s" % (user, self.key, self.source))
129 else:
130 cmd_l.append("[%s]:%s" % (self.key, self.source))
131
132 cmd_l.append(os.path.join(self.dest, "%s.%s" % \
133 (os.path.basename(self.source), self.key)))
134 else:
135 cmd_l.append(self.source)
136 if user:
137 cmd_l.append("%s@[%s]:%s" % (user, self.key, self.dest))
138 else:
139 cmd_l.append("[%s]:%s" % (self.key, self.dest))
140
141 return (cmd_l, None)
142
143 class WorkerSsh(ExecWorker):
144 """
145 ClusterShell ssh-based worker Class.
146
147 Remote Shell (ssh) usage example:
148 >>> worker = WorkerSsh(nodeset, handler=MyEventHandler(),
149 ... timeout=30, command="/bin/hostname")
150 >>> task.schedule(worker) # schedule worker for execution
151 >>> task.resume() # run
152
153 Remote Copy (scp) usage example:
154 >>> worker = WorkerSsh(nodeset, handler=MyEventHandler(),
155 ... timeout=30, source="/etc/my.conf",
156 ... dest="/etc/my.conf")
157 >>> task.schedule(worker) # schedule worker for execution
158 >>> task.resume() # run
159 """
160
161 SHELL_CLASS = SshClient
162 COPY_CLASS = ScpClient
163
164 WORKER_CLASS=WorkerSsh