Shell script to connect to multiple servers
|
|
Deepika Munjampally | Date: Tuesday, 2014-06-10, 5:22 PM | Message # 1 |
Group: Guests
| Hi All,
I need to connect to Multiple servers from a shell script and want to execute a command. Below is the script which I am trying but not working. Kindly help me out if you know it. Please mail me. Thanks in advance!
-rwxrwxrwx 1 root sys 150 May 5 14:12 fname -rwxrwxrwx 1 root sys 204 May 14 14:33 return_code_22
# cat return_code_22 #### this script is used to check nagios communication #### for i in `cat fname` do ssh se283701@$i hostname ## now enter passwd enter ## executing command here ##cd /opt/nrpe ##nohup server & exit done
# cat fname ## keeping track of all ip address 10.184.10.8 10.184.10.9 10.184.10.10 10.184.10.11 10.184.10.12 10.184.10.21 10.184.10.34 10.184.10.35 10.184.10.7
Regards, Deepika
|
|
|
|
shanky | Date: Tuesday, 2014-06-10, 5:25 PM | Message # 2 |
Lieutenant
Group: Administrators
Messages: 46
Awards: 0
Reputation: 104
Status: Offline
| Hi Deepika
Hi I have created a script as per your requirement. The script goes to listed servers(in file fname), executes commands(you want) and returns.
Here is the script:
#!/usr/bin/ksh
for i in $(cat fname);
do
print "SSHing to server:$i";
ssh user1@$i "hostname; exit";
#EXITING from first server
done
Please let me know if it suits your need. Please reply me if it’s not as per your requirement.
s.k.bhagat
|
|
|
|
shanky | Date: Tuesday, 2014-06-10, 5:29 PM | Message # 3 |
Lieutenant
Group: Administrators
Messages: 46
Awards: 0
Reputation: 104
Status: Offline
| On behalf of Deepika:
Hi Shankar, I was able to connect to servers but I want to execute few commands inside each server. Plz see the below commands This is the one which you have given
set -x for i in $(cat fname); do print "SSHing toserver:$i"; ssh se283701@$i "hostname; cd /opt/nrpe; nohup server &;exit"; #ssh username@$i "hostname;exit"; #EXITING from first server done
I was trying with this below code
set -x for i in `cat fname` do ssh se283701@$i ./cmd.sh ## in this shell I hvgiven those 2 commands but did not wrk instead of new line. hostname ## now enter passwd enter ## executing command here ##cd /opt/nrpe ##nohup server &
##exit done
I want to execute the highlighted commands in all servers. Please let me know if you can.
Regards, Deepika
s.k.bhagat
|
|
|
|
shanky | Date: Tuesday, 2014-06-10, 6:05 PM | Message # 4 |
Lieutenant
Group: Administrators
Messages: 46
Awards: 0
Reputation: 104
Status: Offline
| You can try below code, tested successfully.
#!/usr/bin/ksh
for i in $(cat fname); do print "SSHing to server:$i";
ssh
user1@$i "hostname;cd /home/user1;nohup ./showcal.ksh ; exit"; #EXITING from first server
done
Thanks
s.k.bhagat
|
|
|
|
Deepu | Date: Thursday, 2014-06-12, 1:52 PM | Message # 5 |
Private
Group: Users
Messages: 1
Awards: 0
Reputation: 4
Status: Offline
| # cat output.sh set -x cd /opt/nrpe nohup server & sleep 5
# ./output.sh
+ cd /opt/nrpe + sleep 5 + nohup server Sending output to nohup.out (Nohup output is sending when running from different server)
# cat new1.sh set -x for i in $(cat fname); do print "SSHing to server:$i"; #ssh user1@$i "hostname;cd /home/user1;nohup ./showcal.ksh ; exit"; ssh se283701@$i "hostname; cd /opt/nrpe; nohup ./output.sh ;exit"; #EXITING from first server done
# ./new1.sh + cat fname + print SSHing to server:10.184.10.8 SSHing to server:10.184.10.8 + ssh [email protected] hostname; cd /opt/nrpe; nohup ./output.sh ;exit # #### ## ##### ##### ##### ## # # # # # # # # # # # #### # # # # # # ##### # # ###### ##### # # # # # # # # # # # # # # # #### # # # ##### ##### ##### This is a private system. Unauthorized use is prohibited. Password: sapdb1 + cd /opt/nrpe + sleep 5 + nohup server (## nohup output is not sending) + print SSHing to server:10.184.10.9 SSHing to server:10.184.10.9 + ssh [email protected] hostname; cd /opt/nrpe; nohup ./output.sh ;exit ##### #### ## ##### ##### ##### # # # # # # # # # # # # #### # # # # # # ##### ##### # ###### ##### # # # # # # # # # # # # # # # #### # # # ##### ##### ####### This is a private system. Unauthorized use is prohibited. Password:
Note: ohup is not working when trying to run from new1.sh script. Kindly check once and please provide the solution.
|
|
|
|
shanky | Date: Thursday, 2014-06-12, 5:45 PM | Message # 6 |
Lieutenant
Group: Administrators
Messages: 46
Awards: 0
Reputation: 104
Status: Offline
| As I know, we dont need to use "nohup command &". i.e we never user nohup and & both together. Only "nohup command" is enough and if we use this we can execute the script successfully. And for that server: The error looks like the user “se283701” doesn’t exist in local database on 10.184.10.8 since denying the user authentication.
And also ensure the specified user allowed on “/etc/pam.d” for successful login attempt.
Thanks
s.k.bhagat
|
|
|
|