Skip to content

Instantly share code, notes, and snippets.

@shuuji3
Last active November 10, 2020 01:19
Show Gist options
  • Save shuuji3/fb70bffdef909131142bfff181ebf0f0 to your computer and use it in GitHub Desktop.
Save shuuji3/fb70bffdef909131142bfff181ebf0f0 to your computer and use it in GitHub Desktop.
Generate a dynamic SSH config file to ssh Multipass using fish shell.
function gen-ssh-config-for-multipass --description "Generate a SSH config for multipass."
echo 'Include ~/.ssh/config
Host mp-*
User multipass
'
set -l vms (multipass ls --format csv | grep Running)
for vm in $vms
set name (echo $vm | awk -F , '{ print $1 }')
set ip (echo $vm | awk -F , '{ print $3 }')
echo "Host mp-$name
Hostname $ip
"
end
end
function ssh
command ssh -F (gen-ssh-config-for-multipass | psub) $argv
end
> multipass ls
Name State IPv4 Image
primary Stopped -- Ubuntu 18.04 LTS
vm-1 Running 192.168.64.1 Ubuntu 18.04 LTS
vm-2 Running 192.168.64.2 Ubuntu 18.04 LTS
vm-3 Running 192.168.64.3 Ubuntu 18.04 LTS
> gen-ssh-config-for-multipass
Host mp-*
User multipass
Host mp-vm-1
Hostname 192.168.64.1
Host mp-vm-2
Hostname 192.168.64.2
Host mp-vm-3
Hostname 192.168.64.3
> ssh -F (gen-ssh-config-for-multipass | psub) mp-vm-1
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-65-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Wed Oct 30 03:55:59 JST 2019
System load: 0.72 Processes: 119
Usage of /: 56.0% of 4.67GB Users logged in: 0
Memory usage: 30% IP address for enp0s2: 192.168.64.1
Swap usage: 0% IP address for docker0: 172.17.0.1
* Kata Containers are now fully integrated in Charmed Kubernetes 1.16!
Yes, charms take the Krazy out of K8s Kata Kluster Konstruction.
https://ubuntu.com/kubernetes/docs/release-notes
0 packages can be updated.
0 updates are security updates.
Welcome to fish, the friendly interactive shell
multipass@vm-1 ~>
@shuuji3
Copy link
Author

shuuji3 commented Dec 3, 2019

Please package this script as a fisher one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment