1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
from core.vectors import PhpCode, ModuleExec
from core.module import Module
from core import messages
from core.loggers import log
import random
class Cd(Module):
"""Change current working directory."""
aliases = [ 'cd' ]
def init(self):
self.register_info(
{
'author': [
'Emilio Pinna'
],
'license': 'GPLv3'
}
)
self.register_arguments([
{ 'name' : 'dir', 'help' : 'Target folder', 'nargs' : '?' }
])
def run(self, **kwargs):
# When no folder is specified, change folder to SCRIPT_NAME to
# simulate the bash behaviour. If not available, use current dir.
if not self.args.get('dir'):
script_folder = ModuleExec(
'system_info',
[ '-info', 'script_folder' ]
).load_result_or_run(
result_name = 'script_folder'
)
self.args['dir'] = script_folder if script_folder else '.'
# The execution and result storage is done manually cause
# no result has to be stored if the execution fails. This
# is not simple to implement using
# self.vectors.get_result(.., store_result).
folder = PhpCode("""@chdir('${dir}')&&print(@getcwd());""", "chdir").run(
self.args
)
if folder:
self._store_result('cwd', folder)
else:
log.warning(
messages.module_file_cd.failed_directory_change_to_s %
(self.args['dir'])
)
def run_alias(self, line, cmd):
# Run this alias independently from the shell_sh status
return self.run_cmdline(line)
|