如何从登录bash历史记录中删除通过pexpect执行的命令

时间:2019-04-30 06:33:04

标签: python bash ssh pexpect

我正在使用pexpect远程登录到某些计算机并执行一些命令。我需要从登录到该计算机的bash历史记录文件中删除这些命令。我可以通过在命令前面放置一个空格来删除命令(典型方法:p),但是库中仍然打印了一些剩余的日志行。我也需要删除它。是否有任何库级别配置可删除以静默方式登录的命令?

if not (hostname or username or password or commands):
            logging.debug(
                "Skip running remote commands args are empty")
            return None
        from pexpect import pxssh
        results = []
        server = pxssh.pxssh()
        server.login(hostname, username, password)
        for command in commands:
            server.sendline(command)  # run a command
            server.prompt()  # match the prompt
            output = server.before
            result = output.replace(command, '').strip()
            results.append(result)
        server.logout()
        return results
// bash history output
 1966  29/04/19 14:14:17 unset PROMPT_COMMAND
 1967  29/04/19 14:14:17 PS1='[PEXPECT]\$ '
 1968  29/04/19 14:14:17 exit
 1969  29/04/19 15:10:27 unset PROMPT_COMMAND
 1970  29/04/19 15:10:27 PS1='[PEXPECT]\$ '
 1971  29/04/19 15:10:28 exit
 1972  29/04/19 15:14:58 unset PROMPT_COMMAND
 1973  29/04/19 15:14:59 PS1='[PEXPECT]\$ '
 1974  29/04/19 15:14:59 exit
 1975  29/04/19 16:10:47 unset PROMPT_COMMAND
 1976  29/04/19 16:10:47 PS1='[PEXPECT]\$ '
 1977  29/04/19 16:10:47 exit
 1978  29/04/19 16:15:40 unset PROMPT_COMMAND
 1979  29/04/19 16:15:40 PS1='[PEXPECT]\$ '
 1980  29/04/19 16:15:40 exit
 1981  29/04/19 17:11:08 unset PROMPT_COMMAND
 1982  29/04/19 17:11:08 PS1='[PEXPECT]\$ '
 1983  29/04/19 17:11:08 exit
 1984  29/04/19 17:16:24 unset PROMPT_COMMAND
 1985  29/04/19 17:16:24 PS1='[PEXPECT]\$ '
 1986  29/04/19 17:16:25 exit
 1987  29/04/19 18:11:28 unset PROMPT_COMMAND
 1988  29/04/19 18:11:28 PS1='[PEXPECT]\$ '
 1989  29/04/19 18:11:28 exit
 1990  29/04/19 18:17:22 unset PROMPT_COMMAND
 1991  29/04/19 18:17:22 PS1='[PEXPECT]\$ '
 1992  29/04/19 18:17:23 exit
 1993  29/04/19 19:11:50 unset PROMPT_COMMAND
 1994  29/04/19 19:11:50 PS1='[PEXPECT]\$ '
 1995  29/04/19 19:11:51 exit
 1996  29/04/19 19:18:04 unset PROMPT_COMMAND
 1997  29/04/19 19:18:04 PS1='[PEXPECT]\$

0 个答案:

没有答案