def addUser(username, password, name=None) : passwd = readFromFile("/etc/passwd") if passwd.find("%s:" % username) < 0 : expects = [ ["Enter new UNIX password: "******"Retype new UNIX password: "******".*password updated successfully", None], ] sbpexpect.expect("adduser --force-badname --ingroup users --gecos='%s,,,' %s" % (name or "", username), expects) else : return False return True
def addUser(username, password, name=None): passwd = readFromFile("/etc/passwd") if passwd.find("%s:" % username) < 0: expects = [ ["Enter new UNIX password: "******"Retype new UNIX password: "******".*password updated successfully", None], ] sbpexpect.expect( "adduser --force-badname --ingroup users --gecos='%s,,,' %s" % (name or "", username), expects) else: return False return True
def runSSHCommand(username, host, command, port=None, password=None, timeout=TIMEOUT) : sshCommand = """ssh %s@%s %s "%s" """ % (username, host, port and "-p %s" % port or "", command) debugOutput("running ssh command: %s" % sshCommand) if password : return sbpexpect.runExpect(sshCommand, events={"assword": "%s\n" % password,"yes/no":"yes\n"}, timeout=timeout) else : return sbpexpect.expect(sshCommand, [[".*", None]], timeout=timeout)
def addSSHPeer(username, password, host) : debugOutput("Adding ssh peer for %s to %s" % (username, host)) # XXX: Does not always pick up root username. sshKeyFile = os.path.expanduser("~/.ssh/id_rsa.pub") sshKeyFile = os.path.expanduser("/root/.ssh/id_rsa.pub") if not os.path.exists(sshKeyFile) : debugOutput("Generating key file") expects = [ [".*Enter file in which to save the key.*:", "\n"], ["Enter passphrase.*:", "\n"], ["Enter same passphrase again.*:", "\n"], [".*", None], ] try : sbpexpect.expect("ssh-keygen -t rsa", expects, timeout=TIMEOUT) except Exception, e: raise Exception("Unable to generate ssh key in %s: %s" % (sshKeyFile, e))
def addSSHPeer(username, password, host): debugOutput("Adding ssh peer for %s to %s" % (username, host)) # XXX: Does not always pick up root username. sshKeyFile = os.path.expanduser("~/.ssh/id_rsa.pub") sshKeyFile = os.path.expanduser("/root/.ssh/id_rsa.pub") if not os.path.exists(sshKeyFile): debugOutput("Generating key file") expects = [ [".*Enter file in which to save the key.*:", "\n"], ["Enter passphrase.*:", "\n"], ["Enter same passphrase again.*:", "\n"], [".*", None], ] try: sbpexpect.expect("ssh-keygen -t rsa", expects, timeout=TIMEOUT) except Exception, e: raise Exception("Unable to generate ssh key in %s: %s" % (sshKeyFile, e))
def runSSHCommand(username, host, command, port=None, password=None, timeout=TIMEOUT): sshCommand = """ssh %s@%s %s "%s" """ % (username, host, port and "-p %s" % port or "", command) debugOutput("running ssh command: %s" % sshCommand) if password: return sbpexpect.runExpect(sshCommand, events={ "assword": "%s\n" % password, "yes/no": "yes\n" }, timeout=timeout) else: return sbpexpect.expect(sshCommand, [[".*", None]], timeout=timeout)
def expect(command, qas, timeout=None): if timeout: return sbpexpect.expect(command, qas, timeout) else: return sbpexpect.expect(command, qas)
# If we don't have a security relation, add one if not testSSHFolderAccess(username, host): debugOutput("No ssh peering, so trying to create one") # Copy the key expects = [ [["(?i)password", "(?i)you sure you want to continue connecting"], ["%s" % password, "yes"]], [[None, "(?i)password"], [None, "%s" % password]], ] # if question is sure, then yes, if question is password, answer is password try: sbpexpect.expect("ssh-copy-id -i %s %s@%s" % (sshKeyFile, username, host), qas=expects, timeout=TIMEOUT) except: raise Exception( "Unable to create ssh peering with %s: check that the username, password and hostname are correct and that firewall or port forwarding settings are appropriately set on the destination." % host) # If we still don't have an ssh peering, raise an exception. if not testSSHFolderAccess(username, host): raise Exception( "Unable to create ssh peering with %s: check that the username, password and hostname are correct and that firewall or port forwarding settings are appropriately set on the destination." % host) debugOutput("ssh peering has been made successfully")
if not os.path.exists(sshKeyFile) : raise Exception("SSH key was not generated.") # If we don't have a security relation, add one if not testSSHFolderAccess(username, host) : debugOutput("No ssh peering, so trying to create one") # Copy the key expects = [ [["(?i)password","(?i)you sure you want to continue connecting"], ["%s" % password, "yes"]], [[None,"(?i)password"], [None, "%s" % password]], ] # if question is sure, then yes, if question is password, answer is password try : sbpexpect.expect("ssh-copy-id -i %s %s@%s" % (sshKeyFile, username, host), qas=expects, timeout=TIMEOUT) except : raise Exception("Unable to create ssh peering with %s: check that the username, password and hostname are correct and that firewall or port forwarding settings are appropriately set on the destination." % host) # If we still don't have an ssh peering, raise an exception. if not testSSHFolderAccess(username, host) : raise Exception("Unable to create ssh peering with %s: check that the username, password and hostname are correct and that firewall or port forwarding settings are appropriately set on the destination." % host) debugOutput("ssh peering has been made successfully") def testSSHFolderAccess(username, host, port=None, file="/tmp", password=None, timeout=TIMEOUT) : debugOutput("Tesing ssh access to %s@%s/%s" % (username, host, file)) TEST_STRING = "access-test-file" testFile = os.path.join(file, "sshaccesstest")
def expect(command, qas, timeout=None) : if timeout : return sbpexpect.expect(command, qas, timeout) else : return sbpexpect.expect(command, qas)