1

Perhaps a python script would work for this simple automation. I need to connect via ssh to my router and run a command over and over. I understand that if I use Plink with a script I will probably have to repeat my command thousands of times. That would work but isn't there a better to just simply repeat the command?

For example, if i have to make a script my file would look like this:

/user/print
/user/print
/user/print
/user/print
/user/print
/user/print
/user/print
.....
Mariyah
  • 13
  • 5
  • You want something like `while true; do date; sleep 1; done` (replacing `date` with a command of your choice). – match Feb 20 '18 at 21:32
  • Should be tagged [tag:windows] – Gilles Quénot Feb 20 '18 at 21:35
  • I tried making a cmd.txt with the following: while true; do /user/print; sleep 1; done. Then I start putty like this: C:\Program Files\PuTTY>putty.exe -t -m cmd.txt -load "mysesseion" -l root -pw mypsss. It starts the session, then gives me a syntax error. – Mariyah Feb 21 '18 at 04:34
  • And does your "router" have bash shell at all? – Martin Prikryl Feb 21 '18 at 10:07
  • Yes, it is a Mikrotik router. it has an SSH console. If I make a cmd.txt with a hundred lines of:` /user print` it works, but stops after a hundred, I just need to make it infinite and a 5 second pause in between – Mariyah Feb 21 '18 at 13:45
  • Try running `uname -a` in your router – Mark Setchell Feb 21 '18 at 14:07

2 Answers2

0
  • To do the loop on the server-side do:

    plink username@example.com "while true; /user/print; done"
    

    This has an advantage that you connect only once to the server.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

You can use the vassal package, which is exactly designed for this.

All you need is to install vassal and do

from vassal.terminal import Terminal
from vassal.scheduler import Scheduler
shell = Terminal(["ssh username@host", "cd scripts", "python foo1.py", "python foo2.py"])
shell = Scheduler(shell, sec=1)
shell.run()

This will run the command once every second, and you can make it run faster to change sec=0.1.

Shawn
  • 571
  • 7
  • 8