commit aece66a1d1016d26c1f05c6fbded7753b6c36d26
| author | 斟酌 鵬兄 <tgckpg@gmail.com> |
| date | 2025-04-25T06:22:40Z |
| subject | Migrate pwgen.py to bash |
commit aece66a1d1016d26c1f05c6fbded7753b6c36d26
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date: 2025-04-25T06:22:40Z
Migrate pwgen.py to bash
---
bash/sources/12_shortcuts | 14 ++++++++++++++
python/pwgen.py | 32 --------------------------------
2 files changed, 14 insertions(+), 32 deletions(-)
diff --git a/bash/sources/12_shortcuts b/bash/sources/12_shortcuts
index 0b90369..16c69b7 100755
--- a/bash/sources/12_shortcuts
+++ b/bash/sources/12_shortcuts
@@ -17,3 +17,17 @@ function kres {
kubectl get $1 -A $_O | grep "$_D*$2$_D*\s\+$_D*$3$_D*"
fi
}
+
+function pwgen {
+ local _LEN=$1
+ case $_LEN in
+ ''|*[!0-9]*)
+ echo "Enter a valid number" > /dev/stderr
+ return 1
+ ;;
+ esac
+ LC_ALL=C tr -dc '[:graph:]' </dev/urandom | head -c $_LEN
+ if [ -t 1 ]; then
+ echo
+ fi
+}
diff --git a/python/pwgen.py b/python/pwgen.py
deleted file mode 100755
index 7422d95..0000000
--- a/python/pwgen.py
+++ /dev/null
@@ -1,32 +0,0 @@
-#!/usr/bin/env python3
-
-import string;
-import random;
-
-import os;
-import sys;
-
-
-if len(sys.argv) < 2:
- sys.exit('Usage: ' + os.path.basename(__file__) + ' length');
-
-l = sys.argv[1];
-
-if not l.isdigit():
- sys.exit('"' + l + '" is not a valid length.');
-
-# cast l into intergers
-l = int(l);
-
-# define the character set
-charSet = string.ascii_letters + string.digits;
-
-# generate stack
-out_stack = ''.join(random.choice(charSet) for x in range(l));
-
-# Am I being piped?
-if sys.stdout.isatty():
- print(out_stack);
-else:
- sys.stdout.write(out_stack);
-