Skip to content

Commit

Permalink
SSH Server on clab with key import (#5)
Browse files Browse the repository at this point in the history
SSH Server on clab with key import
  • Loading branch information
FloSch62 authored Dec 7, 2024
2 parents 7f38286 + 56041c4 commit fcb176e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN apt install -y --no-install-recommends \
dnsutils \
telnet \
unzip \
openssh-server \
zsh && rm -rf /var/lib/apt/lists/*

COPY --chmod=644 --chown=root:root ./wsl-distribution.conf /etc/wsl-distribution.conf
Expand All @@ -30,6 +31,8 @@ COPY ./terminal-profile.json /usr/lib/wsl/terminal-profile.json

COPY ./profile /etc/profile

RUN bash -c "echo 'port 2222' >> /etc/ssh/sshd_config"

# Create clab user and add to sudo group
RUN useradd -m -s /bin/zsh clab && \
echo "clab:clab" | chpasswd && \
Expand Down
55 changes: 55 additions & 0 deletions oobe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,61 @@ function install_fonts {
fi
}

function import_ssh_keys {
KEY_CHECK=$(powershell.exe -NoProfile -Command '
$key_types = @("rsa", "ecdsa", "ed25519")
foreach ( $type in $key_types )
{
if( Test-Path $env:userprofile\.ssh\id_$type.pub )
{
return $type
}
}
Write-Output False
')

mkdir -p /home/clab/.ssh

case $KEY_CHECK in

rsa*)
echo -e "\033[32mRSA key found, Copying into Containerlab WSL...\033[0m"
KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_rsa.pub')
echo $KEY >> /home/clab/.ssh/authorized_keys
;;
ecdsa*)
echo -e "\033[32mECDSA key found, Copying into Containerlab WSL...\033[0m"
KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_ecdsa.pub')
echo $KEY >> /home/clab/.ssh/authorized_keys
;;
ed25519*)
echo -e "\033[32mED25519 key found, Copying into Containerlab WSL...\033[0m"
KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_ed25519.pub')
echo $KEY >> /home/clab/.ssh/authorized_keys
;;
False*)
echo -e "\033[34mNo host keys found, Generating RSA key...\033[0m"
powershell.exe -NoProfile -Command "ssh-keygen -t rsa -b 4096 -f \$env:userprofile\.ssh\id_rsa -N '\"\"'"
KEY=$(powershell.exe -NoProfile -Command 'Get-Content $env:userprofile\.ssh\id_rsa.pub')
echo $KEY >> /home/clab/.ssh/authorized_keys
# powershell.exe -NoProfile -Command "Get-Content $env:userprofile\.ssh\id_rsa.pub | ssh clab@localhost -p 2222 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'"
;;
*)
echo "\033[34m\nCouldn't match key type, invoking Powershell may have failed. Create an issue at https://github.com/srl-labs/wsl-containerlab\033[0m"
esac

echo -e "\033[32mKeys successfully copied. You can SSH into Container WSL passwordless with: 'ssh clab@localhost -p 2222'\033[0m"
}

# We know the user clab exists from Dockerfile with UID 1000
if getent passwd "$DEFAULT_UID" > /dev/null ; then

echo -e "\033[32mWelcome to Containerlab's WSL distribution\033[0m"

echo "cd ~" >> /home/clab/.bashrc

echo "echo clab | sudo -S mkdir -p /run/docker/netns" >> /home/clab/.bashrc

PS3="
Please select which shell you'd like to use: "
Expand Down Expand Up @@ -159,6 +208,12 @@ Select zsh configuration: "
esac
done

read -p "Copy Windows SSH keys for passwordless SSH access? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
import_ssh_keys
fi

exit 0
fi

Expand Down
4 changes: 3 additions & 1 deletion zsh/.zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,6 @@ eval "$(pyenv init -)"
# go path
export PATH=$PATH:/usr/local/go/bin:~/go/bin

cd ~
cd ~
# create /run/docker/netns without password prompt
echo clab | sudo -S mkdir -p /run/docker/netns

0 comments on commit fcb176e

Please sign in to comment.