Revision 82122173 lib/ssh.py

b/lib/ssh.py
29 29
from ganeti import logger
30 30
from ganeti import utils
31 31
from ganeti import errors
32
from ganeti import constants
33

  
34

  
35
__all__ = ["SSHCall", "CopyFileToNode", "VerifyNodeHostname",
36
           "KNOWN_HOSTS_OPTS", "BATCH_MODE_OPTS", "ASK_KEY_OPTS"]
37

  
38

  
39
KNOWN_HOSTS_OPTS = [
40
  "-oGlobalKnownHostsFile=%s" % constants.SSH_KNOWN_HOSTS_FILE,
41
  "-oUserKnownHostsFile=/dev/null",
42
  ]
43

  
44
# Note: BATCH_MODE conflicts with ASK_KEY
45
BATCH_MODE_OPTS = [
46
  "-oEscapeChar=none",
47
  "-oBatchMode=yes",
48
  "-oStrictHostKeyChecking=yes",
49
  ]
50

  
51
ASK_KEY_OPTS = [
52
  "-oStrictHostKeyChecking=ask",
53
  "-oEscapeChar=none",
54
  "-oHashKnownHosts=no",
55
  ]
56

  
32 57

  
33 58
def SSHCall(hostname, user, command, batch=True, ask_key=False):
34 59
  """Execute a command on a remote node.
......
40 65
    hostname: the target host, string
41 66
    user: user to auth as
42 67
    command: the command
68
    batch: if true, ssh will run in batch mode with no prompting
69
    ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that
70
             we can connect to an unknown host (not valid in batch mode)
43 71

  
44 72
  Returns:
45 73
    `utils.RunResult` as for `utils.RunCmd()`
46 74

  
47 75
  """
48
  argv = ["ssh", "-q", "-oEscapeChar=none"]
76
  argv = ["ssh", "-q"]
77
  argv.extend(KNOWN_HOSTS_OPTS)
49 78
  if batch:
50
    argv.append("-oBatchMode=yes")
51 79
    # if we are in batch mode, we can't ask the key
52 80
    if ask_key:
53 81
      raise errors.ProgrammerError("SSH call requested conflicting options")
54
  if ask_key:
55
    argv.append("-oStrictHostKeyChecking=ask")
56
    argv.append("-oHashKnownHosts=no")
57
  else:
58
    argv.append("-oStrictHostKeyChecking=yes")
82
    argv.extend(BATCH_MODE_OPTS)
83
  elif ask_key:
84
    argv.extend(ASK_KEY_OPTS)
59 85
  argv.extend(["%s@%s" % (user, hostname), command])
60 86
  return utils.RunCmd(argv)
61 87

  
......
79 105
    logger.Error("file %s must be an absolute path" % (filename))
80 106
    return False
81 107

  
82
  command = ["scp", "-q", "-p", "-oStrictHostKeyChecking=yes",
83
             "-oBatchMode=yes", filename, "%s:%s" % (node, filename)]
108
  command = ["scp", "-q", "-p"]
109
  command.extend(KNOWN_HOSTS_OPTS)
110
  command.extend(BATCH_MODE_OPTS)
111
  command.append(filename)
112
  command.append("%s:%s" % (node, filename))
84 113

  
85 114
  result = utils.RunCmd(command)
86 115

  

Also available in: Unified diff