TCL/Expect script with if/else logic based on host IP address





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















So I have been using and enjoying /usr/bin/expect for scripts because it can quickly log me into servers since it autocompletes my userid and password. Through some cursory reading, I've come to understand that it is basically a superset of TCL, which I admit knowing nothing about.



I ssh into my server all the time with my MacBook Pro, and I want to take my login script a step further and have it log into the NAS on my internal network one way if I am on my home network (with the RFC-1918 IP space of 192.168.0.0/24), and log into it externally a different way if I am not on that network. Basically an if-else condition.



A command to return the current IPv4 host address (at least on macOS) is:



ifconfig en0 | grep 'inet' | awk '$1 == "inet" { print $2 }'


(Replace en0 with the interface being used as necessary; it's the Wi-Fi on my Mac.)



This returns the IPv4 address. I was thinking this command should be called by some function (which TCL apparently calls procedures) and its output returned to an if-else condition that then executes the appropriate set of commands and exits the script.



I've got as far as doing simple if-else but I am not sure how the standard output from commands can be incorporated into the script. Any ideas?










share|improve this question























  • take a look at sexpect (Expect for Shells) which you can use to write Expect scripts with shell code only.

    – pynexj
    Mar 5 at 6:57











  • You'll find it helpful to run through the Tcl tutorial

    – glenn jackman
    Mar 5 at 18:09


















1















So I have been using and enjoying /usr/bin/expect for scripts because it can quickly log me into servers since it autocompletes my userid and password. Through some cursory reading, I've come to understand that it is basically a superset of TCL, which I admit knowing nothing about.



I ssh into my server all the time with my MacBook Pro, and I want to take my login script a step further and have it log into the NAS on my internal network one way if I am on my home network (with the RFC-1918 IP space of 192.168.0.0/24), and log into it externally a different way if I am not on that network. Basically an if-else condition.



A command to return the current IPv4 host address (at least on macOS) is:



ifconfig en0 | grep 'inet' | awk '$1 == "inet" { print $2 }'


(Replace en0 with the interface being used as necessary; it's the Wi-Fi on my Mac.)



This returns the IPv4 address. I was thinking this command should be called by some function (which TCL apparently calls procedures) and its output returned to an if-else condition that then executes the appropriate set of commands and exits the script.



I've got as far as doing simple if-else but I am not sure how the standard output from commands can be incorporated into the script. Any ideas?










share|improve this question























  • take a look at sexpect (Expect for Shells) which you can use to write Expect scripts with shell code only.

    – pynexj
    Mar 5 at 6:57











  • You'll find it helpful to run through the Tcl tutorial

    – glenn jackman
    Mar 5 at 18:09














1












1








1








So I have been using and enjoying /usr/bin/expect for scripts because it can quickly log me into servers since it autocompletes my userid and password. Through some cursory reading, I've come to understand that it is basically a superset of TCL, which I admit knowing nothing about.



I ssh into my server all the time with my MacBook Pro, and I want to take my login script a step further and have it log into the NAS on my internal network one way if I am on my home network (with the RFC-1918 IP space of 192.168.0.0/24), and log into it externally a different way if I am not on that network. Basically an if-else condition.



A command to return the current IPv4 host address (at least on macOS) is:



ifconfig en0 | grep 'inet' | awk '$1 == "inet" { print $2 }'


(Replace en0 with the interface being used as necessary; it's the Wi-Fi on my Mac.)



This returns the IPv4 address. I was thinking this command should be called by some function (which TCL apparently calls procedures) and its output returned to an if-else condition that then executes the appropriate set of commands and exits the script.



I've got as far as doing simple if-else but I am not sure how the standard output from commands can be incorporated into the script. Any ideas?










share|improve this question














So I have been using and enjoying /usr/bin/expect for scripts because it can quickly log me into servers since it autocompletes my userid and password. Through some cursory reading, I've come to understand that it is basically a superset of TCL, which I admit knowing nothing about.



I ssh into my server all the time with my MacBook Pro, and I want to take my login script a step further and have it log into the NAS on my internal network one way if I am on my home network (with the RFC-1918 IP space of 192.168.0.0/24), and log into it externally a different way if I am not on that network. Basically an if-else condition.



A command to return the current IPv4 host address (at least on macOS) is:



ifconfig en0 | grep 'inet' | awk '$1 == "inet" { print $2 }'


(Replace en0 with the interface being used as necessary; it's the Wi-Fi on my Mac.)



This returns the IPv4 address. I was thinking this command should be called by some function (which TCL apparently calls procedures) and its output returned to an if-else condition that then executes the appropriate set of commands and exits the script.



I've got as far as doing simple if-else but I am not sure how the standard output from commands can be incorporated into the script. Any ideas?







script shell-script conditional-statements expect tcl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 5 at 5:38









user1004512user1004512

61




61













  • take a look at sexpect (Expect for Shells) which you can use to write Expect scripts with shell code only.

    – pynexj
    Mar 5 at 6:57











  • You'll find it helpful to run through the Tcl tutorial

    – glenn jackman
    Mar 5 at 18:09



















  • take a look at sexpect (Expect for Shells) which you can use to write Expect scripts with shell code only.

    – pynexj
    Mar 5 at 6:57











  • You'll find it helpful to run through the Tcl tutorial

    – glenn jackman
    Mar 5 at 18:09

















take a look at sexpect (Expect for Shells) which you can use to write Expect scripts with shell code only.

– pynexj
Mar 5 at 6:57





take a look at sexpect (Expect for Shells) which you can use to write Expect scripts with shell code only.

– pynexj
Mar 5 at 6:57













You'll find it helpful to run through the Tcl tutorial

– glenn jackman
Mar 5 at 18:09





You'll find it helpful to run through the Tcl tutorial

– glenn jackman
Mar 5 at 18:09










1 Answer
1






active

oldest

votes


















1














To get the output of a command in Tcl, use exec:




  • https://stackoverflow.com/questions/12605741/how-to-get-the-results-standard-output-of-a-tcl-exec-command

  • https://www.tcl.tk/man/tcl/TclCmd/exec.htm


For example, set myvar [exec "date -u"]






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fsuperuser.com%2fquestions%2f1411321%2ftcl-expect-script-with-if-else-logic-based-on-host-ip-address%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














    To get the output of a command in Tcl, use exec:




    • https://stackoverflow.com/questions/12605741/how-to-get-the-results-standard-output-of-a-tcl-exec-command

    • https://www.tcl.tk/man/tcl/TclCmd/exec.htm


    For example, set myvar [exec "date -u"]






    share|improve this answer




























      1














      To get the output of a command in Tcl, use exec:




      • https://stackoverflow.com/questions/12605741/how-to-get-the-results-standard-output-of-a-tcl-exec-command

      • https://www.tcl.tk/man/tcl/TclCmd/exec.htm


      For example, set myvar [exec "date -u"]






      share|improve this answer


























        1












        1








        1







        To get the output of a command in Tcl, use exec:




        • https://stackoverflow.com/questions/12605741/how-to-get-the-results-standard-output-of-a-tcl-exec-command

        • https://www.tcl.tk/man/tcl/TclCmd/exec.htm


        For example, set myvar [exec "date -u"]






        share|improve this answer













        To get the output of a command in Tcl, use exec:




        • https://stackoverflow.com/questions/12605741/how-to-get-the-results-standard-output-of-a-tcl-exec-command

        • https://www.tcl.tk/man/tcl/TclCmd/exec.htm


        For example, set myvar [exec "date -u"]







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 5 at 5:56









        grawitygrawity

        243k37513570




        243k37513570






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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%2fsuperuser.com%2fquestions%2f1411321%2ftcl-expect-script-with-if-else-logic-based-on-host-ip-address%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

            Probability when a professor distributes a quiz and homework assignment to a class of n students.

            Aardman Animations

            Are they similar matrix