From d0dcde2e037d15000822d8fc8568695f917fbabf Mon Sep 17 00:00:00 2001 From: Flightkick Date: Wed, 12 Aug 2015 18:12:23 +0200 Subject: [PATCH] Initial commit --- README.md | 34 +++++++++++++++++++++++++++++++++- local.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ remote.sh | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 local.sh create mode 100644 remote.sh diff --git a/README.md b/README.md index b1b9d7b..ae5021b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,34 @@ # lin-remoteupdate -Scripts for automatically updating a remote Linux server over SSH. +These scripts can update multiple remote Linux machines over SSH. + +## Prerequisites +The scripts require sshpass (http://sourceforge.net/projects/sshpass/). +``` +Ubuntu/Debian: apt-get install sshpass +Fedora/CentOS: yum install sshpass +Arch: pacman -S sshpass +``` + +## Usage instructions +1. Download the files to your Linux computer (not the server) +2. Install the prerequisites (see above) +2. Enable file execution permissions on local.sh if applicable +3. Open a terminal and run local.sh +4. Enter the IP's/Hostnames of the servers you would like to update, seperated by a semicolon (;) +5. Enter a sudoers account name +6. Enter the specified account's password +7. Choose the mode to run in: + * 1 = Auto restart when required + * 2 = Promt if restart is required + * 3 = Ignore (do not restart) +8. Profit! + +## What does it do? +The local script opens a SSH tunnel to the remote server and does the following: +sudo apt-get update -y +sudo apt-get upgrade -y +sudo apt-get dist-upgrade -y +sudo apt-get autoremove -y +sudo apt-get clean + +After that the script will check if the machine requires a reboot, depending on your choices when running local.sh it will either automatically restart, prompt for restart or ignore the restart. \ No newline at end of file diff --git a/local.sh b/local.sh new file mode 100644 index 0000000..615f576 --- /dev/null +++ b/local.sh @@ -0,0 +1,53 @@ +#!/bin/bash +hostReachable=0; +IFS=";" +while [ "$hostReachable" -eq 0 ]; do + echo "Please enter the IP/Hostname of the server you would like to update. You can enter multiple hosts semicolon (;) separated:" + read hostlist + for host in $hostlist + do + if ! ping -c 1 "$host" &> /dev/null + then + echo "ERROR: The specified IP/Hostname ($host) is unreachable." + hostReachable=0; + break + else + hostReachable=1; + fi + done +done + +echo "Please enter the account name you would like to log in with:" +read user + +echo "Please enter the password for the specified user account:" +read -s -p Password: pass +echo + +echo "When new updates have been installed a server might need a restart, you can run this updater in various modes:" +echo "1 = Automatically reboot when needed" +echo "2 = Ask me if I would like to reboot" +echo "3 = Automatically ignore the need to reboot" +echo +echo "Please choose the mode you would like to run. (1, 2, 3):" + +rebootBool=0; +while [ $rebootBool -eq 0 ]; do + read rebootInt + if [ "$rebootInt" == "1" ] || [ "$rebootInt" == "2" ] || [ "$rebootInt" == "3" ]; then + rebootBool=1; + else + echo "No valid response. (1, 2, 3):" + fi +done + +echo "Starting routine..." +echo + +for host in $hostlist +do + sshpass -p "$pass" ssh "$host" -l "$user" -o StrictHostKeyChecking=no "bash -s $pass $rebootInt $host" < remote.sh +done + +echo +echo "Script finished!" diff --git a/remote.sh b/remote.sh new file mode 100644 index 0000000..cfc8c06 --- /dev/null +++ b/remote.sh @@ -0,0 +1,35 @@ +#!/bin/bash +echo "$1" | sudo -S apt-get update -y +echo "$1" | sudo -S apt-get upgrade -y +echo "$1" | sudo -S apt-get dist-upgrade -y +echo "$1" | sudo -S apt-get autoremove -y +echo "$1" | sudo -S apt-get clean +if [ -f /var/run/reboot-required ]; then + if [ "$2" -eq 1 ]; then + echo "Autoreboot enabled, will reboot now." + echo "$1" | sudo -S reboot + elif [ "$2" -eq 2 ]; then + rebootBool=0; + while [ $rebootBool -eq 0 ]; do + echo "The newly installed updates require the server to be rebooted, do you wish to reboot this server ($3) now? (yes/no):" + read reboot + + if [ "$reboot" == "yes" ] || [ "$reboot" == "y" ]; then + rebootBool=1; + echo "$1" | sudo -S reboot + elif [ "$reboot" == "no" ] || [ "$reboot" == "n" ]; then + rebootBool=1; + echo + echo "Updates installed but might not be applied until a reboot takes place." + else + echo + echo "$reboot is not a valid response." + fi + done + elif [ "$2" -eq 3 ]; then + echo "Autoignore enabled, will ignore reboot." + echo "Updates installed but might not be applied until a reboot takes place." + fi +else + echo "Reboot not required" +fi