Score:0

How to execute a local script from a local client with Ubuntu on a remote VPS with Ubuntu without prompt?

la flag

I have made extensive research about how to setup and secure correctly our new VPS with a Ubuntu 22.04 OS for three weeks, and I have currently a good idea of what has to be done.

However, I would like to build a bash script to make all these numerous and complex tasks automatically with a global script and sub-scripts by subject.

For example, the global script VPS_Setup.sh will launch successively the sub-scripts below:

- VPS_Setup_01_Update_OS.sh
- VPS_Setup_02_Create_New_Sudo_Group.sh
- VPS_Setup_03_Create_New_Sudo_User.sh
- VPS_Setup_04_Adjust_Timezone.sh
- VPS_Setup_05_Adjust_Swapfile.sh
...

Inside VPS_Setup_01_Update_OS.sh, i have simply write the following commands:

apt update -y
apt upgrade -y
apt autoremove -y
apt autoclean -y

After many attempts mainly based on these sources:

I'm launching sub-scripts from the global script by this way:

SSHPASS='mypassword' sshpass -e ssh -tt -o StrictHostKeyChecking=no root@XX.XX.XX.XX 'bash -s' < ./VPS_Setup_01_Update_OS.sh

The sub-script is launched, and I receive no error messages.
However, only the first command is executed.
Then, it stops and display the command prompt.

Here is what is displayed:

Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.2.0 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
Last login: Thu Feb  9 19:54:37 2023 from XX.XX.XX.XX
root@XXXXXX:~# apt update -y
apt upgrade -y
apt autoremove -y
apt autoclean -y
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:3 http://archive.canonical.com/ubuntu jammy InRelease
Hit:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
root@XXXXXX:~#

This is strange, as the script displays the four commands before executing only the first one.

Additionally, if I try to execute another command manually from the root prompt, nothing happens, until I press ^C.
Then, it returns to my local prompt.

I've also tried the second way : executing commands directly from the global script, by this way:

SSHPASS='mypassword' sshpass -e ssh -tt -o StrictHostKeyChecking=no root@XX.XX.XX.XX <<ENDSSH1
  apt update -y
  apt upgrade -y
  apt autoremove -y
  apt autoclean -y
ENDSSH1

Exactly same result!

Question 1: What am I doing wrong and how should I proceed ?
Question 2: Do you know a place where I could find this kind of scripts, that I could adapt to our needs ? I've searched for, but didn't find anything...

Peter Zhabin avatar
cn flag
You should really consider using Ansible for this purpose as you're currently trying to reinvent it..
Emmanuel FRANCOIS avatar
la flag
We are new to server administration, and our startup is at development step. We only want to setup quickly and as well as possible a development and test server. Before deploying our app at production stage, we plan to hire a good system administrator to setup and secure the production server. Today, we don't want to spend resources to learn and implement a solution like Ansible. We just want to use basic technology like bash scripting to make this VPS run and to be able to reset it quickly if we need to...
Peter Zhabin avatar
cn flag
Thing is, Ansible for your needs is much simpler than bash scripting..
Score:0
la flag

Ok, I found my mistake: the script file was created under Windows, so end of lines weren't interpreted correctly! Bash scripting files should ALWAYS be created on a Linux system...

Jorge Valentini avatar
in flag
You can write in Windows but using an editor that allows you to change line endings. For instance vscode will allow to change CR/LF (windows) to LF (linux/mac). Notepad++ allows this as well. Also if you already have the file with CR/LF in linux with your bash script, you can change line endings using sed or tr.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.