Skip to content

Instantly share code, notes, and snippets.

@eNV25
Created February 6, 2021 14:31
Show Gist options
  • Save eNV25/80b911079d383da0392f6e60a7ceb5d5 to your computer and use it in GitHub Desktop.
Save eNV25/80b911079d383da0392f6e60a7ceb5d5 to your computer and use it in GitHub Desktop.
adds CTRL-C handler and error messages to stderr
diff --git a/wpa-psk.py b/wpa-psk.py
index 0881ae3..9089caf 100644
--- a/wpa-psk.py
+++ b/wpa-psk.py
@@ -4,6 +4,12 @@ import sys
from argparse import ArgumentParser
from getpass import getpass
from hashlib import pbkdf2_hmac
+from signal import signal, SIGINT
+
+def die(*_, **__):
+ sys.exit()
+
+signal = signal(SIGINT, die)
iwd = """[Security]
PreSharedKey={psk}"""
@@ -35,14 +41,14 @@ parser.add_argument(
args = parser.parse_args()
if not args.passphrase:
- print("# reading passphrase from stdin")
+ print("# reading passphrase from stdin", file=sys.stderr)
args.passphrase = getpass(prompt="")
if not 8 <= len(args.passphrase) <= 63:
- print("Passphrase must be 8..63 characters")
+ print("Passphrase must be 8..63 characters", file=sys.stderr)
sys.exit(1)
passphrase = args.passphrase.encode()
if any(b < 32 or b == 127 for b in passphrase):
- print("Invalid passphrase character")
+ print("Invalid passphrase character", file=sys.stderr)
sys.exit(1)
ssid = args.ssid.encode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment