|
#! /usr/bin/python3 |
|
|
|
# Copyright (C) 2018 Karim Kanso. All Rights Reserved. |
|
# |
|
# This program is free software: you can redistribute it and/or modify |
|
# it under the terms of the GNU General Public License as published by |
|
# the Free Software Foundation, either version 3 of the License, or |
|
# (at your option) any later version. |
|
# |
|
# This program is distributed in the hope that it will be useful, |
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
# GNU General Public License for more details. |
|
# |
|
# You should have received a copy of the GNU General Public License |
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
|
import sys |
|
|
|
words = 8 |
|
|
|
def err() : |
|
print("Usage: ", sys.argv[0], " ", end="", file=sys.stderr) |
|
for i in range(words) : |
|
print("hh ll ", end="", file=sys.stderr) |
|
print("", file=sys.stderr) |
|
print("Calculates the cookie for cisco routers, copy the {} words from the cookie onto the command line.\n\thh = high byte in hex\n\tll = low byte in hex".format(words)) |
|
sys.exit(1) |
|
|
|
def main() : |
|
if len(sys.argv) != words * 2 + 1 : |
|
err() |
|
accumulator = 0 |
|
try : |
|
for i in range(words) : |
|
h = int(sys.argv[1 + i * 2], base=16) |
|
l = int(sys.argv[(i+1) * 2], base=16) |
|
w = l + (h << 8) |
|
accumulator += w |
|
except ValueError as ve : |
|
print("Unable to process cookie value: {}".format(ve), file=sys.stderr) |
|
err() |
|
result = "{0:x}".format(accumulator) |
|
if len(result) < 4 : |
|
print("Error, failed to calculate accumulator of sufficient size: {}".format(result), file=sys.stderr) |
|
err() |
|
print("Done, result: ", result[-4:]) |
|
|
|
if __name__ == "__main__" : |
|
main() |