OK, I believe I understand your question now.
Let us assume that you have
host1 to which you connect with
putty, on this host you shall install
tmux (from the package manager, they shall be there). I suggest
screen since it is a little easier for a beginner.
Also, you need to be able to login to
host1 therefore you need to setup a
ssh key which will allow for login without a password. To perform this you need to run:
ssh-keygen -f ~/.ssh/host1tohost2
It will ask for a passphrase, to perform the script automatically (without the need for manual input of a passphrase) you shall set it to an empty passphrase. Keep host1 safe! Thanks to this key anyone in control of
host1 also gains control of
host2 (to the extent the user permissions allow).
That command will generate the following files:
You shall copy
scp ~/.ssh/host1tohost2.pub me@host2:~
~/.ssh in host2 and move
host1tohost2.pub there, then create a file called
authorized_keys in that directory:
mkdir -f ~/.ssh
mv ~/host1tohost2.pub ~/.ssh
cat host1tohost2.pub >> authorized_keys
This allows for login from
host2 without an interactive password prompt.
Let us assume that the file
servers.txt is at
host2. This assumption is needed since we will be using this
path in a script at
Back to host1
The infinite loop script you need is:
while ((1)); do
# >$LOGFILE # clear log file (see comments)
ssh -i ~/.ssh/host1tohost2 me@host2 'cat /home/me/myservers/servers.txt' |
while read x; do
echo "ping to $x"
ping -c1 $x >> $LOGFILE
Let's assume that this script is saved to a file called
pingservers.sh. But do not run it yet, since it will stop once you close
screen comes into the scene (you installed it, right?). Run:
And apparently nothing changes. You're now in a
shell that is being accessed through a
/var/run, this shell will not close once if you
detach from it and then close
putty (actually if you just close
putty the OS will figure things out and not close the shell anyway, but let's do a proper
detach for educational reasons). Before detaching it is time to run:
It will run forever, and keep
echoing a couple of messages while writing to the log file.
To detach from
screen you need to enter
Ctrl+A D (hold Ctrl, press A, release Ctrl and press D). And you're back in the original
shell you logged in with
putty, you can now close
putty and have a coffee.
Once you login back into
host1 the file
~/mypings.log shall be updated with more and more pings (no need to run
screen again). To check the file being updated in real-time you can use:
tail -f ~/mypings.log
The arch linux wiki has much more useful info about ssh-keygen and ssh keys in general, and the same is true about gnu screen. These two articles are excellent source of information for both programs.