-
-
Save geoffalday/2021517 to your computer and use it in GitHub Desktop.
# How to generate a secret key with Python | |
# via http://flask.pocoo.org/docs/quickstart/ | |
import os | |
os.urandom(24) |
For Python 2.X and Python 3.X
>>> import binascii
>>> binascii.hexlify(os.urandom(24))
b'0ccd512f8c3493797a23557c32db38e7d51ed74f14fa7580'
Please i need help here:
import binascii
binascii.hexlify(os.urandom(24))
b'0ccd512f8c3493797a23557c32db38e7d51ed74f14fa7580'
! /usr/bin/python
print("*********************************************************************")
print("Cisco IOU License Generator - Kal 2011, python port of 2006 C version")
print("Modified to work with python3 by c_d 2014")
import os
import socket
import hashlib
import struct
get the host id and host name to calculate the hostkey
hostid=os.popen("hostid").read().strip()
hostname = socket.gethostname()
ioukey=int(hostid,16)
for x in hostname:
ioukey = ioukey + ord(x)
print"hostid=" + hostid +", hostname="+ hostname + ", ioukey=" + hex(ioukey)[2:]
create the license using md5sum
iouPad1='\x4B\x58\x21\x81\x56\x7B\x0D\xF3\x21\x43\x9B\x7E\xAC\x1D\xE6\x8A'
iouPad2='\x80' + 39*b'\0'
md5input=iouPad1 + iouPad2 + struct.pack('!L', ioukey) + iouPad1
iouLicense=hashlib.md5(md5input).hexdigest()[:16]
print("\nAdd the following text to ~/.iourc:")
print("[license]\n" + hostname + " = " +iouLicense + ";\n")
print("You can disable the phone home feature with something like:")
print(" echo '127.0.0.127 xml.cissco.com' >> /etc/hosts\n")
When this code was run at gns3 iouvm:
gns3 ssh mode here
gns3@gns3vm:/opt/gns3/images/IOU$
gns3@gns3vm:/opt/gns3/images/IOU$
gns3@gns3vm:/opt/gns3/images/IOU$ python3 CiscoIOUKeygen.py
File "CiscoIOUKeygen.py", line 18
print"hostid=" + hostid +", hostname="+ hostname + ", ioukey=" + hex(ioukey)[2:]
^
SyntaxError: invalid syntax
gns3@gns3vm:/opt/gns3/images/IOU$
Please i need solution.
Thank you.
@emma-yemi The Syntax error is because in python3 print
is a function, not a statement. You need to enclose the params inside parenthesis. Look at the print
function few lines ago like :
print("\nAdd the following text to ~/.iourc:")
Notice the usage of ()
So if you change line 18 as follows, the error will go away :
print("hostid= {} , hostname={} , ioukey={}".format(hostid, hostname, hex(ioukey)[2:])
great!
greet, thanks!
Try this those who are using pyth ver 3..really hats off this man
https://www.ipvanquish.com/2016/09/25/how-to-generate-cisco-iou-licence-on-gns3-vm-with-python-3/
In python3.6 secrets module can be used to generate secrets.
>>> secrets.token_hex(16)
'f9bf78b9a18ce6d46a0cd2b0b86df9da'
Example taken from the official documentation on secrets module.
Can the secrets module be installed in python 2.x?
@csolorio I checked for a backport of Python 3.6 stdlib secrets
and I couldn't find one. But if you look at cpython/secrets.py you may find it's a small enough module to backport on your own for your project. (Or just implement and test the same gist yourself.)
Hello
Traceback (most recent call last):
File "manage.py", line 9, in
from solarpi.app import create_app
File "/home/amani/Desktop/new/solarpi/solarpi/app.py", line 10, in
from solarpi.settings import ProdConfig
File "/home/amani/Desktop/new/solarpi/solarpi/settings.py", line 7, in
class Config(object):
File "/home/amani/Desktop/new/solarpi/solarpi/settings.py", line 8, in Config
SECRET_KEY = os_env['SOLARPI_SECRET']
File "/home/amani/Desktop/new/.venv/lib/python2.7/UserDict.py", line 40, in getitem
raise KeyError(key)
KeyError: 'SOLARPI_SECRET'
and the following is my setting .py
mport os
os_env = os.environ
class Config(object):
SECRET_KEY = os_env['SOLARPI_SECRET']
APP_DIR = os.path.abspath(os.path.dirname(file)) # This directory
PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir))
BCRYPT_LOG_ROUNDS = 13
ASSETS_DEBUG = True
DEBUG_TB_ENABLED = True # Disable Debug toolbar
DEBUG_TB_INTERCEPT_REDIRECTS = True
CACHE_TYPE = 'simple' # Can be "memcached", "redis", etc.
SQLALCHEMY_TRACK_MODIFICATIONS = False
class ProdConfig(Config):
"""Production configuration."""
ENV = 'prod'
DEBUG = False
DB_NAME = 'app.db'
DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(DB_PATH)
DEBUG_TB_ENABLED = False # Disable Debug toolbar
SENTRY_DNS = os_env.get('SENTRY_DNS', None)
class DevConfig(Config):
"""Development configuration."""
ENV = 'dev'
DEBUG = True
DB_NAME = 'dev.db'
DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(DB_PATH)
DEBUG_TB_ENABLED = True
ASSETS_DEBUG = True # Don't bundle/minify static assets
CACHE_TYPE = 'simple' # Can be "memcached", "redis", etc.
class TestConfig(Config):
TESTING = True
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///dev.db'
BCRYPT_LOG_ROUNDS = 1 # For faster tests
WTF_CSRF_ENABLED = False # Allows form testing
any idea how i can solve this
For Python 2.X and Python 3.X
>>> import binascii >>> binascii.hexlify(os.urandom(24)) b'0ccd512f8c3493797a23557c32db38e7d51ed74f14fa7580'
Did you get any solution for this. I too facing the same issue
You can add some formatting too (Python 2.7):