gpt4 book ai didi

linux - 在 Linux 中使用 ssh 在本地服务器上执行远程脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:23 25 4
gpt4 key购买 nike

我在远程服务器的主文件夹中有一个名为“test”的脚本,其中包含以下几行:

#!/bin/bash

tengbe=`ifconfig | grep -B1 192.168 | awk 'NR==1 { print $1 }' | sed 's/://g'`

基本上,一旦脚本被调用,它只是将本地服务器的接口(interface)名称存储到一个变量中。当我 ssh 进入远程服务器调用脚本时,出现错误:

ssh remoteserver-IP './test'
./test: line 3: ifconfig: command not found

我的脚本可能有什么问题?我看到了各种无法解决我的问题的答案。

最佳答案

尝试:

$ ssh remotehost ifconfig
bash: ifconfig: command not found

ifconfig 不在远程主机上的 PATH 中。你可以证明它:

$ which ifconfig
/sbin/ifconfig
$ ssh remotehost 'echo $PATH'
(returns lots of dirs, none of which is /sbin)

要解决这个问题,请指定 ifconfig 的完整路径:

$ ssh remotehost /sbin/ifconfig

... 或在调用它之前配置 $PATH:

$ ssh remotehost 'PATH=$PATH:/sbin ifconfig'

... 或编辑 $HOME/.bashrc(或替代方案——阅读您的 shell 的初始化过程)以将 /sbin 添加到 $PATH一直。

为了安全起见,在脚本中通常最好指定绝对路径,也许通过变量。所以在你的脚本中:

#!/bin/bash
IFCONFIG=/sbin/ifconfig
tengbe=$(${IFCONFIG} | grep -B1 192.168 | awk 'NR==1 { print $1 }' | sed 's/://g')

请注意,我已将您的反引号替换为 $() - 这不是必需的,但这是一个养成的好习惯 - What is the benefit of using $() instead of backticks in shell scripts?

关于linux - 在 Linux 中使用 ssh 在本地服务器上执行远程脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42363462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com