I have a test server, from which i need to login to two different prod server and execute top, free -h, ps -ef commands could some one help me with a shell script
Asked
Active
Viewed 1,974 times
0
-
And where did you got stuck? – DevilaN Jun 21 '17 at 09:59
-
Possible duplicate of [Run ssh and immediately execute command](https://stackoverflow.com/questions/18522647/run-ssh-and-immediately-execute-command) – Anthony Horne Jun 21 '17 at 11:49
2 Answers
0
Only ssh
command can do this:
$ for ip in 192.168.138.22{1,2,3}; do ssh ${ip} -o StrictHostKeyChecking=no "free -h"; done
total used free shared buff/cache available
Mem: 7.8G 782M 5.2G 152M 1.8G 6.4G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.8G 1.3G 4.1G 169M 2.5G 5.8G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.8G 563M 5.9G 118M 1.4G 6.6G
Swap: 7.9G 0B 7.9G
However, this need to authenticate ssh-key
with each other. So, we can use sshpass to pass the passqord
:
$ for ip in 192.168.138.22{1,2,3}; do sshpass -p PASSWORD ssh ${ip} -o StrictHostKeyChecking=no "free -h"; done
total used free shared buff/cache available
Mem: 7.8G 782M 5.2G 152M 1.8G 6.4G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.8G 1.3G 4.1G 169M 2.5G 5.8G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.8G 563M 5.9G 118M 1.4G 6.6G
Swap: 7.9G 0B 7.9G

Weike
- 1,232
- 12
- 15
0
If you have ssh access to your server, you can execute commands remotely on this way:
ssh -i <PATH_YOUR_PRIVATE_KEY>remote_username@remote_host "<COMMAND>"

Igor Uchôa
- 329
- 1
- 9