【问题标题】:Connect via ssh with a single .bat script to multiple Adresses通过 ssh 使用单个 .bat 脚本连接到多个地址
【发布时间】:2020-05-15 08:15:35
【问题描述】:

我有一个包含 27 个 Mikrotik 路由器的环境,我想在每个路由器上添加一个具有相同凭据的用户。

通常我必须连接每个路由器并单击 GUI 来添加用户,但现在我找到了一种通过 cmd 使用 SSH 连接的方法。

我写了这个 - 它连接到单个路由器并执行添加用户过程

ssh admin@10.1.2.3 -password "Passw0rd!" "user add name=customer-support password=#F0ry0u! group=full"

但现在我想制作一个脚本,该脚本可能会读取一个 csv 文件,其中包含我要执行更改的所有路由器的 IP 地址,并在每个路由器上连接以执行命令。
这可能吗?

【问题讨论】:

    标签: ssh scripting mikrotik


    【解决方案1】:

    由于SSH is available for MikroTik路由器,您确实可以从文件中读取它们的地址。

    例如,使用 bash(您甚至可以在 Windows 上通过 WSL/WSL2 或 Git for Windows 执行它,其中包含 MinGW bash

    请参见例如“Bash Read Comma Separated CSV File on Linux / Unix”,此处适用于您的情况

    #!/bin/bash
    # Purpose: Read Comma Separated CSV File
    # Author: Vivek Gite under GPL v2.0+
    # ------------------------------------------
    INPUT=data.cvs
    OLDIFS=$IFS
    IFS=','
    [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
    while read maddress
    do
        echo "Address : $maddress"
        # do your SSH call here
    done < $INPUT
    IFS=$OLDIFS
    

    但如果您真的需要一个 bat 脚本,请参阅例如“Help in writing a batch script to parse CSV file and output a text file”。

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 2018-09-10
      • 2018-01-28
      • 2016-11-08
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      相关资源
      最近更新 更多