【问题标题】:Transfer variables in ssh an get answer back在 ssh 中传输变量并获取答案
【发布时间】:2016-12-06 16:37:00
【问题描述】:

我想在 bash 中编写脚本以连接到服务器,在另一台服务器上 ping 它,从 ping 命令获取 ip,并将该信息发送回运行脚本的 pc,最后连接到之前被 ping 过的服务器.

我的脚本:

#!/bin/bash
echo "Script to connect to server"
#ip_p used to connect to first server, on that server i want to use ping command'
ip_p=XYz.XYZ.XYZ.XYZ
user='username to the servers' 

#ip_d - in that variable i want to save ip of the pinged server

ssh -t $user@$ip_p ip_d="ip_d=ping -c1 $1.domain | sed -nE 's/^PING[^(]+\(([^)]+)\).*/\1/p' && exit "
echo "start"
echo $ip_d
echo "stop"
ssh -t $user@$ip_d

我希望如何工作: 域我想检查 test.nyslay.pl

  1. 连接到脚本中定义的ip和用户名的服务器
  2. ping 服务器(部分“.nyslay.pl”,始终相同,但“测试”我想从脚本运行的第一个参数中读取
  3. 从上一点获取域的 ip
  4. 将 ip 从点 2 传输到运行脚本的本地计算机
  5. 连接到我们从点获得ip的服务器:2

【问题讨论】:

    标签: linux bash unix ssh ping


    【解决方案1】:

    使用命令替换:

    ip_d=$(ssh -t $user@$ip_p "ping -c1 $1.domain | sed -nE 's/^PING[^(]+\(([^)]+)\).*/\1/p'")
    

    ping 的输出(通过sed)通过ssh 到达本地机器,其输出被捕获在本地变量ip_d 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多