自学内容网 自学内容网

使用SSH在远程服务器上运行本地Shell脚本

Running a Local Shell Script on a Remote Server Using SSH

使用 SSH 在远程服务器上运行本地机器上的 shell 脚本是非常有用的,对于需要远程管理服务器的系统管理员和开发人员来说尤其如此。

假设您是一名系统管理员,您需要更新许多服务器上的软件,这些服务器与您不在同一位置。您可以使用 shell 脚本一次为所有服务器完成这项工作,而不是逐个登录到每个服务器,这为您节省了大量的时间和精力。

必备条件

  • SSH access to the remote server.
  • A local shell script that you want to execute remotely.
  • SSH client installed on your local machine.

Step 1: Ensure SSH Access

在继续之前,确保您具有对远程服务器的 SSH 访问权限,您可以登录测试一下。

ssh username@remote-server-ip

Step 2: Create Your Local Script

在本地机器上创建一个 shell 脚本。例如,我们将其命名为 check mem.sh

#!/bin/bash

echo "Current date and time: $(date)"
echo "Free memory on the server:"
free -h

#echo "Memory usage summary:"
#vmstat -s

确保脚本是可执行的

chmod +x check_mem.sh

Step 3: Execute Script Directly Over SSH

您可以使用 SSH 直接执行脚本,而不是将脚本复制到远程服务器。

ssh username@remote-server-ip 'bash -s' < check_mem.sh

Running Shell Script on a Remote Using SSH

我的开源项目

酷瓜云课堂-开源知识付费解决方案


原文地址:https://blog.csdn.net/xiaochong0302/article/details/142991468

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!