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, login_user, login_password):
27 self._password = bmc_password
30 self._login_user = login_user
31 self._login_password = login_password
33 def _open_console(self, log):
34 logging.info('Open SOL console')
36 logging.debug('deactivate sol')
37 expect_session = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} sol deactivate'.format(self._host, self._user, self._password))
38 expect_session.expect(pexpect.EOF)
40 logging.debug('activate sol, output will go to %s', log)
41 self._sol = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} sol activate'.format(self._host, self._user, self._password), timeout=None)
42 logfile = open(log, 'wb')
43 self._sol.logfile_read = logfile
45 def _close_console(self):
46 logging.info('Close SOL console')
49 logging.debug('Logout from host')
50 self._sol.sendline('logout\r\n')
52 self._sol.expect('login:', timeout=10)
55 logging.debug('deactivate sol')
56 session = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} sol deactivate'.format(self._host, self._user, self._password))
57 session.expect(pexpect.EOF)
59 def _expect_cmd_prompt(self):
60 self._sol.expect('# ', timeout=10)
63 logging.info('Login to host')
68 self._expect_cmd_prompt()
69 logging.debug('Command prompt found')
72 except pexpect.exceptions.TIMEOUT as e:
78 self._sol.expect('login:', timeout=10)
79 logging.debug('Login prompt found')
81 self._sol.sendline(self._login_user)
83 self._sol.expect('Password:', timeout=10)
84 logging.debug('Password prompt found')
86 self._sol.sendline(self._login_password)
89 self._expect_cmd_prompt()
90 logging.debug('Command prompt found')
91 except pexpect.exceptions.TIMEOUT as e:
95 def _cat_log(self, path, timeout=120):
96 logging.debug('Catting %s', path)
98 self._sol.sendline('cat {}; echo CONSOLE_CAT_DONE\r\n'.format(path))
99 self._sol.expect('CONSOLE_CAT_DONE', timeout=timeout)
100 logging.debug('Catting done')
102 self._expect_cmd_prompt()
104 def cat(self, path, log_file, timeout=None):
106 self._open_console(log_file)
108 self._cat_log(path, timeout)
109 self._close_console()
110 except Exception as ex:
111 logging.warn('Cat file failed: %s', str(ex))
112 raise CatFileException(str(ex))
115 parser = argparse.ArgumentParser()
116 parser.add_argument('-H', '--bmc_host', required=True, help='BMC host')
117 parser.add_argument('-U', '--bmc_user', required=True, help='BMC user')
118 parser.add_argument('-P', '--bmc_password', required=True, help='BMC user password')
119 parser.add_argument('-u', '--user', required=True, help='Login user')
120 parser.add_argument('-p', '--password', required=True, help='Login user password')
121 parser.add_argument('-f', '--file', required=True, help='File path to cat')
122 parser.add_argument('-o', '--output_file', required=True, help='Output file name of the log')
123 parser.add_argument('-t', '--timeout', required=False, help='Timeout for catting the file')
125 args = parser.parse_args()
127 logging.basicConfig(level=logging.DEBUG)
129 cat_file = CatFile(args.bmc_host, args.bmc_user, args.bmc_password, args.user, args.password)
130 cat_file.cat(args.file, args.output_file, args.password)
132 if __name__ == "__main__":