-1

I need to create one script for fetching the details from multiple servers with one jump server so while running the script it should ask one time user name and password after that all server it should go with out password how can I create a script?

if [ $(id -u) -eq 0 ]; then
    read -p "Enter username : " username
    read -s -p "Enter password : " password
    egrep "^$username" /etc/passwd >/dev/null
    if [ $? -eq 0 ]; then
        echo for HOST in $(cat servers.txt ) ; do ssh $HOST "cat /var/log/QPKS/qpk" ; done
4
  • 1
    There are tools like Ansible out there for this. – Raman Sailopal Jul 26 '18 at 14:21
  • 1
    There are two fi missing, and /etc/passwd is misspelled. You also don't allow for backslashes in passwords (use read -r). Please see and use shellcheck.net – Kusalananda Jul 26 '18 at 14:27
  • ok but seriously this is the first time i am trying so exact thing i didnt got – Jibin Benny Jul 26 '18 at 14:55
  • 1
    You should be using private key authentication with SSH, that gets rid of the need for getting the username and password interactively. – Kusalananda Jul 26 '18 at 15:07
0

Ansible

I'd do this with Ansible. You can create a inventory file which includes your servers like this:

$ cat inventory
[serverlist]
host1
host2
host3
host4

Then run an Ansible ad-hoc command like this:

$ ansible -i inventory -m shell -a "cat /var/log/QPKS/qpk" all

If you have to provide access to this file via sudo add the -b switch to ansible. If you need to provide a password for sudo, use -K. To connect to the remote server as an alternative user use -u.

   -b, --become
       run operations with become (does not imply password prompting)

   -K, --ask-become-pass
       ask for privilege escalation password

   -u REMOTE_USER, --user REMOTE_USER
       connect as this user (default=None)

Other methods

In the past I've used other tools to do this same thing. the first link shows doing a similar thing using pssh. The 2nd link shows how you can use pssh to propagate your SSH key across a bunch of servers the 1st time, when all you have is username/password access to a set of servers, and don't want to key your password in dozens of times to push your SSH key out initially.

Once your SSH key has been dispersed, the above looping you're having to do goes away since you'll have SSH key access set up, eliminating the need to key in your password.

-2

this is they way I did it like simple shell method.

#!/bin/bash

cd ~
read -p "Username:" uname
echo -n Password:
read -s password
read -p "`echo -e '\nFile Name:'`" fname

if [ ! -e $fname ]; then
        echo "No file found"
elif [ ! -s $fname ]; then
        echo "File has no data"
fi

#lcount=0
#hcount=0
count=0
#check for the host name in the file provided. Get them one by one on the following loop.
for host in `cat $fname`;
        do
#intimate te user on progress and logon to the server to get OS type
                echo "$host in progress"
                ost=$(sshpass -p  $password ssh -q $uname@$host uname) &> /dev/null #check the OS type of the server
        if [[ $ost =~ .*UX.* ]]
        then
                if [[ -e result_$1.$(date '+%Y%m%d') && count -le 0 ]]; then #to remove if the file exists withe same name. always use different name
                rm result_$1.$(date '+%Y%m%d')
                fi

                echo "### $host ###" >> result_$1.$(date '+%Y%m%d')
                sshpass -p  $password ssh -q $uname@$host swlist | grep -e Pat -e QPK >> result_$1.$(date '+%Y%m%d') #get the current patch and apend the file
                echo "$host completed"
#               (( hcount++ ))
        elif [[ $ost =~ .*Linux.* ]]
        then

                if [[ -e result_$1.$(date '+%Y%m%d') && count -le 0 ]]; then ##to remove if the file exists withe same name. always use different name
                rm result_$1.$(date '+%Y%m%d')
                fi


                echo "### $host ###" >> result_$1.$(date '+%Y%m%d')
                sshpass -p $password ssh -q $uname@$host "cat /var/log/QPKS/qpk" >> result_$1.$(date '+%Y%m%d') #get the current patch and apend the file
                echo "$host completed"
#               (( lcount++ ))
        else
                echo "Please check the server $host. Unable to check the patch" #if the server could not be logged in to get the patch
        fi

        (( count++ ))
        done
2
  • This is not a good way to solve this. You're using the technologies in ways that's inconsistent with their true intent. Obviously just my $0.02 but using proper tools such as the ones I've mentioned in my A'er is the cleaner alternative to this type of problem. – slm Aug 1 '18 at 9:08
  • -1 for unquoted variable expansions and not allowing the password to contain backslashes, and in general very messy code with lots of commands commented out and repeated code segments. – Kusalananda Aug 1 '18 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.