Shell Script for automate SSH logins - linux

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).

Related

My script is working fine butit is not taking next server from the server list [duplicate]

This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Closed yesterday.
My script is working fine butit is not taking next server from the server list
#!/bin/bash
# Read in server names from afile
while read -r server; do
# Check if server is reachable
if ping -c 1 "$server" &> /dev/nul l; then
echo "Server $server is reachable."
# Check if mount point exists
if ssh "$server" "[ -d /path/to/mount/point ]"; then
echo "Mount point exists on server $server."
else
echo "Mount point does not exist on server $server."
fi
else
echo "Server $server is not reachable."
fi
done < servers.txt
For an example 192.168.0.1 and 192.168.0.2 is there in server list file .but it is showing output for 192.168.0.1 . It is not showing output for 192.168.0.2
As pointed out in the comments section, ssh command is consuming your script stdin.
Add -n option to ssh should prevent this (see this).
So change this line:
if ssh "$server" "[ -d /path/to/mount/point ]"; then
To:
if ssh -n "$server" "[ -d /path/to/mount/point ]"; then

How can I always run a command automatically after doing rlogin to a remote server?

I use my script when connecting to a remote server.
#!/bin/bash
host=$1
root=$2
case "$host" in
"1" ) host="server1";;
"2" ) host="server2";;
esac
if [ -z "$root" ];then
rlogin -l user $host
else
rlogin -l root $host
fi
Something like this.
What I want more is to run PS1="\[\e[34;1m\]\u#\[\e[36;1m\]\h:\[\e[32;1m\]\w:> \[\e[0m\]" after connecting to the remote server, which makes the terminal visually good.
Editting .bashrc cannot be done because this remote server is used my multiple users.
I tried
rlogin -l user $host && PS1="\[\e[34;1m\]\u#\[\e[36;1m\]\h:\[\e[32;1m\]\w:> \[\e[0m\]"
but this didn't work.
How can I achieve this?

Ping multiple IP's without exiting from SSH using shell script

I have list of IP's which has to be pinged each other. Once I SSH to IP-1, i should ping all IP's in a loop before I come out of the loop.
I have tried the below..
for name in "${ip[#]}";
do
status=$(ssh -n -o LogLevel=QUIET -t -t -o StrictHostKeyChecking=no
ubuntu#$node ping -W 2 -q -c 5 $name")
if [ "$?" -eq "2" ]; then
echo -e "$(tput setab 7) $(tput setaf 1)$(date) $i unable to ping $name
$(tput sgr0)"
fi
done
This code is working. However every time it requires to do SSH, which is having a performance impact as i'm having more than 100 IP's in the list.
Can I get any help on this?
You could just make this list part of the command that you run on your target host, something like this:
ips=( "10.0.0.1" "10.0.0.2")
ssh serverName 'for i in '${ips[#]}'; do ping ${i} -c1; done'
Note the breaking single-quote to pass the array.
Edit:
Just to have it mentioned here: the tool "fping" is quite right for the job. It would give you just the list you asked for:
ips=( "10.0.0.1" "10.0.0.2")
ssh serverName 'fping -a '${ips[#]}' 2>/dev/null'
Cupcake is right about the possible problems that arise when you passing the list as suggested having entries containing whitespaces. In this special case, however, there are no whitespaces to be expected.
This should give you the List of IPs without fping
ips=( "10.0.0.1" "10.0.0.2")
ssh serverName 'for host in '${ips[#]}'; do if ping -c1 -w1 ${host} >/dev/null 2>&1; then echo ${host};fi;done'

how to catch when the connectivity is lost in bash?

i have the following script
rc=0
while [ $rc -ne 1 ]; do
ping -c1 someip
if [ "$?" -ne "0" ]; then
echo "lost ip"
else
sshpass -p password ssh user#ip 'date +"%s"'
sshpass -p password ssh user#ip 'command1'
sshpass -p password ssh user#ip 'command2'
# some other commands
fi
done
the problem is when ping lost connectivity when the script is executing the external commands through ssh, the script is hang even when the external machine recover its internet connectivity, so i was wondering how can i solve that situation.
Thanks

how to hide specific standard output of shell command

I am doing ssh of a "if statement" to a remote server.
Example:
========== Start of Script========
#!/bin/sh
CMD='if [ ! -d "/user/directory" ]; then echo -e "user directory missing"; else echo -e "present"; fi;
ssh remoteserver "$CMD"
==========End of script==========
Query:
While running this script ,I get welcome message from remote server and then the message given by my if condition. I do not wish to receive the welcome message from remote server. What can be done to supress that ?
example:
:/root> ./script.sh
Warning Notice
This is a protected network,and if you are not authorized.....
I think you can try:
ssh -o LogLevel=Error <rest of cmd>
or
ssh remoteserver 'remotecommand args ... 2>&1' 2>/dev/null
which will only removes the welcome message.
You can check other solutions in here
http://www.linuxquestions.org/questions/linux-security-4/how-do-you-turn-off-login-banner-for-non-interactive-ssh-470516/

Resources