Create Bash Script to Wait and then Run












1














What I am basically trying to achieve is to automate something I do every time I start work.



In this order, I




  1. VPN connection

  2. Open new gnome-terminal tab

  3. SSH into a remote machine


I have this so far:



WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
xdotool windowfocus $WID
xdotool type --delay 1 --clearmodifiers "sudo openvpn --config 'conn1.ovpn'"
xdotool key Return

xdotool key ctrl+shift+t
wmctrl -i -a $WID
sleep 1

while ! $(/usr/bin/fping -q 192.168.0.5); do sleep 1;done

xdotool type --delay 3 --clearmodifiers "ssh machine1"
sleep 3
xdotool key Return


The problem I am having is that the SSH commands require the VPN connection to be open first. I thought that by putting the last sleep command in, that the code would wait for 3 seconds before hitting enter.



What is actually happening is that the openvpn command is not running until after the last sleep, so the SSH command fails.



Can someone please point me in the direction I need to be heading for this.










share|improve this question





























    1














    What I am basically trying to achieve is to automate something I do every time I start work.



    In this order, I




    1. VPN connection

    2. Open new gnome-terminal tab

    3. SSH into a remote machine


    I have this so far:



    WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
    xdotool windowfocus $WID
    xdotool type --delay 1 --clearmodifiers "sudo openvpn --config 'conn1.ovpn'"
    xdotool key Return

    xdotool key ctrl+shift+t
    wmctrl -i -a $WID
    sleep 1

    while ! $(/usr/bin/fping -q 192.168.0.5); do sleep 1;done

    xdotool type --delay 3 --clearmodifiers "ssh machine1"
    sleep 3
    xdotool key Return


    The problem I am having is that the SSH commands require the VPN connection to be open first. I thought that by putting the last sleep command in, that the code would wait for 3 seconds before hitting enter.



    What is actually happening is that the openvpn command is not running until after the last sleep, so the SSH command fails.



    Can someone please point me in the direction I need to be heading for this.










    share|improve this question



























      1












      1








      1







      What I am basically trying to achieve is to automate something I do every time I start work.



      In this order, I




      1. VPN connection

      2. Open new gnome-terminal tab

      3. SSH into a remote machine


      I have this so far:



      WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
      xdotool windowfocus $WID
      xdotool type --delay 1 --clearmodifiers "sudo openvpn --config 'conn1.ovpn'"
      xdotool key Return

      xdotool key ctrl+shift+t
      wmctrl -i -a $WID
      sleep 1

      while ! $(/usr/bin/fping -q 192.168.0.5); do sleep 1;done

      xdotool type --delay 3 --clearmodifiers "ssh machine1"
      sleep 3
      xdotool key Return


      The problem I am having is that the SSH commands require the VPN connection to be open first. I thought that by putting the last sleep command in, that the code would wait for 3 seconds before hitting enter.



      What is actually happening is that the openvpn command is not running until after the last sleep, so the SSH command fails.



      Can someone please point me in the direction I need to be heading for this.










      share|improve this question















      What I am basically trying to achieve is to automate something I do every time I start work.



      In this order, I




      1. VPN connection

      2. Open new gnome-terminal tab

      3. SSH into a remote machine


      I have this so far:



      WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
      xdotool windowfocus $WID
      xdotool type --delay 1 --clearmodifiers "sudo openvpn --config 'conn1.ovpn'"
      xdotool key Return

      xdotool key ctrl+shift+t
      wmctrl -i -a $WID
      sleep 1

      while ! $(/usr/bin/fping -q 192.168.0.5); do sleep 1;done

      xdotool type --delay 3 --clearmodifiers "ssh machine1"
      sleep 3
      xdotool key Return


      The problem I am having is that the SSH commands require the VPN connection to be open first. I thought that by putting the last sleep command in, that the code would wait for 3 seconds before hitting enter.



      What is actually happening is that the openvpn command is not running until after the last sleep, so the SSH command fails.



      Can someone please point me in the direction I need to be heading for this.







      bash shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Rui F Ribeiro

      39.3k1479131




      39.3k1479131










      asked Nov 7 '17 at 23:40









      Wildcard27Wildcard27

      13516




      13516






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Put a waiting loop before starting ssh:



          while ! $(/usr/bin/fping -q <openvpn remote endpoint ip>); do sleep 1;done





          share|improve this answer





















          • When I do this, the VPN connection won't run. The new tab opens but both tabs appear to be waiting for the IP to be available
            – Wildcard27
            Nov 8 '17 at 0:09










          • @Wildcard27 do you mean that with the ping loop the VPN doesn't go up anymore?
            – Zip
            Nov 8 '17 at 0:44











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403202%2fcreate-bash-script-to-wait-and-then-run%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Put a waiting loop before starting ssh:



          while ! $(/usr/bin/fping -q <openvpn remote endpoint ip>); do sleep 1;done





          share|improve this answer





















          • When I do this, the VPN connection won't run. The new tab opens but both tabs appear to be waiting for the IP to be available
            – Wildcard27
            Nov 8 '17 at 0:09










          • @Wildcard27 do you mean that with the ping loop the VPN doesn't go up anymore?
            – Zip
            Nov 8 '17 at 0:44
















          1














          Put a waiting loop before starting ssh:



          while ! $(/usr/bin/fping -q <openvpn remote endpoint ip>); do sleep 1;done





          share|improve this answer





















          • When I do this, the VPN connection won't run. The new tab opens but both tabs appear to be waiting for the IP to be available
            – Wildcard27
            Nov 8 '17 at 0:09










          • @Wildcard27 do you mean that with the ping loop the VPN doesn't go up anymore?
            – Zip
            Nov 8 '17 at 0:44














          1












          1








          1






          Put a waiting loop before starting ssh:



          while ! $(/usr/bin/fping -q <openvpn remote endpoint ip>); do sleep 1;done





          share|improve this answer












          Put a waiting loop before starting ssh:



          while ! $(/usr/bin/fping -q <openvpn remote endpoint ip>); do sleep 1;done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 '17 at 23:54









          Ipor SircerIpor Sircer

          10.5k11024




          10.5k11024












          • When I do this, the VPN connection won't run. The new tab opens but both tabs appear to be waiting for the IP to be available
            – Wildcard27
            Nov 8 '17 at 0:09










          • @Wildcard27 do you mean that with the ping loop the VPN doesn't go up anymore?
            – Zip
            Nov 8 '17 at 0:44


















          • When I do this, the VPN connection won't run. The new tab opens but both tabs appear to be waiting for the IP to be available
            – Wildcard27
            Nov 8 '17 at 0:09










          • @Wildcard27 do you mean that with the ping loop the VPN doesn't go up anymore?
            – Zip
            Nov 8 '17 at 0:44
















          When I do this, the VPN connection won't run. The new tab opens but both tabs appear to be waiting for the IP to be available
          – Wildcard27
          Nov 8 '17 at 0:09




          When I do this, the VPN connection won't run. The new tab opens but both tabs appear to be waiting for the IP to be available
          – Wildcard27
          Nov 8 '17 at 0:09












          @Wildcard27 do you mean that with the ping loop the VPN doesn't go up anymore?
          – Zip
          Nov 8 '17 at 0:44




          @Wildcard27 do you mean that with the ping loop the VPN doesn't go up anymore?
          – Zip
          Nov 8 '17 at 0:44


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403202%2fcreate-bash-script-to-wait-and-then-run%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?