Files
hermes-skills/skills/devops/server-provisioning-standard/scripts/run_as_root.exp
T

39 lines
1.1 KiB
Plaintext

#!/usr/bin/expect -f
# Usage: ./run_as_root.exp <server-ip> "<command>"
# Requires the local system to have 'expect' installed (`apt-get install -y expect`).
# Automates SSH access via ippadmin, switching to root using `su - root`, and running a command.
# This bypasses the issue of `sudo` requiring a password on a non-interactive SSH connection.
set ip [lindex $argv 0]
set cmd [lindex $argv 1]
# Make sure you have the correct key and root password set here or pass as args
set key_path "/root/.ssh/itpp-infra"
set root_pass "heUa1ucN6Xwta2O"
spawn ssh -i $key_path -o StrictHostKeyChecking=no ippadmin@$ip
expect {
"$ " { send "su - root\r" }
timeout { puts "Timeout waiting for prompt"; exit 1 }
}
expect {
"Password: " { send "$root_pass\r" }
timeout { puts "Timeout waiting for password prompt"; exit 1 }
}
expect {
"# " { send "$cmd\r" }
timeout { puts "Timeout waiting for root prompt"; exit 1 }
}
expect {
"# " { send "exit\r" }
timeout { puts "Timeout waiting for command execution completion"; exit 1 }
}
expect {
"$ " { send "exit\r" }
timeout { puts "Timeout waiting for ippadmin prompt"; exit 1 }
}