通过SSH远程运行脚本 [英] Run scripts remotely via SSH
问题描述
我需要从100台远程服务器上收集用户信息.我们拥有用于身份验证的公共/私有密钥基础结构,并且我已经配置了ssh-agent
命令来转发密钥,这意味着我可以在没有密码提示的情况下在任何服务器上登录(自动登录).
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"
我不知道如何在没有交互的情况下使用ssh运行此脚本?
I don't know how to run this script using ssh without interaction??
推荐答案
由于您需要登录到远程计算机,因此AFAICT无法无ssh"执行此操作.但是,ssh
接受登录后在远程计算机上执行的命令(而不是将启动的Shell).因此,如果您可以将脚本保存在远程计算机上,例如作为~/script.sh
,您可以执行它而无需使用以下命令启动交互式shell
Since you need to log into the remote machine there is AFAICT no way to do this "without ssh". However, ssh
accepts a command to execute on the remote machine once logged in (instead of the shell it would start). So if you can save your script on the remote machine, e.g. as ~/script.sh
, you can execute it without starting an interactive shell with
$ ssh remote_machine ~/script.sh
脚本终止后,连接将自动关闭(如果您没有故意进行配置).
Once the script terminates the connection will automatically be closed (if you didn't configure that away purposely).
这篇关于通过SSH远程运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!