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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423import sshconf
import pytest
import os
test_config = os.path.join(os.path.dirname(__file__), "test_config")
test_config2 = os.path.join(os.path.dirname(__file__), "test_config2")
test_config_include_specific = os.path.join(
os.path.dirname(__file__), "test_config_include_specific"
)
def test_parsing():
c = sshconf.read_ssh_config(test_config)
assert len(c.hosts()) == 2
assert c.host("*")["user"] == "something"
assert c.host("svu")["proxycommand"] == "nc -w 300 -x localhost:9050 %h %p"
s1 = c.config().splitlines()
s2 = open(test_config).readlines()
assert len(s1) == len(s2)
def test_set():
c = sshconf.read_ssh_config(test_config)
c.set("svu", Compression="no", Port=2222)
print(c.config())
print("svu", c.host("svu"))
assert " Compression no" in c.config()
assert " Port 2222" in c.config()
def test_set_host_failed():
c = sshconf.read_ssh_config(test_config)
with pytest.raises(ValueError):
c.set("svu", Host="svu-new")
def test_rename():
c = sshconf.read_ssh_config(test_config)
assert c.host("svu")["hostname"] == "www.svuniversity.ac.in"
hosts = c.hosts()
c.rename("svu", "svu-new")
hosts2 = c.hosts()
assert hosts.index("svu") == hosts2.index("svu-new") # order is the same
assert "Host svu-new" in c.config()
assert "Host svu\n" not in c.config()
assert "svu" not in c.hosts()
assert "svu-new" in c.hosts()
c.set("svu-new", Port=123, HostName="www.svuniversity.ac.in") # has to be success
assert c.host("svu-new")["port"] == 123
assert c.host("svu-new")["hostname"] == "www.svuniversity.ac.in" # still same
with pytest.raises(ValueError): # we can't refer to the renamed host
c.set("svu", Port=123)
def test_update_fail():
c = sshconf.read_ssh_config(test_config)
with pytest.raises(ValueError):
c.set("notfound", Port=1234)
def test_add():
c = sshconf.read_ssh_config(test_config)
hosts = list(c.hosts())
c.add(
"venkateswara",
Hostname="venkateswara.onion",
User="other",
Port=22,
ProxyCommand="nc -w 300 -x localhost:9050 %h %p",
)
hosts2 = list(c.hosts())
assert hosts + ["venkateswara"] == hosts2
assert "venkateswara" in c.hosts()
assert c.host("venkateswara")["proxycommand"] == "nc -w 300 -x localhost:9050 %h %p"
assert "Host venkateswara" in c.config()
with pytest.raises(ValueError):
c.add("svu")
with pytest.raises(ValueError):
c.add("venkateswara")
def test_add_before_host():
c = sshconf.read_ssh_config(test_config)
hosts = list(c.hosts())
c.add(
"venkateswara",
before_host="svu",
Hostname="venkateswara.onion",
User="other",
Port=22,
ProxyCommand="nc -w 300 -x localhost:9050 %h %p",
)
hosts2 = list(c.hosts())
c.add(
"venkateswara2",
before_host="*",
Hostname="venkateswara.onion",
User="other",
Port=22,
ProxyCommand="nc -w 300 -x localhost:9050 %h %p",
)
hosts3 = list(c.hosts())
new_config = c.config()
print("new config", new_config)
assert hosts == ["*", "svu"]
assert hosts2 == ["*", "venkateswara", "svu"]
assert hosts3 == ["venkateswara2", "*", "venkateswara", "svu"]
assert "venkateswara" in c.hosts()
assert "Host venkateswara" in new_config
with pytest.raises(ValueError): # cant add before a host that is not found
c.add(
"svucs",
before_host="not-found-host",
Hostname="venkateswara.onion",
User="other",
Port=22,
ProxyCommand="nc -w 300 -x localhost:9050 %h %p",
)
with pytest.raises(ValueError): # its there now
c.add("venkateswara")
def test_save():
import tempfile
tc = os.path.join(tempfile.gettempdir(), "temp_ssh_config-4123")
try:
c = sshconf.read_ssh_config(test_config)
c.set("svu", Hostname="ssh.svuniversity.ac.in", User="mca")
c.write(tc)
c2 = sshconf.read_ssh_config(tc)
assert c2.host("svu")["hostname"] == "ssh.svuniversity.ac.in"
assert c2.host("svu")["user"] == "mca"
assert c.config() == c2.config()
finally:
os.remove(tc)
def test_empty():
import tempfile
tc = os.path.join(tempfile.gettempdir(), "temp_ssh_config-123")
try:
c = sshconf.empty_ssh_config_file()
c.add("svu33", HostName="ssh33.svu.local", User="mca", Port=22)
c.write(tc)
c2 = sshconf.read_ssh_config(tc)
assert 1 == len(c2.hosts())
assert c2.host("svu33")["hostname"] == "ssh33.svu.local"
finally:
os.remove(tc)
def test_mapping_set_existing_key():
c = sshconf.read_ssh_config(test_config)
c.set(
"svu", Hostname="ssh.svuniversity.ac.in", User="mca", proxycommand="nc --help"
)
print(c.config())
assert "Hostname ssh.svuniversity.ac.in" in c.config()
assert "User mca" in c.config()
assert "ProxyCommand nc --help" in c.config()
def test_mapping_set_existing_key_multi_values():
c = sshconf.read_ssh_config(test_config)
c.set(
"svu",
Hostname="ssh.svuniversity.ac.in",
User="mca",
remoteforward=[
"localhost:3322 localhost:22",
"localhost:10809 172.26.176.1:10809",
],
)
print(c.config())
assert "Hostname ssh.svuniversity.ac.in" in c.config()
assert "User mca" in c.config()
assert "RemoteForward localhost:3322 localhost:22" in c.config()
assert "RemoteForward localhost:10809 172.26.176.1:10809" in c.config()
def test_mapping_set_new_key():
c = sshconf.read_ssh_config(test_config)
c.set("svu", forwardAgent="yes", unknownpropertylikethis="noway")
assert "Hostname www.svuniversity.ac.in" in c.config() # old parameters
assert "Port 22" in c.config()
assert "ForwardAgent yes" in c.config() # new parameter has been properly cased
assert "unknownpropertylikethis noway" in c.config()
def test_mapping_add_new_keys():
c = sshconf.read_ssh_config(test_config)
c.add(
"svu-new",
forwardAgent="yes",
unknownpropertylikethis="noway",
Hostname="ssh.svuni.local",
user="mmccaa",
)
assert "Host svu-new" in c.config()
assert "ForwardAgent yes" in c.config()
assert "unknownpropertylikethis noway" in c.config()
assert "Hostname ssh.svuni.local" in c.config()
assert "forwardagent" in c.host("svu-new")
assert "unknownpropertylikethis" in c.host("svu-new")
assert "hostname" in c.host("svu-new")
assert "user" in c.host("svu-new")
def test_remove():
c = sshconf.read_ssh_config(test_config)
c.add(
"abc",
forwardAgent="yes",
unknownpropertylikethis="noway",
Hostname="ssh.svuni.local",
user="mmccaa",
)
c.add(
"def",
forwardAgent="yes",
unknownpropertylikethis="noway",
Hostname="ssh.svuni.local",
user="mmccaa",
)
config1 = c.config()
hosts = list(c.hosts())
c.remove("abc")
config2 = c.config()
hosts2 = list(c.hosts())
c.remove("svu")
config3 = c.config()
hosts3 = list(c.hosts())
assert "abc" in hosts
assert "abc" not in hosts2
assert "Host abc" in config1
assert "Host abc" not in config2
assert "Host svu" not in config3
assert hosts2 == ["*", "svu", "def"] # test order
assert hosts3 == ["*", "def"]
assert "# within-host-comment" not in config3
def test_read_duplicate_keys():
c = sshconf.read_ssh_config(test_config2)
host = c.host("foo")
assert 5 == len(host.keys())
assert "localforward" in host
assert 2 == len(host["localforward"])
def test_set_duplicate_keys():
c = sshconf.read_ssh_config(test_config2)
lfs = c.host("foo")["localforward"]
assert type(lfs) is list
assert len(lfs) == 2
lfs.append("1234 localhost:4321")
c.set("foo", localforward=lfs)
import tempfile
tc = os.path.join(tempfile.gettempdir(), "temp_ssh_config-tudk")
try:
c.write(tc)
d = sshconf.read_ssh_config(tc)
host2 = d.host("foo")
assert len(host2["localforward"]) == 3
finally:
os.remove(tc)
def test_mapping_remove_existing_key():
c = sshconf.read_ssh_config(test_config)
svu = c.host("svu")
print(svu)
c.unset("svu", "proxycommand")
print(c.config())
assert "ProxyCommand" not in c.config()
svu2 = c.host("svu")
assert "proxycommand" not in svu2
assert "hostname" in svu2
assert "port" in svu2
def test_read_included_specific():
c = sshconf.read_ssh_config(test_config_include_specific)
hosts = c.hosts()
print("hosts", hosts)
assert "svuincluded" in hosts
h = c.host("svuincluded")
print(h)
print(c.config())
def test_save_with_included(tmpdir):
conf = tmpdir.join("config")
incl = tmpdir.join("included_host")
conf.write("""
Host svu.local
Hostname ssh.svu.local
User something
Include included_host
""")
incl.write("""
Host svu.included
Hostname ssh.svu.included
User whatever
""")
c = sshconf.read_ssh_config(conf)
hosts = c.hosts()
print("hosts", hosts)
assert "svu.local" in hosts
assert "svu.included" in hosts
h = c.host("svu.included")
print(h)
c.set("svu.included", Hostname="ssh2.svu.included", Port="1234")
h = c.host("svu.included")
print(h)
print(c.config())
c.save()
assert "ssh2.svu.included" not in conf.read()
assert "ssh2.svu.included" in incl.read()
def test_read_included_glob(tmpdir):
conf = tmpdir.join("config")
incl = tmpdir.mkdir("conf.d").join("included_host")
conf.write("""
Host svu.local
Hostname ssh.svu.local
User something
Include conf.d/*
""")
incl.write("""
Host svu.included
Hostname ssh.svu.included
User whatever
""")
c = sshconf.read_ssh_config(conf)
hosts = c.hosts()
print("hosts", hosts)
assert "svu.local" in hosts
assert "svu.included" in hosts