I'm not sure why you need opcodes. But if for exploit development you probably already have metasploit, which comes with a really useful ruby script called nasm_shell.rb (in the tools directory).
Each line you enter is displayed as a hexadecimal representation of the ascii correct operation codes.
If this is for some other purpose or you don’t want any heavyweight toolset like metasploit to freeze for any reason, you can just pull out the script and install its dependencies. It uses Rex and assumes nasm is installed.
If you want to adapt it, you only need a few lines in the shell.run function
source share