Run scripts remotely via SSH

Satish picture Satish · Oct 17, 2012 · Viewed 12.2k times · Source

I need to collect user information from 100 remote servers. We have public/private key infrastructure for authentication, and I have configured ssh-agent command to forward key, meaning i can login on any server without password prompt (auto login).

Now I want to run a script on all server to collect user information (how many user account we have on all servers).

This is my script to collect user info.

#!/bin/bash
_l="/etc/login.defs"
_p="/etc/passwd"

## get mini UID limit ##
l=$(grep "^UID_MIN" $_l)

## get max UID limit ##
l1=$(grep "^UID_MAX" $_l)

awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max  && $7 != "/sbin/nologin" ) print $0 }' "$_p"

I don't know how to run this script using ssh without interaction??

Answer

andrux picture andrux · Oct 17, 2012

Sounds like something you can do using expect.

http://linux.die.net/man/1/expect

Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be.