Last active
February 7, 2024 16:29
-
-
Save st1vms/6a63fd3bbcba2fb506295a41c6dc5513 to your computer and use it in GitHub Desktop.
ScriptingTools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Collection of scripting tools for linux and python |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# If file doesn't exists | |
if [[ -z "$1" || ! -f "$1" ]] ; then | |
echo "File does not exists!" | |
exit -1 | |
fi | |
# Check if text is non-empty | |
if [[ -z "$(cat $1)" ]] ; then | |
echo "String must not be empty!" | |
exit -1 | |
fi | |
# Perform clipboard redirection and wait for a single paste-action | |
cat "$1" | /usr/bin/xclip -quiet -i "$1" -r -l 2 -selection clipboard &>/dev/null | |
# Clear clipboard content | |
echo -n | /usr/bin/xclip -i -l 1 -selection clipboard | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# pip install sounddevice | |
# sudo apt-get install libportaudio2 | |
"""Play a sine signal.""" | |
import argparse | |
import sys | |
import time | |
import numpy as np | |
import sounddevice as sd | |
def int_or_str(text): | |
"""Helper function for argument parsing.""" | |
try: | |
return int(text) | |
except ValueError: | |
return text | |
parser = argparse.ArgumentParser(add_help=False) | |
parser.add_argument( | |
'-l', '--list-devices', action='store_true', | |
help='show list of audio devices and exit') | |
args, remaining = parser.parse_known_args() | |
if args.list_devices: | |
print(sd.query_devices()) | |
parser.exit(0) | |
parser = argparse.ArgumentParser( | |
description=__doc__, | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
parents=[parser]) | |
parser.add_argument( | |
'frequency', nargs='?', metavar='FREQUENCY', type=float, default=500, | |
help='frequency in Hz (default: %(default)s)') | |
parser.add_argument( | |
'-d', '--device', type=int_or_str, | |
help='output device (numeric ID or substring)') | |
parser.add_argument( | |
'-a', '--amplitude', type=float, default=0.2, | |
help='amplitude (default: %(default)s)') | |
parser.add_argument( | |
'-t', '--time', type=float, default=1, | |
help='time duration for sin sound (default: %(default)s)') | |
args = parser.parse_args(remaining) | |
start_idx = 0 | |
try: | |
samplerate = sd.query_devices(args.device, 'output')['default_samplerate'] | |
def callback(outdata, frames, time, status): | |
if status: | |
print(status, file=sys.stderr) | |
global start_idx | |
t = (start_idx + np.arange(frames)) / samplerate | |
t = t.reshape(-1, 1) | |
outdata[:] = args.amplitude * np.sin(2 * np.pi * args.frequency * t) | |
start_idx += frames | |
with sd.OutputStream(device=args.device, channels=1, callback=callback, | |
samplerate=samplerate): | |
print(f'Playing {args.frequency} Hz for {args.time} seconds', end='\r') | |
time.sleep(args.time) | |
except KeyboardInterrupt: | |
parser.exit('') | |
except Exception as e: | |
parser.exit(type(e).__name__ + ': ' + str(e)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import subprocess | |
import time, sys, string,os | |
import argparse | |
if os.geteuid() != 0: | |
print(f"This script must be run as root") | |
sys.exit(0) | |
DEFAULT_DELAY_SEC = 5 | |
DO_PRINT_ENTER = False | |
_YDOTOOL_SOCKET_PATH = "/run/user/1000/.ydotool_socket" | |
os.environ["YDOTOOL_SOCKET"] = "/run/user/1000/.ydotool_socket" | |
_SOCK_P: subprocess.Popen = None | |
def open_subp(text: str, no_output:bool=False) -> subprocess.Popen: | |
if no_output: | |
return subprocess.Popen(text, shell=True, | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.STDOUT) | |
return subprocess.Popen(text, shell=True) | |
def init_dev() -> bool: | |
global _SOCK_P | |
try: | |
_SOCK_P = open_subp(f"ydotoold -m -p {_YDOTOOL_SOCKET_PATH} -o 1000:1000", no_output=True) | |
return True | |
except Exception as e: | |
print(str(e)) | |
_SOCK_P = None | |
return False | |
def deinit_dev(): | |
try: | |
global _SOCK_P | |
if _SOCK_P != None: | |
# Kills daemon | |
_SOCK_P.kill() | |
_SOCK_P.wait() | |
# Removes socket | |
os.unlink(_YDOTOOL_SOCKET_PATH) | |
open_subp(f"killall ydotoold", no_output=True).wait() | |
except Exception as e: | |
print(str(e)) | |
_SOCK_P = None | |
def type(text: str): | |
open_subp(f"ydotool type {text}").wait() | |
def send_enter(): | |
open_subp(f"ydotool key 28:1 28:0").wait() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(add_help=False) | |
parser.add_argument( | |
"-e", | |
"--doenter", | |
action="store_true", | |
help="wheter to issue an enter key after typing completed", | |
) | |
args, remaining = parser.parse_known_args() | |
DO_PRINT_ENTER = args.doenter | |
parser = argparse.ArgumentParser( | |
description=__doc__, | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
parents=[parser], | |
) | |
parser.add_argument( | |
"-d", | |
"--delay", | |
nargs="?", | |
metavar="DELAY", | |
type=int, | |
default=0, | |
help="time delay before input events (default: %(default)s)", | |
) | |
parser.add_argument( | |
"-t", | |
"--text", | |
nargs="?", | |
metavar="TEXT", | |
type=str, | |
required=True, | |
help="text to be typed", | |
) | |
args = parser.parse_args(remaining) | |
assert args.delay >= 0 | |
t = "".join(c for c in args.text if c in string.printable) | |
if len(t) == 0: | |
sys.exit(0) | |
if not init_dev(): | |
sys.exit(0) | |
try: | |
time.sleep(args.delay) | |
type(t) | |
if DO_PRINT_ENTER: | |
send_enter() | |
sys.exit(1) | |
except Exception as e: | |
print(str(e)) | |
sys.exit(0) | |
finally: | |
deinit_dev() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from sys import exit as sys_exit | |
from string import ascii_lowercase as al | |
from string import ascii_uppercase as au | |
from string import digits as dig | |
from random import choice | |
def main(): | |
N = int(input("How many passwords?\n>>")) | |
if N <= 0: | |
return 1 | |
C = int(input("How many characters?\n>>")) | |
if C <= 0: | |
return 1 | |
for _ in range(N): | |
print(''.join(choice(f"{al}{au}{dig}@.:-_#!$&=+*") for _ in range(C))) | |
if __name__ == "__main__": | |
_e = main() | |
input("\n\nPress Enter to quit...") | |
sys_exit(_e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment