【问题标题】:mikrotik command over bashbash 上的 mikrotik 命令
【发布时间】:2017-09-25 11:12:54
【问题描述】:

我想通过 bash 脚本阻止一些网站。我有一个网站名称列表 list.txt。路由器是mikrotik。 我需要语法方面的帮助。 ssh 连接后无法发送命令

file = "list.txt"

行 = cat $ file

sshpass -p 'blabla' ssh y@x.x.x.x

for line in $lines;做

"/ip proxy accesses add dst-host="$line"action=denycomment="list"

完成

【问题讨论】:

    标签: bash ssh mikrotik


    【解决方案1】:

    这是一个非常古老的问题,但如果有人遇到同样的问题,我会给出我的答案;)

    #!/bin/bash
    
    where="<path to your mikrotik config.txt>";
    len=`cat $where | wc -l`;
    config=`for (( c=1; c<=$len; c++ )) do line=\`sed -n "$c""p" $where\`; echo  $line; done`;
    
    sshpass -p "blabla" ssh -t -oStrictHostKeyChecking=no y@x.x.x.x $config
    

    Mikrotik 也支持 ssh-key !使用 key 代替 sshpass 更安全。

    【讨论】:

      【解决方案2】:

      你在本地机器上声明 $file 和 $lines,你连接的路由器不能循环这个值。

      试试这样的:

      while read l;do
      sshpass -p 'blabla' ssh y@x.x.x.x ip proxy access add dst-host ="$l" action = deny comment ='list'
      done < list.txt
      

      【讨论】:

      • 感谢您的回答,您非常友善,但我需要更好的 ssh 连接会话计数解决方案。 ssh 必须在循环之前打开,然后在循环中发送命令
      【解决方案3】:

      除了使用sshpass,您还可以添加您的公钥:

       ssh 192.168.88.1 "/file print file=key; file set key contents=\"`cat ~/.ssh/id_rsa.pub`\";/user ssh-keys import public-key-file=key.txt;/ip ssh set always-allow-password-login=yes"
      

      然后你可以直接 ssh 而不必使用 sshpass。如果您主要使用 ssh,那么您可以通过将您的用户名更改为 username+ct 在您的 mikrotik 主机的 /etc/ssh/ssh_config 中禁用着色,或者只是 ssh username+ct@router

      @DamianK 的回答变成:

      where="<path to your mikrotik config.txt>";
      len=`cat $where | wc -l`;
      config=`for (( c=1; c<=$len; c++ )) do line=\`sed -n "$c""p" $where\`; echo  $line; done`;
      
      ssh x.x.x.x $config
      

      您可以做的另一件漂亮的事情是告诉 ssh 保持连接打开,例如 600 秒 - 这样您就不会遇到多次运行 ssh 的登录延迟,因此您可以使用 @esstorm 的回答。只需将其放入您的/etc/ssh/ssh_config

       ControlMaster auto
       ControlPath ~/.ssh/socket-%r@%h-%p
       ControlPersist 600
      

      唯一的障碍是,如果连接超时,如果您在该超时内重新连接,连接将挂起。当然也可以手动删除socket,可以通过find .ssh/sock*找到

      @esstorm 的回答变成:

      while read l;do
      ssh y@x.x.x.x ip proxy access add dst-host ="$l" action = deny comment ='list'
      done < list.txt
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-13
        • 2018-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        相关资源
        最近更新 更多