简体   繁体   中英

Repeat a command on SSH server forever, via Plink or a python script

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
.....
  • 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM