Traverse through list of servers using ssh non-interactively.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Traverse through list of servers using ssh non-interactively.
# 1  
Old 01-15-2019
Traverse through list of servers using ssh non-interactively.

I have 300 servers listed in servers.txt

I motto is to check if my id "user1" has sudo privileges on the 3000 servers. I m using sudo -l to check if I have privileges or not. If wish to check this either non-interactively; if not; interactively.

Below is the script I wrote:

Code:
newinput="server.txt"
while IFS= read -r var
do
echo " Variable is:"$var
sshpass -p mypassword ssh -t user1@$var 'echo "mypassword" | sudo -S -l; echo $?; exit'
 
done < "$newinput"

However; after returning non-zero for echo $? the script does not traverse through the list and checks only the first server.

Could you propose a feasible solution to the problem ?
# 2  
Old 01-15-2019
Instead of this "pull" approach by polling all these servers; have you considered "pushing" the same data via crontab scripts?
# 3  
Old 01-15-2019
There is only one way connection from the server i m running the script to the 300 servers. Hence my approach. Please help.
# 4  
Old 01-15-2019
Sorry, I cannot help you.

It is a much better solutions, in my view, to run a cron script on each server and send the data back to a repository. Or at a minimum to run a cron script and to stage the data before it is "polled" from somewhere.

If you don't have control of your architecture and network systems engineering, I can't help you.

Perhaps someone else will ?
# 5  
Old 01-15-2019
You would need to use a different file descriptor, since descriptor 0 (stdin) is needs by ssh.
Code:
while IFS= read -r var <&3
do
  [..]
done 3< "$newinput"

# 6  
Old 01-15-2019
I know different systems may have different policies with respect to sudo-er lists, but would it be possible to check to see if you are in the sudo group on each system?
Code:
for h in $LIST_OF_HOSTS
do
printf "%s: %s\n" "${h}" "$(ssh ${uname}@{h} /usr/bin/groups)" 
done | grep sudo

This only works for systems that put users in the sudo, group and use the sudo group in the sudoers file, which will be the default, I imagine, but a decent, security-minded sysadmin may set up an alternative policy. Having said that, I once installed a SUSE (possibly openSUSE?) system where the user needed the root password to use sudo.

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find active SSH servers w/ ssh keys on LAN

Hi, I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step: #!/bin/bash # LAN SSH KEYS DISCOVERY SCRIPT </etc/passwd \ grep /bin/bash | cut -d: -f6 | sudo xargs -i -- sh -c ' && cat... (11 Replies)
Discussion started by: syrius
11 Replies

2. UNIX for Beginners Questions & Answers

ssh multiple servers

Hi folks. I'm pretty new to unix, while I'm learning a lot I'm finding bash scripting quite confusing. Im sure it's not really, my head just hasn't clicked with it. Anyway, I need a script to loop the ip addresses stored in a file and run a "pgrep <process>" and return the pid or some... (2 Replies)
Discussion started by: MuntyScrunt
2 Replies

3. UNIX for Dummies Questions & Answers

Ssh not working to one server from any of the servers

Hello, I tried ssh in debug mode and below is the debug snippet.ssh to a host is not working from any of the hosts No credentials cache found debug1: Miscellaneous failure No credentials cache found debug1: Next authentication method: publickey debug1: Offering RSA public key:... (7 Replies)
Discussion started by: Vishal_dba
7 Replies

4. Shell Programming and Scripting

List of servers that are NOT authorized for password-less SSH

Hello friends, We have around 500 servers (HPUX, AIX and linux) and all of them need to be accessed from our management box (linux) via password-less ssh. Out of 500 around 150 servers are setup password-less. We need to setup password-less SSH for remaining servers. First we need to get the... (3 Replies)
Discussion started by: magnus29
3 Replies

5. Shell Programming and Scripting

Ssh to an array of servers in a for loop

There are 4 remote hosts that I have stored in an array. A ssh trust has been created from the local host to each of the remote hosts. I am trying to ssh to each of the servers in a for loop as shown below. declare -a host host}]="server1" host}]="server2" host}]="server3" ... (9 Replies)
Discussion started by: Sree10
9 Replies

6. IP Networking

Cant SSH Solaris servers

Hi all. Im working in a telco Environment and recently setup a new server. The other servers are a combination of Solaris + Linux machines. Using my new server , I can ping all other servers ( solaris + redhat linux ) but the issue lies where I try to ssh. I can only successfully ssh linux... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

7. HP-UX

how to run top or glance non-interactively using ssh

Hello Am trying to run top /glance non-interactively using ssh from redhat to hp-ux hosts but getting following errors.. ======================= ssh msi 'top -n 1' Sorry, I need to know a more specific terminal type than "unknown". ssh msi top -d1 -n20 > /tmp/top.out Sorry, I need... (3 Replies)
Discussion started by: delphys
3 Replies

8. UNIX for Dummies Questions & Answers

SSH into multiple linux servers

Hi All, Okay, I need help. I need to ssh in to multiple linux servers execute certain commands and get them to email and print on the screen when the script is being executed. So below is my script. Its not working :-(. #!/bin/bash #linux/UNIX box with ssh key based login... (7 Replies)
Discussion started by: xytiz
7 Replies

9. Shell Programming and Scripting

connecting servers using SSH - help needed

Hi all, I have a script written successfully and tuned by our members, now I am trying to execute this script from a taxi server. My aim is I want to connect to four servers via ssh where I want to ran a script or run some commands and write the output to a file in the Taxi server. I... (12 Replies)
Discussion started by: senthilkumar_ak
12 Replies

10. UNIX for Dummies Questions & Answers

Using File Descriptors, traverse a list

I have written this code, and according to my research it SHOULD be going down the list until it is finished, but I am getting blank feedback. Nothing is being output as far as I can tell. #!/bin/sh while echo Enter to start traversing read enter do read list <&3 echo $list done any... (2 Replies)
Discussion started by: MaestroRage
2 Replies
Login or Register to Ask a Question