0
我有一個測試服務器,從中我需要登錄到兩個不同的prod服務器並執行頂部,免費-h,ps -ef命令 有人可以幫助我有一個shell腳本如何使用shell腳本在遠程機器上執行linux命令
我有一個測試服務器,從中我需要登錄到兩個不同的prod服務器並執行頂部,免費-h,ps -ef命令 有人可以幫助我有一個shell腳本如何使用shell腳本在遠程機器上執行linux命令
只有ssh
命令可以做到這一點:
$ 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
然而,這需要authenticate ssh-key
對方。所以,我們可以用sshpass傳遞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
如果你有SSH訪問您的服務器,你可以在這種方式遠程執行命令:
ssh -i <PATH_YOUR_PRIVATE_KEY>[email protected]_host "<COMMAND>"
而你在哪裏卡住了? – DevilaN
[運行ssh並立即執行命令]可能重複(https://stackoverflow.com/questions/18522647/run-ssh-and-immediately-execute-command) –