【问题标题】:Copy/paste contents from a file into multiple tmux ssh sessions将文件中的内容复制/粘贴到多个 tmux ssh 会话中
【发布时间】:2021-02-23 18:41:56
【问题描述】:

我有一个包含 IP 地址列表的文件,例如称为 IP_LIST。我的目标是遍历每个 IP 并将其复制到多个 tmux 窗格(每个窗格 1 个 IP)。下面的脚本假定您已经在一个 tmux 会话中。我很难将 IP 复制到剪贴板,然后将其粘贴到每个窗格。我确定我需要以某种方式合并 tmux-buffer。我希望有人能指出我正确的方向。

目前,该脚本执行以下操作:

  1. 读取 IP_LIST 文件
  2. 根据 IP 地址的数量拆分窗格。
  3. ssh 进入指定主机。 (我在 /etc/ssh 中将主机配置为“mc”作为我的默认值 /config)
while IFS= read -r ip || [[ -n "$ip" ]]; do

    echo "$ip" | xclip -selection clipboard  
    tmux split-window -v; tmux send-keys "ssh mc" ; tmux send-keys "KPEnter"
    tmux bind C-b run "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer"
done < "$1"

IP_list

1.2.3.4
4.3.2.1
6.7.8.9

可以通过以下方式运行:

./tmux_ip_script IP_LIST

编辑:使用 tmux 缓冲区更新了脚本。但是,不会复制到每个窗格。

【问题讨论】:

    标签: bash ssh tmux


    【解决方案1】:

    我有类似的东西。我将它与另一个以 IP 地址作为第一列的函数一起使用。我称我为“so.sh”(用于 ssh 打开)。它使用发送密钥。在这种情况下,您将使用cat &lt;myfile&gt; | so.sh,它将打开一个新的 tmux 窗口,其中包含一个新窗格,每个窗格都有一个 ssh 会话。也许可以根据您的需要进行修改。

    #!/bin/bash
    # so.sh => ssh open
    # takes piped input, creats a new tmux window, and then  
    # uses first column to open a new tmux pane with an ssh session to each pane
    
    if ! [ -n "$TMUX" ]; then
      echo "This must be run from a tmux session"
      exit 0
    fi
    
    base="so" 
    
    
    ipList=""
    while read -r data; do
      ipList+=$(echo $data | awk '{print $1}')" "
    done 
    
    tmux new-window -n $base
    
    p=1
    
    for ip in $ipList; do
      if [[ $p > 1 ]]; then 
          tmux splitw -v -t "$base" # vertical split
        tmux select-layout -t "$base" even-vertical
      fi
        tmux send-keys -t "$base.$p" "ssh $ip" enter
      ((p++))
    done
    
    
    

    【讨论】:

    • tmux send-keys -t &lt;target_window&gt;.&lt;pane_number&gt; 可能是你的关键部分。
    【解决方案2】:

    您可能想查看xpanes

    $ cat IP_list
    1.2.3.4
    4.3.2.1
    6.7.8.9
    $ xpanes -c 'ssh {}' $(cat IP_list)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      相关资源
      最近更新 更多