SSH Bash script issues with if statement - linux

I'm trying to learn how to write Bash scripts. I have this script to update my servers through ssh. I'm trying to add a check and a conditional to determine if the OS uses Yum or Apt then it will run the appropriate update commands. The if else statement seems to be wrong but I'm not sure how to correct this.
Here is the script:
#!/bin/bash
USERNAME="root"
HOSTS="host1 host2 host3"
apt_run="apt update && apt -y upgrade"
yum_run="yum check-update && yum -y update"
for HOSTNAME in ${HOSTS} ; do
ssh -l ${USERNAME} ${HOSTNAME}
find_os=$( command -v yum || command -v apt-get ) || echo "Neither
yum nor apt-get found"
if [[ $find_os='yum' ]]
then
"${yum_run}"
else
"${apt_run}"
fi
done

Here is my script for my virtual machines.
#!/bin/bash
hosts=(
leap151 kali ubuntu omv
)
for hostname in "${hosts[#]}"; do
ssh -t root#"$hostname" << 'EOF'
if type -P zypper >/dev/null; then
command zypper ref && command zypper up
elif type -P apt-get >/dev/null; then
command apt-get update && command apt-get upgrade
else
echo 'Neither zypper nor apt found!' >&2
exit 127
fi
EOF
done
Use an array for the host. Since you're using bash the builtin type is fine just for searching the executable within your PATH. See help type for more info. Use the -t option in ssh also use a heredoc just what I have/did. The exit 127 is what the shell would exit if there are no executable see man 1p exit.

Related

a weird ssh file appearing from nowhere with name pbcopyfy in .ssh directory

Well not a question as such, but more of a brainstorming thing or better say an issue. Well today I came to my server and realised that i can't git pull due to the keys being denied which was okay because for sure there were no keys in .ssh directory. All deleted and a new file (pbcopyfy there). Not sure what this means though. The content of the file are shown below:
#!/bin/sh
# Copyright (C) 2009-2017 Three Nine Consulting
# Always good practice to update packages. However ask user if they would like to do so
# For explanation on how this works and why check out https://garywoodfine.com/use-pbcopy-on-ubuntu/
read -p "Do you want to update your package repositories before proceeding ? " -n 1 -r
echo #adding new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y
fi
# Check to see if Xclip is installed if not install it
if [ $(dpkg-query -W -f='${Status}' xclip 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
echo 'xclip not installed .... installing now!'
sudo apt install xclip -y;
fi
# Add the aliases to the .bashrc
echo 'updating bash profile'
echo "#pbcopy & pbpaste aliases" >> ~/.bashrc
echo "alias pbcopy='xclip -selection clipboard'" >> ~/.bashrc
echo "alias pbpaste='xclip -selection clipboard -o'" >> ~/.bashrc
source ~/.bashrc
echo 'Complete! happy PbCopy'
Is this something I should be worried about or should I just get new keys and go up and running with new keys?

Multiline bash command in Jenkins pipeline

I have the following sh command in my Jenkinsfile which does not work because it tries to execute the last "DATA" as a command.
If I move last "DATA" to the beginning of the line it works but is not as beautiful as I want.
Is there a way to the indention in this case?
sh """
sshpass -p 'password' ssh -o StrictHostKeyChecking=no appsadm#$backup_registry <<DATA
sudo /etc/init.d/docker stop || true
sudo yum remove -y docker-engine.x86_64
sudo rm -fr /var/lib/docker /var/log/docker
sudo rpm -iUvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm || true
sudo yum update -y
sudo yum -y install docker-io
sudo sed -i 's#other_args=.*#other_args="--insecure-registry $official_registry:5000"#g' /etc/sysconfig/docker
sudo /etc/init.d/docker start
DATA
"""
I know this is an old question, but I had ran into this at some point, and eventually ended up using stripIndent()
steps {
echo 'Deploying....'
sh """
ssh somewhere <<EOF
cd somewhere
do some more stuff
EOF
""".stripIndent()
}
That way you can still keep your indentations and formatting
because <<DATA specifies the end of here-doc <<-DATA suppress leading tabs but not spaces
cat <<-DATA
hello
<tab>DATA
another option is to add spaces in marker
cat << " DATA"
hello
DATA
Edit: We don't need to use EOF, simply put the semicolon at the end of statement on multiline shell script as shown below
sh """ if [ -d /opt/tomcat/apache-tomcat-8.5.38/webapps/ROOT ] ;
then ;
ssh $USERNAME#$DEV_HOSTNAME 'sudo rm -rf /opt/tomcat/apache-tomcat-8.5.38/webapps/ROOT' ;
echo 'ROOT directory deleted successfully' ;
fi ;
"""

unable to execute shell script as a user

I've created one script for environment setup. I need Oracle to be installed as part of the script. below is the script i've written (pardon for errors, i'm new). The problem is, when i executed it, they are running fine individually, but collectively, the script is exiting and failing after "UNZIP" command. Can someone please help?
sudo -i -u oracle mkdir /home/oracle/dump
sudo -i -u oracle cd /home/oracle/dump
JAR1="p13390677_112040_Linux-x86-64_1of7.zip"
JAR2="p13390677_112040_Linux-x86-64_2of7.zip"
folder="/home/oracle/database"
if test -s "$JAR1"
then
echo "$JAR1 exists"
else
sudo -i -u oracle
wget XXXp://XXXXXX/XXXXXX/download/database/11.2.0.4/p13390677_112040_Linux-x86-64_1of7.zip
fi
if test -s "$JAR2"
then
echo "$JAR2 exists"
else
sudo -i -u oracle
wget XXXp://XXXXXXX/software/download/database/11.2.0.4/p13390677_112040_Linux-x86-64_2of7.zip
fi
if test -s "$folder"
then
echo "$folder exists"
else
unzip /home/oracle/p13390677_112040_Linux-x86-64_1of7.zip
unzip /home/oracle/p13390677_112040_Linux-x86-64_2of7.zip
fi
sudo -i -u oracle ORACLE_HOSTNAME=`hostname`
sudo -i -u oracle ./runInstaller $ORACLE_HOSTNAME -silent -responseFile /home/oracle/db.rsp
cd /opt/db/oraInventory
./orainstRoot.sh
cd /opt/db/oracle/11.2.0
./root.sh

cannot 'sudo' inside of bash if statement

I've dual linux boot i'm newbie in bash
when running the following script i got strange error:
if [[ 'grep -i fedora /etc/issue' ]]; then
echo "the OS is Fedora"
$(sudo yum update -y && sudo yum upgrade -y)
else
echo "the OS is Ubuntu"
$(sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y)
fi
error : ./server_update.sh: line 9: Loaded: command not found
It's attempting to execute the output of your apt-get/yum commands, lose the $(..)
You also have an issue at the start:
if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
is the correct way to check if a string exists.
Your code should then look like this:
if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
echo "the OS is Fedora"
sudo yum update -y && sudo yum upgrade -y
else
echo "the OS is Ubuntu"
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y
fi

How to write an install script for ubuntu

I just developed an opengl game in linux (ubuntu).. Now I would like to write a setup script for the same which installs the game directly into the apt using the command..
sudo apt-get install ...
so that it runs from anywhere throughout linux without going into the specified folder for the game. Anyone knows how to do that ?
http://blog.boxedice.com/2010/02/05/how-to-create-a-debian-deb-package/
http://ptspts.blogspot.com/2010/02/how-to-create-debianubuntu-package-deb.html
http://wiki.debian.org/HowToPackageForDebian
etc
#!/bin/bash
set -eu -o pipefail # fail on error , debug all lines
sudo -n true
test $? -eq 0 || exit 1 "you should have sudo priveledge to run this script"
echo installing the must-have pre-requisites
while read -r p ; do sudo apt-get install -y $p ; done < <(cat << "EOF"
perl
zip unzip
exuberant-ctags
mutt
libxml-atom-perl
postgresql-9.6
libdbd-pgsql
curl
wget
libwww-curl-perl
EOF
)
echo installing the nice-to-have pre-requisites
echo you have 5 seconds to proceed ...
echo or
echo hit Ctrl+C to quit
echo -e "\n"
sleep 6
sudo apt-get install -y tig

Resources