简体   繁体   中英

How can I run some commands automatically after waiting for my SSH connection to be established with a while loop

Long time reader. First time poster. I searched high and low for a solution but I am stuck.

I am rebooting a linux server then running the following loop on my local machine (Mac):

while true; do ssh sudo-user@10.10.10.10; sleep 1; done

This will loop until it is able to make a successful SSH connection:

ssh: connect to host 10.10.10.10 port 22: Operation timed out <-- still rebooting
ssh: connect to host 10.10.10.10 port 22: Operation timed out <-- still rebooting
ssh: connect to host 10.10.10.10 port 22: Connection refused <-- booted but SSHD not started yet.

Now if I type

sudo su - 

then

tail -f /var/log/somelog 

and press enter before the connection is made those two commands are "queued up" and run on the remote server after the connection is made and everything is great. My question is how can I add those two commands to my loop so I don't have to type them in manually after running the loop? I have tried several approaches but nothing works.

To sum it up. I want to run a loop that will run till SSH becomes available, I am connected to the shell, then automatically run sudo su - and tail -fa log file.

Here's an example start to finish:

Broadcast message from root@10.10.10.10
(/dev/pts/4) at 0:28 ...

The system is going down for reboot NOW!
Connection to 10.10.10.10 closed by remote host.
Connection to 10.10.10.10 closed.

[~]: while true; do ssh sudo-user@10.10.10.10; sleep 1; done

ssh: connect to host 10.10.10.10 port 22: Connection refused
ssh: connect to host 10.10.10.10 port 22: Operation timed out
ssh: connect to host 10.10.10.10 port 22: Operation timed out
sudo su - (<--- type this command press enter)
ssh: connect to host 10.10.10.10 port 22: Operation timed out
ssh: connect to host 10.10.10.10 port 22: Connection refused
tail -f /var/log/somelog (<--- type this command press enter)
ssh: connect to host 10.10.10.10 port 22: Connection refused
ssh: connect to host 10.10.10.10 port 22: Connection refused
Last login: Sun Nov 16 00:47:55 2014 from 10.10.10.10
"Welcome to blah MOTD"
sudo su -
tail -f /var/log/somelog
[sudo-user@10.10.10.10 ~]$ sudo su -
Last login: Sun Nov 16 00:30:18 UTC 2014 on pts/5
[root@10.10.10.10 ~]# tail -f /var/log/somelog
[somelogfileoutput] blah blah blah
[somelogfileoutput] blah blah blah
[somelogfileoutput] blah blah blah
[somelogfileoutput] blah blah blah

Hope this makes sense. Thanks!

Since your loop is only for waiting - why don't you just increase timeout for establishing connection?
Look at ConnectTimeout in man ssh_config . Then you will be able to put everything in one command.

BTW: Looks like you use sudo su without password. If this is true it is dangerous. You may consider configuring sudo to allow execution of this particular command ( tail -f /var/log/somelog ) without password, while requestng password for everything else.

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