41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
"""Release-only SSH helper; uses the existing key and verified known_hosts."""
|
|
import argparse
|
|
from pathlib import Path
|
|
import socket
|
|
import sys
|
|
import paramiko
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--run')
|
|
parser.add_argument('--upload', nargs=2)
|
|
parser.add_argument('--download', nargs=2)
|
|
args = parser.parse_args()
|
|
sock = socket.create_connection(('192.168.110.111', 22), timeout=15,
|
|
source_address=('192.168.123.43', 0))
|
|
client = paramiko.SSHClient()
|
|
client.load_host_keys(str(Path.home() / '.ssh/known_hosts'))
|
|
client.set_missing_host_key_policy(paramiko.RejectPolicy())
|
|
client.connect('192.168.110.111', username='ps', sock=sock,
|
|
key_filename=str(Path.home() / '.ssh/id_ed25519'),
|
|
look_for_keys=False, allow_agent=False, timeout=15)
|
|
try:
|
|
if args.upload or args.download:
|
|
with client.open_sftp() as sftp:
|
|
if args.upload:
|
|
sftp.put(*args.upload)
|
|
else:
|
|
sftp.get(*args.download)
|
|
print('Transfer complete.', flush=True)
|
|
if args.run:
|
|
command = Path(args.run).read_text(encoding='utf-8-sig').replace('\r\n', '\n')
|
|
stdin, stdout, stderr = client.exec_command('bash -se', get_pty=False)
|
|
stdout.channel.set_combine_stderr(True)
|
|
stdin.write(command)
|
|
stdin.flush()
|
|
stdin.channel.shutdown_write()
|
|
for line in stdout:
|
|
print(line, end='', flush=True)
|
|
sys.exit(stdout.channel.recv_exit_status())
|
|
finally:
|
|
client.close()
|