Skip to content

Commit

Permalink
Implement logic to allow connecting via SSH by name #119
Browse files Browse the repository at this point in the history
User would just `ssh -t <consolepi address> -p 2202 <device name>`
The `-t` is key or picocom launches without a tty and tab completion
and some other things break.

`<device name>` is the name of the adapter.  Which should be an alias like
`r1-6300M-sw` but could also be `ttyUSB0`

currently extract the baud and connection settings from ser2net config.
  • Loading branch information
wade ~ Pack3tL0ss committed Jul 8, 2023
1 parent 9ca208c commit bc4193a
Showing 1 changed file with 52 additions and 28 deletions.
80 changes: 52 additions & 28 deletions src/remote_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import psutil
import sys
import subprocess
from time import sleep
import time
sys.path.insert(0, '/etc/ConsolePi/src/pypkg')
from consolepi import config, utils # type: ignore # NoQA
from consolepi.consolepi import ConsolePi # type: ignore # NoQA
Expand Down Expand Up @@ -40,33 +40,57 @@ def terminate_process(pid):
x += 1


def check_hung_process(cmd: str, device: str) -> int:
ppid = find_procs_by_name(cmd, device)
retry = 0
msg = f"\n{_device.replace('/dev/', '')} appears to be in use (may be a previous hung session)."
msg += '\nDo you want to Terminate the existing session'
if ppid is not None and utils.user_input_bool(msg):
while ppid is not None and retry < 3:
print('Terminating Existing Session...')
try:
terminate_process(ppid)
time.sleep(3)
ppid = find_procs_by_name(cmd, device)
except PermissionError:
print('This appears to be a locally established session, if you want to kill that do it yourself')
break
except psutil.AccessDenied:
print('This appears to be a locally established session, session will not be terminated')
break
except psutil.NoSuchProcess:
ppid = find_procs_by_name(cmd, device)
retry += 1

return ppid


if __name__ == '__main__':
if len(sys.argv) >= 3:
# Allow user to ssh to configured port which using ForceCommand and specifying only the device they want to connect to
if len(sys.argv) == 2 and "picocom" not in sys.argv[1]:
_device = f'/dev/{sys.argv[1].replace("/dev/", "")}'
adapter_data = cpi.local.adapters.get(_device)
if not adapter_data:
print(f'{_device.replace("/dev/", "")} Not found on system... Refreshing local adapters.')
cpi.local.build_adapter_dict(refresh=True)
adapter_data = cpi.local.adapters.get(_device)

if adapter_data:
print(f'Establishing Connection to {_device.replace("/dev/", "")}...')
_cmd = adapter_data["config"]["cmd"].replace("{{timestamp}}", time.strftime("%F_%H.%M")).split()
else:
print(f'{_device.replace("/dev/", "")} Not found on system... Exiting.')
sys.exit(1)
# Sent from menu on remote full command is sent
elif len(sys.argv) >= 3:
_cmd = sys.argv[1:]
ppid = find_procs_by_name(sys.argv[1], sys.argv[2])
retry = 0
msg = f"\n{sys.argv[2].replace('/dev/', '')} appears to be in use (may be a previous hung session)."
msg += '\nDo you want to Terminate the existing session'
if ppid is not None and utils.user_input_bool(msg):
while ppid is not None and retry < 3:
print('Terminating Existing Session...')
try:
terminate_process(ppid)
sleep(3)
ppid = find_procs_by_name(sys.argv[1], sys.argv[2])
except PermissionError:
print('This appears to be a locally established session, if you want to kill that do it yourself')
break
except psutil.AccessDenied:
print('This appears to be a locally established session, session will not be terminated')
break
except psutil.NoSuchProcess:
ppid = find_procs_by_name(sys.argv[1], sys.argv[2])
retry += 1
_device = f'/dev/{sys.argv[2].replace("/dev/", "")}'

ppid = check_hung_process(_cmd[0], _device)

if ppid is None:
# if power feature enabled and adapter linked - ensure outlet is on
# TODO try/except block here
if config.power:
cpi.cpiexec.exec_auto_pwron(sys.argv[2])
subprocess.run(_cmd)
if ppid is None:
# if power feature enabled and adapter linked - ensure outlet is on
# TODO try/except block here
if config.power:
cpi.cpiexec.exec_auto_pwron(_device)
subprocess.run(_cmd)

0 comments on commit bc4193a

Please sign in to comment.