frappy-cli: connect to servers on localhost by default

--scan option: specify where to scan if not on localhost

Change-Id: I51a694eb3cb045e7d18c19a332db8e6ba063009b
This commit is contained in:
2025-06-02 15:22:00 +02:00
parent 745e15c709
commit c0f6569f1b
3 changed files with 117 additions and 88 deletions

View File

@ -24,12 +24,14 @@
import sys
import argparse
import socket
from pathlib import Path
# Add import path for inplace usage
sys.path.insert(0, str(Path(__file__).absolute().parents[1]))
from frappy.client.interactive import init, run, clientenv, interact
from frappy.protocol.discovery import scan
def parseArgv(argv):
@ -37,6 +39,9 @@ def parseArgv(argv):
parser.add_argument('-i', '--include',
help='file to execute after connecting to the clients', metavar='file',
type=Path, action='append', default=[])
parser.add_argument('-s', '--scan',
help='hosts to scan for (-s subnet for all nodes in subnet)',
action='append', default=[])
parser.add_argument('-o', '--only-execute',
help='Do not go into interactive mode after executing files. \
Has no effect without --include.', action='store_true')
@ -46,9 +51,38 @@ def parseArgv(argv):
return parser.parse_args(argv)
def own_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(('10.254.254.254', 1))
return s.getsockname()[0]
except Exception:
return '127.0.0.1'
finally:
s.close()
args = parseArgv(sys.argv[1:])
success = init(*args.node)
nodes = args.node
hosts = args.scan
if not nodes and not hosts:
host = ['localhost']
if hosts:
answers = []
for host in hosts:
ans = scan()
if host == 'subnet': # all in subnet
answers.extend(ans)
else: # filter by ip
ip = socket.gethostbyname(host)
if ip == '127.0.0.1':
ip = own_ip()
answers.extend(a for a in ans if a.address == ip)
nodes.extend(f'{h.hostname}:{h.port}' for h in answers)
success = init(*nodes)
run_error = ''
file_success = False