TCL/Expect script with if/else logic based on host IP addressShell Script if elseExpect script inside bash...

What happened to QGIS 2.x LTR?

How to substitute values from a list into a function?

Wrap all numerics in JSON with quotes

Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?

Levi-Civita symbol: 3D matrix

For a 1-action spell, do I need to take a turn to ready the spell before I can cast it, or can I cast it immediately?

Make me a metasequence

Roots of 6th chords on the guitar for different inversions/voicings

Book about a time-travel war fought by computers

Source for Cremation Specifically Not Jewish

Is there a math equivalent to the conditional ternary operator?

How to lift/raise/repair a segment of concrete slab?

Are paired adjectives bad style?

Inverse of the covariance matrix of a multivariate normal distribution

Where is the line between being obedient and getting bullied by a boss?

How can I handle a player who pre-plans arguments about my rulings on RAW?

In Adventurer's League, is it possible to keep the Ring of Winter if you manage to acquire it in the Tomb of Annihilation adventure?

Can a space-faring robot still function over a billion years?

Borrowing Characters

Every subset equal to original set?

Don't know what I’m looking for regarding removable HDDs?

Skis versus snow shoes - when to choose which for travelling the backcountry?

lead or lag function to get several values, not just the nth

I can't die. Who am I?



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


Shell Script if elseExpect script inside bash script optional statementBash/expect script to log in via ssh and port knocking. SSH Keys not possibleUsing EXPECT script with a $ in the passwordNot able to copy a file via script from local host to remote hostTimeout does not work in expect scriptfile exists not working in expect scriptHow to handle a condition when an expect script executing another script on a remote host is interrupted?Expect script for telnet communication with DLink routerAHK if/else of output from Custom Message Box Function













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







New contributor




user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















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

    – pynexj
    22 hours ago











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

    – glenn jackman
    11 hours ago
















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







New contributor




user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















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

    – pynexj
    22 hours ago











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

    – glenn jackman
    11 hours ago














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







New contributor




user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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







New contributor




user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









user1004512user1004512

61




61




New contributor




user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user1004512 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













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

    – pynexj
    22 hours ago











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

    – glenn jackman
    11 hours ago



















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

    – pynexj
    22 hours ago











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

    – glenn jackman
    11 hours ago

















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

– pynexj
22 hours ago





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

– pynexj
22 hours ago













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

– glenn jackman
11 hours ago





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

– glenn jackman
11 hours ago










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
    });


    }
    });






    user1004512 is a new contributor. Be nice, and check out our Code of Conduct.










    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 23 hours ago









        grawitygrawity

        240k37508561




        240k37508561






















            user1004512 is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            user1004512 is a new contributor. Be nice, and check out our Code of Conduct.













            user1004512 is a new contributor. Be nice, and check out our Code of Conduct.












            user1004512 is a new contributor. Be nice, and check out our Code of Conduct.
















            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

            VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...

            Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

            Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...