Skip to content

Commit 7a94207

Browse files
committed
Add .ssh hosts and port forwarding
1 parent 4fb089d commit 7a94207

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

netsim/reports/ssh_config.j2

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{% set ssh_defaults = defaults.ssh|default({}) %}
22
{% set hostname = ssh_defaults.hostname|default('netlab') %}
3-
{% set public_ip = ssh_defaults.publicip|default('127.0.0.1') %}
3+
{% set public_ip = ssh_defaults.publicip|default('') %}
44
{% set netlab_path = ssh_defaults.netlabpath|default('netlab') %}
5+
{% set ssh_conn = ['paramiko','netconf','paramiko_ssh','network_cli'] %}
56
#
67
# This SSH configuration file allows you to connect to netlab devices running
78
# on a remote server straight from your workstation
@@ -11,26 +12,43 @@
1112
#
1213
# You can change the SSH parameters with these environment variables
1314
#
14-
# NETLAB_SSH_HOSTNAME The hostname used for port forwarding (not implemented yet)
15-
# NETLAB_SSH_PUBLICIP The IP address or hostname of the netlab server
15+
# NETLAB_SSH_HOSTNAME The hostname of the netlab server
16+
# NETLAB_SSH_PUBLICIP The IP address of the netlab server when the hostname is not resolvable
1617
# NETLAB_SSH_NETLABPATH The path to 'netlab' command (if it's not in PATH)
1718
#
1819
Host {{ hostname }}
20+
StrictHostKeyChecking off
21+
UserKnownHostsFile /dev/null
22+
{% if public_ip %}
1923
HostName {{ public_ip }}
20-
{# for host in groups['fortios'] %}
21-
{% set hv = hostvars[host] %}
22-
{% if hv.get('provider') != 'clab' %}
23-
LocalForward {{ 8000 + hv.id }} {{ hv.ansible_host }}:443
24-
{% endif %}
25-
{% endfor #}
24+
{% endif %}
25+
{% for provider in defaults.providers.keys() %}
26+
{% for n_name,n_data in nodes.items() if provider in n_data and 'ports' in n_data[provider] %}
27+
{% for p_fwd in n_data[provider].ports %}
28+
{% set p_data = p_fwd.split(':') %}
29+
LocalForward {{ p_data[0] }} {{ n_data.ansible_host }}:{{ p_data[1] }}
30+
{% endfor %}
31+
{% endfor %}
32+
{% endfor %}
2633

2734
{% for n_name,n_data in nodes.items() %}
2835
Host {{ n_name }}
29-
HostName {{ public_ip }}
36+
HostName {{ public_ip or hostname }}
3037
RequestTTY yes
3138
RemoteCommand {{ netlab_path }} connect -i {{ defaults.multilab.id|default('default') }} {{ n_name }}
3239
UserKnownHostsFile /dev/null
33-
StrictHostKeyChecking no
40+
StrictHostKeyChecking off
41+
UserKnownHostsFile /dev/null
3442
LogLevel QUIET
3543

3644
{% endfor %}
45+
{% for n_name,n_data in nodes.items()
46+
if n_data.ansible_connection in ssh_conn %}
47+
Host {{ n_name }}.ssh
48+
HostName {{ n_data.ansible_host }}
49+
User {{ n_data.ansible_user }}
50+
ProxyJump {{ hostname }}
51+
StrictHostKeyChecking off
52+
UserKnownHostsFile /dev/null
53+
54+
{% endfor %}

0 commit comments

Comments
 (0)