def _mysql(pwd): """Open a mysql client and auth login. """ cmd = shlex.split('mysql -uroot -p -A') child = pexpect.spawn(cmd[0], cmd[1:]) if not _auth(child, pwd): return False return child
def fupdatepwd(pwd): """Force update password of root. MySQL server should first enter rescue mode. """ child = pexpect.spawn('mysql -A') i = child.expect(['mysql>', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False try: if not _sql( child, 'UPDATE mysql.user SET Password=PASSWORD("%s") WHERE User="******"' % _escape(pwd)): raise Exception() if not _sql(child, 'FLUSH PRIVILEGES'): raise Exception() except: _exit(child) return False else: _exit(child) return True
def scan(disk, size=''): """Rescan partitions on a disk. True will return if scan successfully, or else False will return. Example: fdisk.scan('/dev/sdb') """ try: cmd = shlex.split('fdisk \'%s\'' % disk) except: return False child = pexpect.spawn(cmd[0], cmd[1:]) i = child.expect(['(m for help)', 'Unable to open']) if i == 1: child.wait() return False child.sendline('w') i = child.expect( ['The kernel still uses the old table', pexpect.TIMEOUT, pexpect.EOF], timeout=1) if i == 0: rt = False else: rt = True if child.isalive(): child.wait() return rt
def delete(partition): """Delete a partition. True will return if delete successfully, or else False will return. Example: fdisk.delete('/dev/sdb1') """ disk = partition[:-1] partno = partition[-1:] try: cmd = shlex.split('fdisk \'%s\'' % disk) except: return False child = pexpect.spawn(cmd[0], cmd[1:]) i = child.expect(['(m for help)', 'Unable to open']) if i == 1: if child.isalive(): child.wait() return False child.sendline('d') rt = True i = child.expect([ 'Partition number', 'Selected partition %s' % partno, 'No partition is defined yet', pexpect.TIMEOUT ], timeout=1) if i == 0: child.sendline(partno) elif i == 2 or i == 3: rt = False if rt: i = child.expect(['(m for help)', 'has empty type']) if i == 0: child.sendline('w') elif i == 1: rt = False if not rt: child.sendline('q') if child.isalive(): child.wait() return rt
def run_cmd(self): rsync_cmd = self.get_cmd() print rsync_cmd if self.ssh_key_file: print "Rsync: use ssh key" self._run_command(rsync_cmd) elif self.password: print "Rsync: use password" rsync_process = pexpect.spawn(rsync_cmd) rsync_process.expect('.*[Pp]assword:.*') rsync_process.sendline(self.password) rsync_process.expect(pexpect.EOF) else: print "Rsync: no password and key" self._run_command(rsync_cmd)
def shutdown(pwd): """Shutdown mysql server. """ try: cmd = shlex.split('mysqladmin -uroot shutdown -p') except: return False child = pexpect.spawn(cmd[0], cmd[1:]) i = child.expect(['Enter password', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False child.sendline(pwd) i = child.expect(['error', pexpect.EOF]) if child.isalive(): return child.wait() == 0 return i != 0
def updatepwd(pwd, oldpwd): """Update password of root. """ try: cmd = shlex.split('mysqladmin -uroot password "%s" -p' % pwd) except: return False child = pexpect.spawn(cmd[0], cmd[1:]) i = child.expect(['Enter password', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False child.sendline(oldpwd) i = child.expect(['error', pexpect.EOF]) if child.isalive(): return child.wait() == 0 return i != 0
def passwd(username, password): try: cmd = shlex.split('passwd \'%s\'' % username) except: return False child = pexpect.spawn(cmd[0], cmd[1:]) i = child.expect(['New password', 'Unknown user name']) if i == 1: if child.isalive(): child.wait() return False child.sendline(password) child.expect('Retype new password') child.sendline(password) i = child.expect(['updated successfully', pexpect.EOF]) if child.isalive(): child.wait() return i == 0
def chpasswd(path, oldpassword, newpassword): """Change password of a private key. """ if len(newpassword) != 0 and not len(newpassword) > 4: return False cmd = shlex.split('ssh-keygen -p') child = pexpect.spawn(cmd[0], cmd[1:]) i = child.expect(['Enter file in which the key is', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False child.sendline(path) i = child.expect( ['Enter old passphrase', 'Enter new passphrase', pexpect.EOF]) if i == 0: child.sendline(oldpassword) i = child.expect( ['Enter new passphrase', 'Bad passphrase', pexpect.EOF]) if i != 0: if child.isalive(): child.wait() return False elif i == 2: if child.isalive(): child.wait() return False child.sendline(newpassword) i = child.expect(['Enter same passphrase again', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False child.sendline(newpassword) child.expect(pexpect.EOF) if child.isalive(): return child.wait() == 0 return True
def genkey(path, password=''): """Generate a ssh key pair. """ cmd = shlex.split('ssh-keygen -t rsa') child = pexpect.spawn(cmd[0], cmd[1:]) i = child.expect(['Enter file in which to save the key', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False child.sendline(path) i = child.expect(['Overwrite', 'Enter passphrase', pexpect.EOF]) if i == 0: child.sendline('y') i = child.expect(['Enter passphrase', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False elif i == 2: if child.isalive(): child.wait() return False child.sendline(password) i = child.expect(['Enter same passphrase', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False child.sendline(password) child.expect(pexpect.EOF) if child.isalive(): return child.wait() == 0 return True
def export_database(pwd, dbname, exportpath): """Export database to a file. """ filename = '%s_%s.sql' % (dbname, time.strftime('%Y%m%d_%H%M%S')) filepath = os.path.join(exportpath, filename) if not valid_filename(filename): return False cmd = 'mysqldump -uroot -p %s' % dbname cmd = '/bin/bash -c "%s > %s"' % (cmd, filepath) child = pexpect.spawn(cmd) i = child.expect(['Enter password', pexpect.EOF]) if i == 1: if child.isalive(): child.wait() return False child.sendline(pwd) i = child.expect(['error', pexpect.EOF]) if child.isalive(): w = child.wait() return w == 0 return i != 0
def add(disk, size=''): """Add a new partition on a disk. If the size exceed the max available space the disk left, then the new partition will be created with the left space. A disk can have 4 partitions at max. True will return if create successfully, or else False will return. Example: fdisk.add('/dev/sdb') # use all of the space fdisk.add('/dev/sdb', '5G') # create a partition with at most 5G space """ try: cmd = shlex.split('fdisk \'%s\'' % disk) except: return False child = pexpect.spawn(cmd[0], cmd[1:]) i = child.expect(['(m for help)', 'Unable to open']) if i == 1: if child.isalive(): child.wait() return False rt = True partno_found = False partno = 1 while not partno_found: child.sendline('n') i = child.expect( ['primary partition', 'You must delete some partition']) if i == 1: break child.sendline('p') i = child.expect(['Partition number', 'Selected partition']) if i == 0: child.sendline('%d' % partno) i = child.expect(['First cylinder', '(m for help)']) if i == 0: partno_found = True partno += 1 if partno > 4: break if not partno_found: rt = False if rt: child.sendline('') child.expect('Last cylinder') child.sendline('+%s' % size) i = child.expect( ['(m for help)', 'Value out of range', 'Last cylinder']) if i == 1: child.sendline('') child.expect('(m for help)') elif i == 2: # wrong size input child.sendline('') child.expect('(m for help)') rt = False if rt: child.sendline('w') else: child.sendline('q') if child.isalive(): child.wait() return rt