Shell script with case statement and loop for not working - linux

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'

Related

Is there a way that you can use the "if" branch to check whether a USB stick is connected?

Is there a way like that you can use the "if" branch to check whether a USB-Stick is connected? And if the stick is not connected, a message should then be issued.
Something like:
if [-e /sdb1]
then
cp /home/backup/* /sdb1
rm -r /home/backup/*
echo "your files are successfully transferred."
else
echo "please be sure, if your USB-Stick is connected"
fi
No, the mount point exists whether or not a USB device is connected.
Something like this should suffice:
if [[ $(df | grep "/sdb1") && -d /sdb1 && -w /sdb1 ]]
That is if, of course, you have actually created the directory /sdb1/.
Use the findmnt utility (installed by default on RH and Ubuntu, at least)
findmnt /backup
echo $?
1
Return of 1 means not mounted.
Here is some sample code using it with an if statment
findmnt /backup >/dev/null
if [ $? = 0 ]; then
echo "It's mounted all right"
else
r=$(( RANDOM % 4 ))
echo "USB is not mounted."
case $r in
0) echo "Check the couch cushions."
;;
1) echo "I think I saw it in the kitchen."
;;
2) echo "Sign up for Prime and get free shipping!"
;;
3) echo "The dog ate it."
;;
esac
fi

Nested Case statement in shell script | Getting Syntax error

I am trying to run below shell script but i am getting syntax error.
script.sh env1 ManagedSvr1
line 29: warning: here-document at line 6 delimited by end-of-file (wanted `EOF')
line 30: syntax error: unexpected end of file
#!/bin/bash
case "$1" in "env1")
ssh weblogic#hostname1 << EOF
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac
;;
"env2")
ssh weblogic#hostname2 << EOF
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac
;;
esac
Your Here Docs specify EOF to end them (<< EOF) but you never have an EOF to end them. Note that EOF doesn't mean End of File, it means the string 'EOF'. https://en.wikipedia.org/wiki/Here_document has examples.
I'm not sure what you're hoping to accomplish, but it looks to me that you need to specify which file to tail. Are you hoping to pass the inner case into the remote shell on the server you're sshing into? It would simplify your code to set your filename and servername first and then ssh and execute the command. Actually, I don't see much purpose in your inner case statements anyway. Instead of wrapping everything in the 'env' case, you could just set the hostname to a variable. And then the "servername" can just be interpolated into the filesystem path. something like this seems like a simple approach:
#!/bin/bash
case "$1" in
env1)
hostname="hostname1"
;;
env2)
hostname="hostname2"
;;
esac
echo ssh weblogic#$hostname tailf /app/Oracle/Middleware/domains/dq/servers/$servername/logs/$servername.log
Seems to work simply and straight forward:
servername=ManagedSvr1 ./t.sh env1
ssh weblogic#hostname1 tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
Take out the "echo" to actually execute the ssh.
You actually don't need "HERE" docs, per say, you're using one case statement which is taking standard input to your SSH command. take single quotes as input to ssh This lets you do multiline input in peace. Quoting your "EOF" and terminating it, is also acceptable usage.
#!/bin/bash
case "$1" in
"env1")
ssh -tt weblogic#hostname1 <<< '
case "$server" in
"ManagedSvr1")
cat /var/log/syslog
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac '
;;
"env2")
ssh -tt weblogic#hostname2 <<< '
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac '
;;
esac
This would be an easier way to manage the script. Especially as you're not utilising remote user change: Lets you set all your variables locally, and then just connect and execute the single command.
#!/bin/bash
server="$2"
case "$1" in
"env1")
hostname="hostname1"
case "$server" in
"ManagedSvr1")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr1.log)
;;
"ManagedSvr2")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log)
;;
esac
;;
"env2")
hostname="hostname2"
case "$server" in
"ManagedSvr1")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log)
;;
"ManagedSvr2")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log)
;;
esac
;;
*)
exit 1
;;
esac
if [[ $? == 0 ]]
then ssh weblogic#$hostname $remote_command
fi

Shell Script for automate SSH logins

I have created a shell script for login my servers:
#!/bin/sh
echo "Please hostname ..."
while :
do
read INPUT_STRING
case $INPUT_STRING in
host1)
`bash |sshpass -p 'qwerty123' ssh -o StrictHostKeyChecking=no root#192.168.1.4`
;;
host2)
`sshpass -p 'qwerty123' ssh -o StrictHostKeyChecking=no root#192.168.1.8`
;;
*)
echo "Sorry, I don't understand"
break;
;;
esac
done
echo
echo "Bye"
But I canĀ“t login to the server. I think the issue may be changing the shell on the scrip while accessing the server.
Please help.
The back tics are causing your program to fail. Remove the backticks and also the bash | and you may have a chance. All of the comments I've read are correct, though. You should consider using a public / private key pair. You could make your logins to the hosts easier with ~/.ssh/config.
Try this code for now:
#!/bin/sh
echo "Please hostname ..."
while :
do
read INPUT_STRING
case $INPUT_STRING in
host1)
sshpass -p 'qwerty123' ssh -o StrictHostKeyChecking=no root#192.168.1.4
;;
host2)
sshpass -p 'qwerty123' ssh -o StrictHostKeyChecking=no root#192.168.1.8
;;
*)
echo "Sorry, I don't understand"
break;
;;
esac
done
echo
echo "Bye"
Here's a one liner that pretty much does the same thing:
select INPUT_STRING in host1 host2; do ssh $INPUT_STRING; done
However, you'll need to create a ~/.ssh/config file so that host1 and host2 are mapped to IP addresses:
Host host1
HostName 192.168.1.4
User root
StrictHostKeyChecking no
Host host2
HostName 192.168.1.8
User root
StrictHostKeyChecking no
Also, to avoid the sshpass, you can use commands such as ssh-keygen and ssh-copy-id. Plenty has been written about ssh-keygen and ssh-copy-id so I won't go into it here (the keywords should be enough to get you far).

Case statement not working in my shell script

I'm trying to write a shell script for Mac OSX Mountain Lion (10.8.2). However, no matter what I try, I can't get the switch statement to work. He is a very simplified version of the script:
#!/bin/bash
while getopts "ei" opt; do
case $opt in
e)
echo "Got option e"
;;
i)
echo "Got option i"
;;
\?)
echo "Invalid option: -$opt"
;;
esac
done
No matter what I try, I don't get any output. Any ideas? I've done scripts like this in the past.
You might be passing the options incorrectly don't forget the -
script.sh -e
Note you should also exit after an invalid option:
\?)
echo "Invalid option: -$opt"
exit 1
;;
Try replacing
\?)
echo "Invalid option: -$opt"
;;
with:
*)
echo "Invalid option: -$opt"
;;

How do I set gsettings during startup?

I have a pxe server which hosts a live image of Ubuntu 12.04 and I would like to enable VNC on it.
Normally I would do this with the following command:
$ gsettings set org.gnome.Vino enabled true
However, since this live OS lives in RAM it will need to do this on bootup every time. The problem is for some reason it will not work with a script in /etc/init.d/... For the life of me I cannot figure out why gsettings doesn't work in this context..
For reference this is the script I am using in /etc/init.d:
#!/bin/bash
log=/var/log/gsettings.log
#Needed for some reason.. received info from http://stackoverflow.com/questions/10374520/gsettings-with-cron
sessionfile=`find "${HOME}/.dbus/session-bus/" -type f`
export $(grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d')
set_gsettings()
{
echo "Inside set_gsettings" >> $log
#Enable vino
gsettings set org.gnome.Vino enabled true 2>&1 >> $log
gsettings set org.gnome.Vino prompt-enabled false 2>&1 >> $log
}
case "$1" in
start)
echo "Inside IT-gsettings" >> $log
set_gsettings
;;
restart|reload|force-reload)
/etc/init.d/IT-gsettings start
;;
stop)
:
;;
*)
log_success_msg "Usage: /etc/init.d/IT-gsettings {start|stop|restart|reload|force-reload}"
exit 1
;;
esac
exit 0
In short, how can I set gsettings on startup?
It is likely the script runs when still there is no session available.
Given it is a live CD and you have control of it, you might want to change the defaults values in the schema. For vino, you should change the default values in /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml.

Resources