Skip to content

Commit 5dd9cad

Browse files
committed
Add option to specify no user when generating SSH commands and config
1 parent a48561f commit 5dd9cad

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

hostedpi/cli/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
ipv6 = Annotated[bool, Option(help="Use the IPv6 connection method")]
2626
numeric = Annotated[bool, Option("--numeric", "-n", help="Use numeric IPv6 address in SSH command")]
2727
user = Annotated[str, Option("--username", "-u", help="Username for SSH connection")]
28+
no_user = Annotated[bool, Option("--no-user", help="Don't include a username in SSH command")]
2829
yes = Annotated[bool, Option("--yes", "-y", help="Proceed without confirmation")]
2930
number = Annotated[Union[int, None], Option(help="Number of Raspberry Pi servers to create", min=1)]
3031
full_table = Annotated[bool, Option(help="Show full table of Raspberry Pi server info")]

hostedpi/cli/ssh.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def do_command(
1616
ipv6: options.ipv6 = False,
1717
numeric: options.numeric = False,
1818
user: options.user = "root",
19+
no_user: options.no_user = False,
1920
):
2021
"""
2122
Get the SSH command to connect to a Raspberry Pi server
@@ -24,6 +25,9 @@ def do_command(
2425
print_error("--numeric is only supported with --ipv6")
2526
raise Exit(1)
2627

28+
if no_user:
29+
user = None
30+
2731
pi = get_pi(name)
2832
if pi is None:
2933
print_error(f"Pi '{name}' not found")
@@ -45,6 +49,7 @@ def do_config(
4549
ipv6: options.ipv6 = False,
4650
numeric: options.numeric = False,
4751
user: options.user = "root",
52+
no_user: options.no_user = False,
4853
):
4954
"""
5055
Get the SSH config to connect to one or more Raspberry Pi servers
@@ -53,6 +58,9 @@ def do_config(
5358
print_error("--numeric is only supported with --ipv6")
5459
raise Exit(1)
5560

61+
if no_user:
62+
user = None
63+
5664
pis = get_pis(names, filter)
5765
for pi in pis:
5866
try:

hostedpi/pi.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -497,47 +497,55 @@ def cancel(self):
497497

498498
self._cancelled = True
499499

500-
def get_ipv4_ssh_command(self, user: str = "root") -> str:
500+
def get_ipv4_ssh_command(self, user: Union[str, None] = "root") -> str:
501501
"""
502502
Construct an SSH command required to connect to the Pi using SSH over IPv4
503503
"""
504+
if user is None:
505+
return f"ssh -p {self.ipv4_ssh_port} {self.ipv4_ssh_hostname}"
504506
return f"ssh -p {self.ipv4_ssh_port} {user}@{self.ipv4_ssh_hostname}"
505507

506-
def get_ipv6_ssh_command(self, *, user: str = "root", numeric: bool = False) -> str:
508+
def get_ipv6_ssh_command(
509+
self, *, user: Union[str, None] = "root", numeric: bool = False
510+
) -> str:
507511
"""
508512
Construct an SSH command required to connect to the Pi using SSH over IPv6
509513
"""
510514
if numeric:
515+
if user is None:
516+
return f"ssh [{self.ipv6_address.compressed}]"
511517
return f"ssh {user}@[{self.ipv6_address.compressed}]"
512518
else:
519+
if user is None:
520+
return f"ssh {self.ipv6_ssh_hostname}"
513521
return f"ssh {user}@{self.ipv6_ssh_hostname}"
514522

515-
def get_ipv4_ssh_config(self, user: str = "root") -> str:
523+
def get_ipv4_ssh_config(self, user: Union[str, None] = "root") -> str:
516524
"""
517525
Construct a string containing the IPv4 SSH config for the Pi. The contents could be added to
518526
an SSH config file for easy access to the Pi.
519527
"""
520-
return f"""Host {self.name}
521-
user {user}
522-
port {self.ipv4_ssh_port}
523-
hostname {self.ipv4_ssh_hostname}
524-
""".strip()
528+
host_line = f"Host {self.name}\n"
529+
user_line = f" user {user}\n" if user else ""
530+
port_line = f" port {self.ipv4_ssh_port}\n"
531+
hostname_line = f" hostname {self.ipv4_ssh_hostname}"
532+
return host_line + user_line + port_line + hostname_line
525533

526534
def get_ipv6_ssh_config(
527535
self,
528536
*,
529-
user: str = "root",
537+
user: Union[str, None] = "root",
530538
numeric: bool = False,
531539
) -> str:
532540
"""
533541
Construct a string containing the SSH config for the Pi. The contents could be added to an
534542
SSH config file for easy access to the Pi.
535543
"""
536544
hostname = self.ipv6_address.compressed if numeric else self.ipv6_ssh_hostname
537-
return f"""Host {self.name}
538-
user {user}
539-
hostname {hostname}
540-
""".strip()
545+
host_line = f"Host {self.name}\n"
546+
user_line = f" user {user}\n" if user else ""
547+
hostname_line = f" hostname {hostname}"
548+
return host_line + user_line + hostname_line
541549

542550
def add_ssh_keys(self, ssh_keys: SSHKeySources) -> set[str]:
543551
"""

hostedpi/picloud.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ def ipv6_ssh_config(self) -> str:
109109
"""
110110
return self.get_ipv6_ssh_config()
111111

112-
def get_ipv4_ssh_config(self, user: str = "root") -> str:
112+
def get_ipv4_ssh_config(self, user: Union[str, None] = "root") -> str:
113113
"""
114114
Construct a string containing the IPv4 SSH config for all Pis within the account. The
115115
contents could be added to an SSH config file for easy access to the Pis in the account.
116116
"""
117117
return "\n".join(pi.get_ipv4_ssh_config(user=user) for pi in self.pis.values())
118118

119-
def get_ipv6_ssh_config(self, user: str = "root", numeric: bool = False) -> str:
119+
def get_ipv6_ssh_config(self, user: Union[str, None] = "root", numeric: bool = False) -> str:
120120
"""
121121
Construct a string containing the IPv6 SSH config for all Pis within the account. The
122122
contents could be added to an SSH config file for easy access to the Pis in the account.

0 commit comments

Comments
 (0)