sqlmap/lib/core/shell.py

80 lines
2.6 KiB
Python
Raw Normal View History

2008-10-15 19:38:22 +04:00
#!/usr/bin/env python
"""
2012-07-12 21:38:03 +04:00
Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
2008-10-15 19:38:22 +04:00
"""
import atexit
import os
import rlcompleter
from lib.core import readlineng as readline
from lib.core.common import Backend
2011-01-27 15:38:39 +03:00
from lib.core.data import logger
2008-10-15 19:38:22 +04:00
from lib.core.data import paths
from lib.core.enums import OS
2008-10-15 19:38:22 +04:00
def saveHistory():
historyPath = os.path.expanduser(paths.SQLMAP_HISTORY)
readline.write_history_file(historyPath)
def loadHistory():
historyPath = os.path.expanduser(paths.SQLMAP_HISTORY)
if os.path.exists(historyPath):
2011-01-27 15:38:39 +03:00
try:
readline.read_history_file(historyPath)
except IOError, msg:
2011-01-30 19:34:13 +03:00
warnMsg = "there was a problem loading the history file '%s' (%s)" % (historyPath, msg)
2011-01-27 15:38:39 +03:00
logger.warn(warnMsg)
2008-10-15 19:38:22 +04:00
class CompleterNG(rlcompleter.Completer):
def global_matches(self, text):
"""
Compute matches when text is a simple name.
Return a list of all names currently defined in self.namespace
that match.
"""
matches = []
n = len(text)
for ns in [ self.namespace ]:
for word in ns:
2008-10-15 19:38:22 +04:00
if word[:n] == text:
matches.append(word)
return matches
def autoCompletion(sqlShell=False, osShell=False):
# First of all we check if the readline is available, by default
# it is not in Python default installation on Windows
2011-01-15 15:53:40 +03:00
if not readline._readline:
2008-10-15 19:38:22 +04:00
return
if osShell:
if Backend.isOs(OS.WINDOWS):
# Reference: http://en.wikipedia.org/wiki/List_of_DOS_commands
completer = CompleterNG({
"copy": None, "del": None, "dir": None,
"echo": None, "md": None, "mem": None,
"move": None, "net": None, "netstat -na": None,
"ver": None, "xcopy": None, "whoami": None,
})
else:
# Reference: http://en.wikipedia.org/wiki/List_of_Unix_commands
completer = CompleterNG({
"cp": None, "rm": None, "ls": None,
"echo": None, "mkdir": None, "free": None,
"mv": None, "ifconfig": None, "netstat -natu": None,
"pwd": None, "uname": None, "id": None,
})
2008-10-15 19:38:22 +04:00
readline.set_completer(completer.complete)
readline.parse_and_bind("tab: complete")
2008-10-15 19:38:22 +04:00
loadHistory()
atexit.register(saveHistory)