123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- demeter core
- name:demeter.py
- author:rabin
- """
- import pexpect
- from pexpect import replwrap, EOF
- class MysqlWrapper(replwrap.REPLWrapper):
- def __init__(self, cmd_or_spawn, orig_prompt, prompt_change,
- extra_init_cmd=None, line_output_callback=None):
- self.line_output_callback = line_output_callback
- replwrap.REPLWrapper.__init__(self, cmd_or_spawn, orig_prompt,
- prompt_change, extra_init_cmd=extra_init_cmd)
- def _expect_prompts(self, timeout=-1):
- if timeout == None:
- while True:
- pos = self.child.expect_exact([self.prompt, self.continuation_prompt, u'\r\n'],
- timeout=None)
- if pos == 2:
- self.line_output_callback(self.child.before)
- else:
- if len(self.child.before) != 0:
- self.line_output_callback(self.child.before)
- break
- else:
- pos = replwrap.REPLWrapper._expect_prompt(self, timeout=timeout)
- return pos
- def output(content):
- print content
- if __name__ == '__main__':
- user = 'root'
- ip = '192.168.15.10'
- port = '3309'
- password = '123456'
- child = pexpect.spawn('mysql -u%s -h%s -P%s -p%s' % (user, ip, port,password))
- py = MysqlWrapper(child, u"mysql>", None,extra_init_cmd=None,line_output_callback=output)
- #py.run_command("show databases;", timeout=None)
- outout = py.run_command("use dever;select * from plant_forum_posts;", timeout=None)
- print py.child.before
- """
- child = pexpect.spawn('mysql -u%s -h%s -P%s -p' % (user, ip, port))
- child.expect ('password:')
- child.sendline (password)
-
- child.expect('mysql>')
- child.sendline('show databases;')
- child.expect('mysql>')
- child.sendline('use dever;')
- print child.after # Print the result of the ls command.
- #child.interact() # Give control of the child to the user.
- """
|