3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
20 class CatFileException(Exception):
23 class CatFile(object):
24 def __init__(self, bmc_host, bmc_user, bmc_password, bmc_priv_level, login_user, login_password):
27 self._password = bmc_password
28 self._priv_level = bmc_priv_level
31 self._login_user = login_user
32 self._login_password = login_password
34 def _open_console(self, log):
35 logging.info('Open SOL console')
37 logging.debug('deactivate sol')
38 expect_session = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} -L {} sol deactivate'.format(self._host, self._user, self._password, self._priv_level))
39 expect_session.expect(pexpect.EOF)
41 logging.debug('activate sol, output will go to %s', log)
42 self._sol = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} -L {} sol activate'.format(self._host, self._user, self._password, self._priv_level), timeout=None)
43 logfile = open(log, 'wb')
44 self._sol.logfile_read = logfile
46 def _close_console(self):
47 logging.info('Close SOL console')
50 logging.debug('Logout from host')
51 self._sol.sendline('logout\r\n')
53 self._sol.expect('login:', timeout=10)
56 logging.debug('deactivate sol')
57 session = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} -L {} sol deactivate'.format(self._host, self._user, self._password, self._priv_level))
58 session.expect(pexpect.EOF)
60 def _expect_cmd_prompt(self):
61 self._sol.expect('# ', timeout=10)
64 logging.info('Login to host')
69 self._expect_cmd_prompt()
70 logging.debug('Command prompt found')
73 except pexpect.TIMEOUT as e:
79 self._sol.expect('login:', timeout=10)
80 logging.debug('Login prompt found')
82 self._sol.sendline(self._login_user)
84 self._sol.expect('Password:', timeout=10)
85 logging.debug('Password prompt found')
87 self._sol.sendline(self._login_password)
90 self._expect_cmd_prompt()
91 logging.debug('Command prompt found')
92 except pexpect.TIMEOUT as e:
96 def _cat_log(self, path, timeout=120):
97 logging.debug('Catting %s', path)
99 self._sol.sendline('cat {}; echo CONSOLE_CAT_DONE\r\n'.format(path))
100 self._sol.expect('CONSOLE_CAT_DONE', timeout=timeout)
101 logging.debug('Catting done')
103 self._expect_cmd_prompt()
105 def cat(self, path, log_file, timeout=None):
107 self._open_console(log_file)
109 self._cat_log(path, timeout)
110 self._close_console()
111 except Exception as ex:
112 logging.warn('Cat file failed: %s', str(ex))
113 raise CatFileException(str(ex))
116 parser = argparse.ArgumentParser()
117 parser.add_argument('-H', '--bmc_host', required=True, help='BMC host')
118 parser.add_argument('-U', '--bmc_user', required=True, help='BMC user')
119 parser.add_argument('-P', '--bmc_password', required=True, help='BMC user password')
120 parser.add_argument('-L', '--bmc_priv_level', required=False, default='ADMINISTRATOR', help='BMC user privilege level')
121 parser.add_argument('-u', '--user', required=True, help='Login user')
122 parser.add_argument('-p', '--password', required=True, help='Login user password')
123 parser.add_argument('-f', '--file', required=True, help='File path to cat')
124 parser.add_argument('-o', '--output_file', required=True, help='Output file name of the log')
125 parser.add_argument('-t', '--timeout', required=False, help='Timeout for catting the file')
127 args = parser.parse_args()
129 logging.basicConfig(level=logging.DEBUG)
131 cat_file = CatFile(args.bmc_host, args.bmc_user, args.bmc_password, args.bmc_priv_level, args.user, args.password)
132 cat_file.cat(args.file, args.output_file, args.password)
134 if __name__ == "__main__":