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?

  • take a look at [sexpect (Expect for Shells)](https://github.com/clarkwang/sexpect) which you can use to write *Expect* scripts with **shell code only**. – su.root Mar 05 '19 at 06:57
  • You'll find it helpful to run through the [Tcl tutorial](https://tcl.tk/man/tcl8.5/tutorial/tcltutorial.html) – glenn jackman Mar 05 '19 at 18:09

1 Answers1

1

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

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

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966