Skip to content

Instantly share code, notes, and snippets.

@o0-o
Last active April 16, 2023 05:40
Show Gist options
  • Select an option

  • Save o0-o/2f7002e0f9384c3eb7725b5df49d89c6 to your computer and use it in GitHub Desktop.

Select an option

Save o0-o/2f7002e0f9384c3eb7725b5df49d89c6 to your computer and use it in GitHub Desktop.
[Install an SSH Key in EdgeOS via SSH] Non-interactive Installation of SSH key on an EdgeOS Device #edgemax #edgerouter #edgeos #expect #ubiquiti
#!/usr/bin/env expect
# USAGE: ./install_ssh_key_on_edgeos_via_ssh.exp 192.168.1.1 ubnt ubnt #EdgeOS Defaults
# WARNING: This script will delete any existing keys on local and remote hosts for the specified user
set timeout 8
set host [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn bash -c "rm ~/.ssh/$user@$host ~/.ssh/$user@$host.pub"
sleep 1
spawn bash -c "ssh-keygen -t rsa -b 4096 -f ~/.ssh/$user@$host -N \"\""
expect "randomart"
sleep 1
spawn bash -c "scp -o StrictHostKeyChecking=no ~/.ssh/$user@$host.pub $user@$host:/home/$user/.ssh"
expect "assword"
send "$password\r"
expect "100%" {
sleep 1
send "$password\r"
exp_continue
}
spawn bash -c "ssh -o StrictHostKeyChecking=no $user@$host"
expect "assword"
send "$password\r"
expect ":~$" {
sleep 1
send "$password\r"
exp_continue
}
send "configure\r"
expect "#"
send "delete system login user ubnt authentication public-keys\r"
expect "#"
send "commit\r"
expect "#"
send "loadkey $user /home/$user/.ssh/$user@$host.pub\r"
expect "#"
send "exit\r"
expect ":~$"
send "exit\r"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment