Changes required in my shell script in Linux - linux

following is the script:
#!/bin/bash
declare pspath=""
declare user=""
declare password=""
declare password=""
for i in "$#"; do
case $i in
--pspath=*)
pspath="${i#*=}"
shift
;;
--user=*)
user="${i#*=}"
shift
;;
--password=*)
password="${i#*=}"
shift
;;
esac
--ip=*)
ip="${i#*=}"
shift
;;
esac
done
command="$pspath -username $user -password $password -ip $ip
$t="sudo pwsh -Command "$command""
echo $t
command i'm running:
./test.sh --pspath=test.ps1 --user=test --password=test$123 --ip=0.0.0.0
output coming:
sudo pwsh -Command test.ps1 -username test -password test23 -ip 0.0.0.0
expected output:
sudo pwsh -Command test.ps1 -username 'test' -password 'test$123' -ip 0.0.0.0
can anyone help me here, please?
PS: command cannot be changed, so need to make changes in shell script itself.
Thanks in Advance

You wrote
./test.sh --pspath=test.ps1 --user=test --password=test$123 --ip=0.0.0.0
You wanted
./test.sh --pspath=test.ps1 --user=test --password='test$123' --ip=0.0.0.0
As written, you are passing in this password:
./test.sh --pspath=test.ps1 --user=test --password=test23 --ip=0.0.0.0
Once you've done that, the $1 characters are lost forever
and cannot be reconstructed by the test.sh script.

Related

Shell script with case statement and loop for not working

trying to connect in servers list and execute some commands, this script dont execute second step (AIX)
SO=uname -s
for server in $(cat maq)
do
case $SO in
Linux)
echo "Connecting in $server"
ssh $server
echo "my system is $SO"
"exit"
;;
AIX)
echo "Connecting in $server"
ssh $server
echo "my system is $SO"
"exit"
;;
esac
done
They execute "my server is Linux" for all case choice! Any Help?
(Edited to make it more clear)
You seem to confuse the local OS with the remote OS. The former won't change during the cycle, only the latter. Here is a complete example.
LocalOs="$(uname -s)"
case "$LocalOs" in
Linux)
echo "Local OS is $LocalOs";
;;
AIX)
echo "Local OS is $LocalOs";
;;
*)
echo "Local OS is $LocalOs";
;;
esac
while read server; do
RemoteOs=$(ssh </dev/null "$server" 'uname -s')
case "$RemoteOs" in
Linux)
echo "OS on $server is $RemoteOs"
;;
AIX)
echo "OS on $server is $RemoteOs"
;;
*)
echo "OS on $server is $RemoteOs"
;;
esac
done <"maq"
Edit: also you misunderstood how ssh works in a script. This doesn't work:
ssh remoteuser#remotehost
commands to be executed on remotehost
exit
This works:
ssh remoteuser#remotehost <<EOF
commands to be executed on remotehost
EOF
Simpler case:
ssh remoteuser#remotehost 'command(s) to be executed on remotehost'

bash script to add and remove users

I am a beginner in bash scripting and I have created a bash script to add users and remove users on Linux. But since I am facing some issues with the script not really major issues but would be helpful if anyone could point me how to improve the script and the worst practice I am doing the script would be helpful
however the problem I have noticed is that the script takes -a to add a user -d to remove user and -h to get help the -a flag as 2 optional arguments -p for password and -s for shell so the command would be
./useradd.sh -a user -p password -s shell
this works as expected and the user is added to the system but the problem I am facing is that if I do not enter -a flag and specify the -s and -p flag the script is just exited I want to show a clear idea to the user why it exited and there is so many such errors I am assuming but I have not tested it out so much any help would be appreciated, so here is my script
#!/bin/bash
## checking if the user is privileged or not
if [[ $EUID != 0 ]]
then
echo "Script has to be ran as root or sudo"
echo "Aborting"
exit 101
fi
## creating help functions
function usage() {
echo "usage: ${0} -a <user> -p <password> -s <shell> | ${0} -d <user> | ${0} -h"
}
function help() {
echo "$0 - Script to add of remove users"
echo "-a - Add a new user"
echo " -p - Set password while creating user if not mentioned will not set any password by default"
echo " -s - Set a shell for the user default is /bin/bash if none specified"
echo "-a - Remove a user"
echo "-h - Print this help text"
}
if [[ "$#" -lt "1" ]]; then
echo "Argument has to be provided see $0 -h"
fi
shell=/bin/bash
password=$(openssl rand -base64 32)
while getopts :a:d:h opt; do
case $opt in
a) user=$OPTARG
while getopts :p:s: test
do
case $test in
p) password=$OPTARG;;
s) shell=$OPTARG;;
/?) echo "The provided flag is not identified see $0 -h"
exit;;
:) echo "$OPTARG requires arguments see $0 -h"
exit;;
esac
done
if [[ "$1" != "-a" ]]
then
echo "You have to specify username using -a flag see $0 -h"
fi
useradd -m $user -s $shell
echo "$user":"$password" | chpasswd
echo "The password for the $user is $password";;
d) userdel -f $OPTARG
if [[ $? == 0 ]]
then
echo "user has been removed"
else
echo "There was some error removing the user"
fi;;
h) help
exit;;
/?) echo "$OPTARG option not valid";;
:) echo "$OPTARG requires argument";;
esac
done
Please show your code! I usually process args with case ... in likes :
#!/bin/bash
while [[ $# -gt 0 ]]; do
case $1 in
"-a")
echo "-a is $2"
shift 2;;
"-d")
echo "-d is $2"
shift 2;;
esac
done

bash unable to export the variable to script

i am stuck with my piece of code any help is appreciated. This is the piece of code i am executing from jenkins.
#!/bin/bash
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value|[0],PrivateIpAddress]' --output text | column | grep devtools > devtools
ip=`awk '{print $2}' devtool`
echo $ip
ssh ubuntu#$ip -n "aws s3 cp s3://bucket/$userlistlocation . --region eu-central-1"
cd builds/${BUILD_NUMBER}/
scp * ubuntu#$ip:/home/ubuntu
if [ $port_type == "normal" ]; then
if [ $duplicate_value == "no" ]; then
if [ $userlist == "uuid" ]; then
ssh ubuntu#$ip -n "export thread_size='"'$thread_size'"'; filename=$(echo $userlistlocation | sed -E 's/.*\/(.*)$/\1/') ; echo $filename ; echo filename='"'$filename'"'; chmod +x uuidwithduplicate.sh; ./uuidwithduplicate.sh"
fi
fi
fi
fi
userlistlocation --> is an user input it can be in any format /rahul/december/file.csv or simply it can be file.csv.
Through sed command i am able to get the output and stored in "filename" variable.
But when i try to echo $filename it's printing as echo $filename it should print as file.csv
this file.csv will be the source file for one more script to run i.e for uuidwithduplicate.sh
both userlistlocation and thread_size are specified through Jenkins job parameters.
I am not facing issues while exporting thread_size, only issue is with filename.
It's just printing echo $filename --> it should print file.csv
Breaking down the ssh command:
ssh ubuntu#$ip -n "export thread_size='"'$thread_size'"'; filename=$(echo $userlistlocation | sed -E 's/.*\/(.*)$/\1/') ; echo $filename ; echo filename='"'$filename'"'; chmod +x uuidwithduplicate.sh; ./uuidwithduplicate.sh"
Into segments of single/double quoted items
"export thread_size='"
'$thread_size'
"#'; filename=$(echo $userlistlocation | sed -E 's/./(.)$/\1/') ; echo $filename ; echo filename='#"
'$filename'
"'; chmod +x uuidwithduplicate.sh; ./uuidwithduplicate.sh"
Note: On the 3rd token, an '#' was added between double quotes and single quote to make it more readable. Not part of the command.
On surface few issues:
The '$thread_size' should be "$thread_size" to enable expansion
The 'echo $filename' is in double quote, resulting in expansion on the local host, where as setting filename=$(echo ...) is executed on the remote host.
There are two echo for filename, not sure why
Proposed solution is to move the setting of filename to the local host (simplify command), and move the thread_size into double quotes. It is possible to put complete command into single double-quoted item:
filename=$(echo $userlistlocation | sed -E 's/.*\/(.*)$/\1/')
ssh localhost -n "export thread_size='$thread_size'; echo '$filename' ; echo filename='$filename'; chmod +x uuidwithduplicate.sh; ./uuidwithduplicate.sh"

Calling multiple shell scripts within a script on different virtual machines

I am trying to create shell scripts which will setup Zookeeper Server in one VM, and its corresponding Zookeeper Clients in different VM's so i written a shell script as below
#!/bin/bash
ZOOKEEPER_SERVER_IP="1.2.3.4"
while read ipaddress zookeepertype number
do
echo -e "Setting up the Zookeepers \n"
echo $ipaddress
if [ "${zookeepertype}" = 'zookeeperserver' ]; then
echo "Setup Zookeeper Server"
#ZOOKEEPER_SERVER_IP = $ipaddress
#echo $ZOOKEEPER_SERVER_IP
#echo $ipaddress
sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/ZooKeeper_Server_Script.sh ubuntu#$ipaddress:/home/ubuntu/
ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress /home/ubuntu/ZooKeeper_Server_Script.sh
echo "This script is about to run ZooKeeper_Server_Script."
echo "The server script has completed.";
#sleep 30
exit 1
fi
echo -e $ZOOKEEPER_SERVER_IP
if [ $zookeepertype = "zookeeperclient" ] ; then
echo "Setup Zookeeper Client"
echo $ipaddress
sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/ZooKeeper_Client_Script.sh ubuntu#$ipaddress:/home/ubuntu/
ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress
#mkdir /home/ubuntu/keyfiles
#exit
#sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/abc/network/test/keyfiles/* ubuntu#$ipaddress:/home/ubuntu/keyfiles
#sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/abc/test/simple/abc.json ubuntu#$ipaddress:/home/ubuntu/
#ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress
#chmod 777 ZooKeeper_Client_Script.sh
#echo "This script is about to run ZooKeeper_Client_Script."
#sh ./ZooKeeper_Client_Script.sh $ZOOKEEPER_SERVER_IP
echo "The client script has completed."
#exit
fi
#Separating Runhosts File
done < setupZkinput.txt
the input file is
1.2.3.4 zookeeperserver 1
5.6.7.8 zookeeperclient 2
9.10.11.12 zookeeperclient 3
The issue that i am facing is
1) Only the server setup is being done , i.e the script is exiting after the first line
2)Not able to assign the server ip dynamically , in the line ZOOKEEPER_SERVER_IP = $ipaddress
Thanks
What is the default permission you are setting up after copying a file to the server?
remember it required execute permission in order to execute script.

Checking result of executed command in a Bash script

I want to run my VirtualBox after my I logged in. To do that, I have to check if it's already running or not. If not, then start the VM.
My problem is that I can not put the result of VirtualBox command into a variable in Bash.
I've created a function to get the exit parameter, but I get a "Syntax error: Invalid parameter '|'" error message.
How can I make it work?
This is my Bash script:
########## Starting om-server VirtualBox ##############
function Run_VM {
"$#"
local status=$?
if [ $status -eq 0 ]; then
echo "Starting om-server VM!";
VBoxManage startvm "om-server"
sleep 30
ssh root#192.168.1.111
else
echo "om-server VM is running!"
fi
return $status
}
check_vm_status="VBoxManage showvminfo \"om-server\" | grep -c \"running (since\""
Run_VM $check_vm_status
########## Starting om-server VirtualBox ##############
In order to do what you want, you would have to use command substitution:
check_vm_status="$(VBoxManage showvminfo \"om-server\" | grep -c \"running (since\")"
The caveat is that the instructions will be executed during the variable expansion (i.e. during the variable allocation).
If you want to execute your instructions only during your function, you could use a eval:
function Run_VM {
eval "$#"
local status=$?
if [ $status -eq 0 ]; then
echo "Starting om-server VM!";
VBoxManage startvm "om-server"
sleep 30
ssh root#192.168.1.111
else
echo "om-server VM is running!"
fi
return $status
}
check_vm_status="VBoxManage showvminfo \"om-server\" | grep -c \"running (since\""
Run_VM $check_vm_status
Note that eval brings a lot of issues.

Resources