-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh_utils.py
174 lines (145 loc) · 5.52 KB
/
ssh_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from cryptography.hazmat.primitives import serialization
def private_to_public_key(private_key_path):
"""Convert a private key to a public key.
Parameters
----------
private_key_path : str
The path to the private key file.
Returns
-------
public_key : str
The public key in OpenSSH format.
"""
# Read the private key from the file
with open(private_key_path, "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
)
# Get the public key
public_key = private_key.public_key()
# Serialize the public key in OpenSSH format
public_key_bytes = public_key.public_bytes(
encoding=serialization.Encoding.OpenSSH,
format=serialization.PublicFormat.OpenSSH,
)
# Add comment to public key and return
public_key_str = public_key_bytes.decode("utf-8") + " bastion-ssh-key"
return public_key_str
class SSHConfig:
"""
A class for parsing and modifying SSH config files.
Parameters
----------
filename : str
The path to the SSH config file.
Methods
-------
add_host(host, **kwargs)
Adds a new host to the config file with the given keyword arguments.
delete_host(host)
Deletes a host from the config file.
print_config()
Prints the entire SSH config file.
lookup_host(host)
Returns a dictionary with the configuration for the specified host.
print_host(host)
Prints the configuration for the specified host.
"""
def __init__(self, filename):
self.filename = filename
self.config = self._read_config()
def _read_config(self):
"""Reads the SSH config file and returns a dictionary."""
config = {}
with open(self.filename) as f:
lines = f.readlines()
i = 0
while i < len(lines):
if lines[i].startswith("Host"):
host = lines[i].strip().split()[1]
config[host] = {}
i += 1
while i < len(lines) and not lines[i].startswith("Host"):
line = lines[i].strip()
if line:
# Check line has at least 2 words before split
if len(line.split()) >= 2:
key, value = line.split(maxsplit=1)
config[host][key] = value
i += 1
else:
i += 1
return config
def _write_config(self):
"""Writes the updated ssh config to the file."""
with open(self.filename, "w") as f:
for host, config in self.config.items():
f.write(f"Host {host}\n")
for key, value in config.items():
f.write(f" {key} {value}\n")
f.write("\n")
def add_host(self, host, **kwargs):
"""Adds a new host to the config file."""
if host in self.config:
print(f"Host {host} already exists in config.")
# raise ValueError(f"Host {host} already exists in config.")
self.config[host] = kwargs
self._write_config()
def delete_host(self, host):
"""Deletes a host from the config file."""
if host not in self.config:
print(f"Host {host} not found in config.")
# raise ValueError(f"Host {host} not found in config.")
del self.config[host]
self._write_config()
def print_config(self):
"""Prints the entire ssh config file."""
with open(self.filename) as f:
print(f.read())
def lookup_host(self, host):
"""Returns the configuration for a specific host."""
if host not in self.config:
raise ValueError("Host not found in config.")
return self.config[host]
def print_host(self, host):
"""Prints the configuration for a specific host."""
host_info = self.lookup_host(host)
print(f"Host {host}")
for key, value in host_info.items():
print(f" {key} {value}")
if "__main__" == __name__:
# Set the path to your ssh config file
ssh_config_file = "/home/your-username/.ssh/config"
# Add a new bastion host
new_host_bastion = {
"Hostname": "<Public IP of your bastion host>",
"User": "ec2-user",
"ForwardAgent": "yes",
"IdentityFile": "/Users/<username>/.ssh/bastion-ssh-key.pem",
"ForwardX11": "yes",
}
# Add a new notebook host
new_host_notebook = {
"Hostname": "<Private IP of your notebook instance>",
"User": "ec2-user",
"UserKnownHostsFile": "/dev/null",
"StrictHostKeyChecking": "no",
"ProxyCommand": "ssh -W %h:%p ec2-user@bastion",
"IdentityFile": "/Users/<username>/.ssh/bastion-ssh-key.pem",
"LocalForward": "6006 localhost:6006", # Tensorboard
"ForwardX11": "yes",
}
# # Create an instance of the SSHConfig class
# config = SSHConfig(ssh_config_file)
# config.add_host("ec2-bastion", **new_host_bastion)
# config.add_host("sagemaker-notebook", **new_host_notebook)
# # Lookup a specific host's configuration
# host = config.lookup_host("ec2-bastion")
# print(host)
# # Print a specific host's configuration
# config.print_host("ec2-bastion")
# config.delete_host("ec2-bastion")
# config.delete_host("sagemaker-notebook")
# # Print the entire config
# config.print_config()