#!/bin/bash
some_array=($1)
echo "-- Setting-Up VM --"
for i in ${some_array[#]}; do
echo "VM #: $i"
case "$i" in
"1")
echo "Setting-Up VM $i"
sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root#10.xx.x.xx <<EOF
pwd
nohup yes | /etc/rc.d/init.d/lifconfig
su tarts
nohup yes | vncserver
sleep 10
exit
exit
EOF
;;
"2")
echo "Setting-Up VM $i"
sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root#10.xx.x.xx <<EOF
pwd
nohup yes | /etc/rc.d/init.d/lifconfig
su tarts
nohup yes | vncserver
sleep 10
exit
exit
EOF
;;
"3")
echo "Setting-Up VM $i"
sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root#10.xx.x.xx <<EOF
pwd
nohup yes | /etc/rc.d/init.d/lifconfig
su tarts
nohup yes | vncserver
sleep 10
exit
exit
EOF
;;
*)
echo "unknown VM!"
;;
esac
done
Can someone please guide me I have the above script which is executed when we run the script for instance ./vmSetup.sh "1 2 3" but this is executed sequentially. I had created this script but now I want to run the cases in the script i.e. 1, 2 and 3 in parallel. Can someone also tell me how to run for instance 8 cases in parallel?
Put each case ... esac statement in the background by ending it with &. Then use wait after the loop to wait for all the background processes to finish.
#!/bin/bash
some_array=($1)
echo "-- Setting-Up VM --"
for i in ${some_array[#]}; do
echo "VM #: $i"
case "$i" in
"1")
echo "Setting-Up VM $i"
sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root#10.xx.x.xx <<EOF
pwd
nohup yes | /etc/rc.d/init.d/lifconfig
su tarts
nohup yes | vncserver
sleep 10
exit
exit
EOF
;;
"2")
echo "Setting-Up VM $i"
sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root#10.xx.x.xx <<EOF
pwd
nohup yes | /etc/rc.d/init.d/lifconfig
su tarts
nohup yes | vncserver
sleep 10
exit
exit
EOF
;;
"3")
echo "Setting-Up VM $i"
sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root#10.xx.x.xx <<EOF
pwd
nohup yes | /etc/rc.d/init.d/lifconfig
su tarts
nohup yes | vncserver
sleep 10
exit
exit
EOF
;;
*)
echo "unknown VM!"
;;
esac &
done
wait
Why do you have a case statement at all when all these are identical?
#!/bin/bash
echo "-- Setting-Up VM(s) --"
for i in "$#"; do
case "$i" in
1) IP=1.2.3.4;;
2) IP=2.2.3.4;;
3) IP=3.2.3.4;;
*) echo "Invalid option '$i'" >&2; exit 1;;
esac
echo "Setting-Up VM $i"
sshpass -p root12 ssh -tt -o StrictHostKeyChecking=no root#$IP <<EOF &
pwd
nohup yes | /etc/rc.d/init.d/lifconfig
su tarts
nohup yes | vncserver
sleep 10
exit
exit
EOF
done
To run a job in background, just use &
sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root#$IP <<EOF &
It's just confusing because of the here-doc, but the & metacharacter still parses correctly on my system. Try it.
Also, rather than quoting all your options in one string and then parsing them back out to an array, why not just simplify the whole thing and let them come in as separate arguments? Call it as
./vmSetup.sh 1 2 3 # NOT "1 2 3" with quotes
and the loop becomes just
for i in "$#" # properly quoted, though wouldn't matter for 1 2 3
This whole thing seems a lot simpler and easier to maintain.
Another option, which is what I would do: create a file that is a list of the DNS/IP addresses you need, then pass that file as the lone argument.
#!/bin/bash
while read -r addr || [[ -n "$addr" ]]
do echo "Setting-Up VM $addr"
sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root#$addr <<EOF &
pwd
nohup yes | /etc/rc.d/init.d/lifconfig
su tarts
nohup yes | vncserver
sleep 10
exit
exit
EOF
done < "$1"
Better, add valid error checking first to make sure the file exists and is readable, etc, but as a simple case, this should work.
For even better, cleaner, safer code, read https://mywiki.wooledge.org/BashFAQ/001 and follow those suggestions. :)
If ./vmSetup.sh 1 works as expected, these should work:
parallel -j8 ./vmSetup.sh {} ::: 1 2 3 ... 100
seq 100 | parallel -j8 ./vmSetup.sh
Related
I have the following bash script:
#!/bin/bash
set command "pgrep -x 'gedit' "
ssh -t test#192.168.94.139 $command
Now, I want to include this as well in the other device:
if pgrep -x "gedit" > /dev/null
then
echo "Running"
else
echo "Not Running"
fi
How can I make the IF Statement run on the other device? I wasn't able to include it in the ssh.
I tried this:
set command "pgrep -x 'gedit' "
ssh -t test#192.168.94.139 '
if pgrep -x "gedit" > /dev/null
then
echo "Running"
else
echo "Not Running"
fi'
But it didn't work! maybe because there is no command at the beginning?
Thanks.
Invoke bash with heredoc:
ssh -t test#192.168.94.139 bash <<EOF
if pgrep -x "gedit" > /dev/null
then
echo "Running"
else
echo "Not Running"
fi
EOF
I would like to get the exit code of my remote command.
It's normally pretty easy, but I need to execute my command through a bastion host and I don't know take that with a easy methode.
So, this is a diagram of my architecture.
PC => Bastion => remote host
I used this code to execute my command and check the return code:
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -At ubuntu#$ip_bastion ssh -Aq -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu#$ip_server bash <<EOF
{ ${cmd} ; } 2>&1 | sed -e 's/^/$(colors_next exec)$region:$ip:$type${normal} /'
EOF
echo $?
if [[ $? -ne 0 ]]; then
error "Error during the command execution."
exit 1
fi
But, the return code is always equal 0, while my command failed (Return 255 on the final server).
Thanks for your help :)
If you are using bash, you can use the PIPESTATUS array variable to get the exit status of each element of the pipeline.
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -At ubuntu#$ip_bastion ssh -Aq -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu#$ip_server bash <<EOF
{ ${cmd} ; } 2>&1 | sed -e 's/^/$(colors_next exec)$region:$ip:$type${normal} /'
exit "\${PIPESTATUS[0]}"
EOF
remote_exitcode=$?
echo "remote_exitcode=${remote_exitcode}"
if [[ ${remote_exitcode} -ne 0 ]]; then
error "Error during the command execution."
exit 1
fi
The echo command is hurting you. You need to capture the value of $? before echoing it, because echo will reset it to 0, assuming that the echo command is successful.
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -At ubuntu#$ip_bastion ssh -Aq -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu#$ip_server bash <<EOF
{ ${cmd} ; } 2>&1 | sed -e 's/^/$(colors_next exec)$region:$ip:$type${normal} /'
EOF
rc=$?
echo $rc
if [[ $rc -ne 0 ]]; then
error "Error during the command execution."
exit 1
fi
Optionally, you may want to exit $rc so that your script exits with the same return code that your SSH command exited with, which might in some cases be helpful in figuring out what went wrong.
I have a small list of servers, and I am trying to add a user on each of these servers. I can ssh individually to each server and run the command.
sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
I wrote a script to loop through the list and run this command but I get some errors.
#!/bin/bash
read -p "Enter server list: " file
if [[ $file == *linux* ]]; then
for i in `cat $file`
do
echo "creating amurug on" $i
ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
echo "==============================================="
sleep 5
done
fi
When I run the script it does not execute the command.
creating amurug on svr102
Usage: useradd [options] LOGIN
Options:
What is wrong with my ssh crommand in my script?
Try this script:
#!/bin/bash
read -p "Enter server list: " file
if [[ "$file" == *linux* ]]; then
while read -r server
do
echo "creating amurug on" "$server"
ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
-e 2014-12-12 -g users -u 1470 amurug"
echo "==============================================="
sleep 5
done < "$file"
fi
As per man bash:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
Having a problem with using nohup in a script. The script works properly if nohup is not used
to start the process. The following error is received when run:
./iper.sh: line 16: syntax error near unexpected token `;'
./iper.sh: line 16: ` [Yy]*) nohup iperf -s > /dev/null 2>&1&; break;;'
Here is the full script:
#!/bin/bash
echo "Checking to see if Iperf is running:"
sleep 2
ps cax | grep iperf > /dev/null
if [ $? -eq 0 ]; then
echo "Iperf is running."
else
echo "Iperf is not running."
fi
sleep 2
while true; do
read -p "Press Y to start Iperf or N to exit: " yn
case $yn in
[Yy]*) nohup iperf -s > /dev/null 2>&1&; break;;
[Nn]*) exit;;
esac
done
What is happening?
If you're going to terminate your command with & to put it to background, do not terminate it with another semicolon ; as well:
[Yy]*) nohup iperf -s > /dev/null 2>&1& break;;
Previously
2>&1&;
I guess you have an extra & in 2>&1&
Change it to 2>&1
Check with below script
#!/bin/bash
echo "Checking to see if Iperf is running:"
sleep 1
if `pgrep iperf >/dev/null 2>&1`
then
echo "iperf Running"
else
echo "iperf not Running"
fi
sleep 1
while true; do
echo "Do you wanna start Iperf (y/n)"
read -n 1 ch; p=`echo ${ch} | tr A-Z a-z`
case $p in
y)nohup iperf -s > /dev/null 2>&1 break;;
n)exit;;
*)continue;
esac
done
when this been executed it waits for user to press*ctrl + c* to come out
if you are using 2>&1&(for continuing without user interference)allowing user to do other work
replace below line in y) condition
nohup iperf -s > /dev/null 2>&1& break;;
I have a script like that:
su lingcat -c PHPRC\=\/home\/lingcat\/etc\/php5\
PHP_FCGI_CHILDREN\=4\ \/usr\/bin\/php\-loop\.pl\ \/usr\/bin\/php5\-cgi\ \-b\
127\.0\.0\.1\:9006\ \>\>\/home\/lingcat\/logs\/php\.log\ 2\>\&1\ \<\/dev\/null\ \&\
echo\ \$\!\ \>\/var\/php\-nginx\/135488849520817\.php\.pid
This is working. But there is too many \ in the script, they make the code unreadable. So, I wrote a new shell script:
#!/bin/sh
case "$1" in
'start')
su biergaizi -c "PHPRC=/home/biergaizi/etc/php5 PHP_FCGI_CHILDREN=2
/usr/bin/php-loop.pl /usr/bin/php-cgi -b /var/run/virtualhost/php5-fpm-biergaizi.test.sock >>/home/biergaizi/logs/php.log 2>&1 </dev/null &
echo $! > /var/php-nginx/biergaizi.test.php.pid"
RETVAL=$?
;;
'stop')
su biergaizi -c "kill `cat /var/php-nginx/biergaizi.test.php.pid` ; sleep 1"
RETVAL=$?
;;
'restart')
$0 stop ; $0 start
RETVAL=$?
;;
*)
echo "Usage: $0 { start | stop }"
RETVAL=1
;;
esac
exit
But /var/php-nginx/biergaizi.test.php.pid is empty.
What's wrong?
The .pid file is empty, because $! gets substituted by the shell executing your script, instead of the shell executing the commands you pass through su. And as there is no recently started background command in your script, it substitutes an empty string. So, shell started by su executes simply echo > /var/php-nginx/biergaizi.test.php.pid.
To prevent that, quote your command passed to su using single quotes, instead of double quotes. It is better to do that to the "stop" command as well. Like this:
su biergaizi -c 'PHPRC=/home/biergaizi/etc/php5 PHP_FCGI_CHILDREN=2
/usr/bin/php-loop.pl /usr/bin/php-cgi -b /var/run/virtualhost/php5-fpm-biergaizi.test.sock >>/home/biergaizi/logs/php.log 2>&1 </dev/null &
echo $! > /var/php-nginx/biergaizi.test.php.pid'
And this:
su biergaizi -c 'kill `cat /var/php-nginx/biergaizi.test.php.pid` ; sleep 1'
See http://www.gnu.org/software/bash/manual/html_node/Quoting.html for details.
try this:
Escape $ from $!, before passing to su -c.