Quickest Way to Connect to LinuxAcademy Servers?
hiddenknight821
Member Posts: 1,209 ■■■■■■□□□□
I know this is probably a petty or odd request. Sometimes I'd run into trouble when remoting in my VMs for some reasons, and I'd have to destroy and create a new one. They'd become non-responsive, even when I rebooted through the LA portal. This happened a couple of times already. Not sure why it went wacky originally. I had pubkey authentication set up and disabled password authentication. However, I'm tired of doing this manually every time. AWS, DigitialOcean, and other VPS providers make this process easier. Although, I do understand the main purpose of LinuxAcademy VMs as they're solely meant to be beaten to death as we lab on it.
If you insist that I'm overthinking it, and I should just leave the VM alone at its default and only spin up a new VM when necessary, then I'll leave it at that. Unless, you're just as curious as I'm, I'd like some helps. I tried to come up with a script for this, but it's more trickier as the script would have to do some heavy-lifting with the interactive commands like 'ssh' and 'su' since this must be done before I can even 'ssh-copy-id'.
Here's a rough script I just started this morning, but I'm now struggling with the 'expect' command as I'm trying to circumvent the 'su root' issue as I can't directly remote in root account using SSH. I'd need root privilege to at least move the default account to the 'wheel' group to perform the administrative tasks. I haven't pasted the 'expect' script yet as I'm still struggling with it. This should make a fun thread.
EDIT: Forgot to mention the synopsis.
$./script.sh [ip address or hostname]
If you insist that I'm overthinking it, and I should just leave the VM alone at its default and only spin up a new VM when necessary, then I'll leave it at that. Unless, you're just as curious as I'm, I'd like some helps. I tried to come up with a script for this, but it's more trickier as the script would have to do some heavy-lifting with the interactive commands like 'ssh' and 'su' since this must be done before I can even 'ssh-copy-id'.
Here's a rough script I just started this morning, but I'm now struggling with the 'expect' command as I'm trying to circumvent the 'su root' issue as I can't directly remote in root account using SSH. I'd need root privilege to at least move the default account to the 'wheel' group to perform the administrative tasks. I haven't pasted the 'expect' script yet as I'm still struggling with it. This should make a fun thread.
#!/bin/sh # EXIT CODES HERE: # 10 = No private or public key files # 20 = Host is unavailable # define variables here private_key="/home/user/privatekey" public_key="/home/user/publickey.pub" # check if both public and private file exists if [ ! -f "$private_key" ] || [ ! -f "$public_key" ] then # Exit program if either files aren't found echo "Either public or public key do not exist" exit 10 else # check to see if host is available ping -c 1 "$1" &>/dev/null if [ "$?" = 0 ] then # attempt to connect to Academy and set up pubkey authentication sshpass -p default ssh -o StrictHostKeychecking=no user@"$1" mkdir -m 700 .ssh sshpass -p default scp "$public_key" user@"$1":~/.ssh/authorized_keys sshpass -p default ssh -v user@"$1" chmod 400 .ssh/authorized_keys else echo "Host is not up or available" exit 20 fi # Disable password authentication # STUCK ON THIS PART AS I NEED ROOT PRIVILEGE fi
EDIT: Forgot to mention the synopsis.
$./script.sh [ip address or hostname]
Comments
-
junilinux Member Posts: 43 ■■■□□□□□□□Remind me of using LA for LPIC-1 preparation, it was so easy for just typing the public IP and everything went smoothly
Why dont you just contact LA support for further troubleshooting about this? -
hiddenknight821 Member Posts: 1,209 ■■■■■■□□□□Okay. I see this thread is closed. So I managed to finish the scripts. Works very nicely. I was having trouble embedding the expect script in my bash script. Instead, I'd have to have my bash script calls the external expect script. If someone can figure out how to put them all in one file, then that'd be great.
main script:#!/bin/sh # REQUIRED PACKAGES FOR SCRIPT TO WORK # sshpass # expect # nmap # EXIT CODES HERE: # 10 = No private or public files # 20 = Host is unavailable # files variables here (substitute accordingly) private_key="~/id_rsa" public_key="~/id_rsa.pub" expect_script="~/expect.sh" # check if both public and private file exist if [ ! -f "$private_key" ] || [ ! -f "$public_key" ] then # Exit program if either file isn't found echo "Either public or private key does not exist" exit 10 else # check to see if host is available output=`nmap "$1" -PN -p ssh | grep open` if [ ! -z "$output" ] then # attempt to connect to server and set up pubkey authentication sshpass -p 123456 ssh -o StrictHostKeychecking=no user@"$1" mkdir -m 700 .ssh sshpass -p 123456 scp "$public_key" user@"$1":~/.ssh/authorized_keys sshpass -p 123456 ssh user@"$1" chmod 400 .ssh/authorized_keys else echo "Host is not up or available" exit 20 fi # Disable password authentication # call Expect script to move user to 'wheel' group $expect_script "$private_key" "$1" # Disable password authentication ssh -t -i "$private_key" user@"$1" "echo 123456 | sudo -S sed -i 's,^PasswordAuthentication yes,PasswordAuthentication no,' /etc/ssh/sshd_config" ssh -t -i "$private_key" user@"$1" "echo 123456 | sudo -S systemctl restart sshd.service" fi
expect script:#!/usr/bin/expect set private [ lindex $argv 0 ] set host [ lindex $argv 1 ] spawn ssh -t -o StrictHostKeychecking=no -i $private user@$host send "su - root\r"; expect "Password:" send "123456\r"; expect "(current) UNIX password:" send "123456\r"; expect "New password:" send "passphrase\r"; expect "Retype new password:" send "passphrase\r"; send "gpasswd -a user wheel\r"; send "exit\r"; send "exit\r"; interact
EDIT: Forgot to mention how to run the script properly. I'd recommend redirecting the STDOUT and STDERR to a text file. I'd run my script like this.
./script.sh ipaddress_or_hostname &> scriptoutput.txtWhy dont you just contact LA support for further troubleshooting about this?
Not worth the trouble as this is pretty trivial. They gave us 6 servers after all, and their servers are meant to be abused. The servers time out every 120 minutes anyway. I'd prefer the solution I came up with. Automating stuff is never boring. -
JockVSJock Member Posts: 1,118Just wanted to say that I'm having this issue too and noticed that the Linux Academy forums, it seems to come up a few times as well.
I've noticed too that is I spin up a VM, even after mounting it, it could be a few seconds to a few minutes before I successfully SSH in. Some of the other specialized labs like the LVM lab it takes some time to log in.
Not sure if AWS is having an issue, I don't know enough about it.***Freedom of Speech, Just Watch What You Say*** Example, Beware of CompTIA Certs (Deleted From Google Cached)
"Its easier to deceive the masses then to convince the masses that they have been deceived."
-unknown -
hiddenknight821 Member Posts: 1,209 ■■■■■■□□□□I'm not sure if AWS is having issue either. Although, I do believe they need to improve the efficiency between the interface and the servers. I hope you find the scripts useful to help you spin up and securely connect to other VMs quickly, which is its purpose.