Created
November 4, 2022 13:08
-
-
Save chr5tphr/a74e85c089214d82e508e65300ca517d to your computer and use it in GitHub Desktop.
clipsock
This file contains 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 argparse import ArgumentParser | |
from os import unlink, chmod | |
from os.path import expanduser | |
from socket import socket, AF_UNIX | |
from subprocess import run, Popen | |
from sys import stderr | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument('-R', '--remote', type=str, default='remote') | |
parser.add_argument('-s', '--socket-path', type=str, default=expanduser('~/.clipsock')) | |
parser.add_argument('-r', '--remote-socket-path', type=str, default=expanduser('~/.clipsock')) | |
args = parser.parse_args() | |
spath = args.socket_path | |
rspath = args.remote_socket_path | |
try: | |
with Popen(['ssh', '-NR', f'{rspath}:{spath}', args.remote]) as proc, socket(AF_UNIX) as sock: | |
try: | |
chmod(sock.fileno(), 0o700) | |
sock.bind(spath) | |
sock.listen(1) | |
while True: | |
conn, addr = sock.accept() | |
with conn: | |
fd = conn.makefile('r') | |
run(['wl-copy', '-p'], stdin=fd) | |
finally: | |
unlink(spath) | |
proc.kill() | |
except KeyboardInterrupt: | |
print('Exiting...', file=stderr) | |
finally: | |
run(['ssh', args.remote, f'rm {rspath}']) | |
if __name__ == '__main__': | |
main() |
This file contains 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 argparse import ArgumentParser | |
from os.path import expanduser | |
from socket import socket, AF_UNIX | |
from sys import stderr, stdin | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument('-n', '--strip-newline', action='store_true') | |
parser.add_argument('-s', '--socket-path', type=str, default=expanduser('~/.clipsock')) | |
args = parser.parse_args() | |
try: | |
with socket(AF_UNIX) as sock: | |
sock.connect(args.socket_path) | |
data = stdin.buffer.read() | |
if args.strip_newline: | |
data = data.strip() | |
sock.sendall(data) | |
except KeyboardInterrupt: | |
print('Exiting...', file=stderr) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment