1 2 Previous Next 25 Replies Latest reply on Jun 12, 2014 9:10 PM by steve_ssh

    Read a value via a telnet connection

    KylePutzier

      I would like to open a telnet connection to 192.168.1.200:2000 and send the command *SRA and be able to put the responce into a FileMaker field.

      Can someone teach me how this can be done.

       

      OS X, FMPA13

       

      Kyle

       

      BTW,

      I can telnet into my device, send the command, and see the expected responce via OS X Terminal

        • 1. Re: Read a value via a telnet connection
          Malcolm

          I would like to open a telnet connection to 192.168.1.200:2000 and send the command *SRA and be able to put the responce into a FileMaker field.

           

          look at osacript. You should be able to get the output from your telnet session into filemaker via applescript using osascript on the commandline.

           

          Malcolm

          • 2. Re: Read a value via a telnet connection
            KylePutzier

            Here is where I am at on this project.

             

            It still needs work. It opens a Terminal window when I wish it wouldn't. I have to use delays to let the telnet commands catch up, which is risky, but I can live with that.

            Is there a way to make this script work without making a Terminal window visible to the user?

             

             

            tell application "Terminal"

                      activate

                      do script "telnet 192.168.1.200 2000"

                      delay 1

                      tell application "System Events" to keystroke "*SRA"

                      tell application "System Events" to keystroke (key code 36)

                      delay 1

                      set WindowContents to contents of front window

                      set MyCount to last word of WindowContents

                      tell application "System Events" to keystroke "]" using control down

                      tell application "System Events" to keystroke "quit"

                      tell application "System Events" to keystroke (key code 36)

                      close front window saving no

            end tell

             

            tell application "FileMaker Pro Advanced"

                      set data cell "Count" of current record to MyCount

            end tell

            • 3. Re: Read a value via a telnet connection
              BruceRobertson

              Learn to use the applescript command "do shell script"

               

              See attached example.

              • 5. Re: Read a value via a telnet connection
                BruceRobertson

                So did you solve your problem?

                • 6. Re: Read a value via a telnet connection
                  KylePutzier

                  Bruce

                  Not to my liking. The script I posted earlier works and gives me the result, but it brings up the terminal window to do it.

                   

                  If I try to connect with a do shell script (do shell script "telnet 192.168.1.200 2000"), I get a "Connection closed by foreign host." error. That sound like an authorization issue to me. The device doesn't seem to require a username, but it does have a password, so I tried that. If I add the password (which is not required while using the terminal) I get "00000000@192.168.1.200: nodename nor servname provided, or not known".


                  The device has a built in web server, but it can only communicate the data via a java script, which is outside of my capabilities.


                  I tried using 24U's SimpleTalk plugin and FMP hangs when I try the STalk_Send function. A support request from 24U is so far unanswered.

                  • 7. Re: Read a value via a telnet connection
                    BruceRobertson

                    As noted by all the examples - this is a solvable problem.

                     

                    That is the advantage of do shell script; it does not require you to open any applications or target any other windows.

                    You don't need any plugins.

                    • 8. Re: Read a value via a telnet connection
                      KylePutzier

                      Bruce,

                       

                      Entering this in terminal opens a connection:

                      "telnet 192.168.1.200 2000"

                       

                      This Applescript opens a connection:

                      tell application "Terminal"

                                do script "telnet 192.168.1.200 2000"

                      end tell

                       

                      This Applescipt results in error "Finder got an error: Connection closed by foreign host." number 1

                      do shell script "telnet 192.168.1.200 2000"

                       

                      I do not know what the problem is. Do you?

                      Permissions?

                       


                      • 9. Re: Read a value via a telnet connection
                        KylePutzier

                        This as a text file also works:

                        /Users/kyleputzier/TestScript.txt =  "telnet 192.168.1.200 2000"

                         

                        This does not

                        do shell script "/Users/kyleputzier/TestScript.txt"

                        • 10. Re: Read a value via a telnet connection
                          KylePutzier

                          This applescript results in "Running." and cannot be quit.

                           

                          do shell script "telnet 192.167.1.200 2000" user name "Kyle Putzier" password "XXXXXX" with administrator privileges

                          • 11. Re: Read a value via a telnet connection
                            steve_ssh

                            Hello Kyle,

                             

                            I suspect that a solution lies with the fact that telnet is an interactive application, and that, treated as such, it will yield the results that you seek.

                             

                            Your current methodology of sending keystroke commands with strategic delays is a good example of working "interactively" with the program, though understandably one which you wish to improve upon.

                             

                             

                            Use of expect:

                             

                            There exists a command line application named expect, which provides for the ability to script commands to be sent interactively to command line programs such as telnet.

                             

                            Essentially, expect allows you to script the process of invoking an application, and conditionally feeding that application data based on possible responses that are "expected".

                             

                            This is one avenue that you could pursue which I believe should work, and which would get you away from having to open a Terminal window and script sending keystrokes.

                             

                             

                             

                            The basic gist would be:

                             

                            - write a script that is executed by expect which is designed to invoke telnet and send your desired command

                             

                            - invoke the expect script via the AppleScript do shell script command

                             

                            Below I will include sample scripts to get you started as inspiration and hopefully proof of concept.  I will also include a few reference links related to this topic.

                             

                            Please note that the sample scripts I worked up are not suitable as-is for production use, as I have not included any error trapping/handling, and moreover I am, by no stretch of the imagination, seasoned in the best practices concerning expect.

                             

                            Also, note that writing the expect script to an external script file might be preferable, as this might allow for composing a more robust script.

                             

                             

                             

                             

                            Here is an example of an AppleScript that I threw together to convince myself that this could work:

                             

                            (*

                             

                            Sends some data via telnet

                             

                            Not suitable as-is for production use; at the very least the following important features are lacking in this example:

                             

                              - Timeout trapping on telnet connection/response

                              - Trapping for any other errors with telnet connection/response

                              - Applescript Try/Catch routine for invoking the "do shell script" command

                             

                            Additional Note:

                             

                              Value for varTelnetPrompt may have to be tweaked depending on OS version

                             

                            *)

                             

                             

                            set varHost to "127.0.0.1"

                             

                            set varPort to 80

                             

                            set varSendPayload to "*SRA"

                             

                             

                            set varTelnetPrompt to "Escape character is*"

                             

                            set varShellScript to "expect -c 'spawn telnet " & varHost & " " & varPort & "; expect \"" & varTelnetPrompt & "\"; send \"" & varSendPayload & "\\r\"; expect \".*\";'"

                             

                            do shell script varShellScript

                             

                             

                             

                             

                             

                             

                            And here is an example of an expect script file which could be invoked using do shell script:

                             

                            #!/usr/bin/expect

                             

                            set vhost [lindex $argv 0]

                            set vport [lindex $argv 1]

                            set vpayload [lindex $argv 2]

                             

                            set vTelnetPrompt "Escape character is*"

                             

                            spawn telnet $vhost $vport

                             

                             

                            expect {

                             

                             

                                $vTelnetPrompt {

                             

                             

                                    send "$vpayload\r"

                             

                             

                                    expect ".*" {}

                             

                                }

                            }

                             

                             

                             

                            The above script could be invoked using AppleScript as follows:

                             

                            do shell script "/path/to/scriptfile 127.0.0.1 80 *SA"

                             

                             

                             

                             

                             

                             

                            In conclusion:

                             

                            I hope this may be of some help.

                             

                            While using this methodology would require some research into best practices for expect scripting, I think that with enough desire on your part, you should be able to get it to work as you wish.

                             

                            Kind regards,

                             

                            -steve

                             

                             

                             

                             

                             

                            References:

                             

                            1) Apple Notes regarding do shell script:

                             

                            https://developer.apple.com/library/mac/technotes/tn2065/_index.html

                             

                            In particular, I'd suggest checking out the "Other Concerns" section, which specifically touches upon strategies for using telnet.

                             

                             

                             

                            2) A forum post regarding this same topic:

                             

                            http://apple.stackexchange.com/questions/127833/need-to-script-connection-to-telnet-and-send-commands-to-remote-device-with-no-u

                             

                             

                             

                            3) Documentation (man page) for expect:

                             

                            https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/expect.1.html

                             

                             

                             

                            4) A tutorial page for expect:

                             

                            http://www.pantz.org/software/expect/expect_examples_and_tips.html

                            • 12. Re: Read a value via a telnet connection
                              KylePutzier

                              Steve,

                              That was exactly the help I needed.

                               

                              This applescript get the job done as I wish, except is takes about 10 seconds to complete. Is there any reason it takes so long?

                              Also, is the last expect just waiting for anything "*" to be returned? If so, why the period  "." ?

                               

                              set varHost to "192.168.1.200"

                              set varPort to 2000

                              set varSendPayload to "*SRA"

                              set varTelnetPrompt to "Escape character is*"

                              set varShellScript to "expect -c 'spawn telnet " & varHost & " " & varPort & "; expect \"" & varTelnetPrompt & "\"; send \"" & varSendPayload & "\\r\"; expect \".*\";'"

                              set varResult to do shell script varShellScript

                              set varValue to last word of varResult

                               

                              tell application "FileMaker Pro Advanced"

                                        set data cell "Count" of current record to varValue

                              end tell

                              • 13. Re: Read a value via a telnet connection
                                BruceRobertson

                                Note that you do not need the tell application "FileMaker Pro Advanced" statement.

                                 

                                You only need the set data statement.

                                • 14. Re: Read a value via a telnet connection
                                  steve_ssh

                                  Hi Kyle,

                                   

                                  Glad you tried it out, and glad that it is showing some promise for you.

                                   

                                  I had used .* as the last match pattern because it is what worked when I was testing on my end.  Using * alone did not work; I believe this is because * will match on nothing, and thus will advance the script forward before any data is returned.  In retrospect, I suppose I could have simply used . as a match pattern.  Regardless, my very generic matching approach is crude, and warrants some thoughtful improvement.

                                   

                                  My suggestion would be to take the script sample that I posted, and start tinkering with it to make it better.

                                   

                                  My guess is that one of the expect patterns is not serving you well, and that expect is just hanging there not matching, and eventually it times out after a default of 10 seconds and then returns control after that.

                                   

                                  Possible Avenues of Investigation:

                                   

                                    - You could try removing the last expect statement, and seeing if that works for you

                                   

                                    - You could try coming up with a better match expression for the last expect statement - one that better matches your target output.

                                   

                                    - You could also try removing or altering the first expect which is looking for the telnet prompt, just in case it is not matching properly.

                                   

                                  If you come up with a good match pattern but you find that expect is not returning the data that you want back to the shell script, you might study up just a tiny bit on the use of expect_out, which is a feature of expect which allows you to access matched data.  In particular, I believe that you would probably make use of expect_out(0,string).

                                   

                                  Bottom Line:

                                   

                                  I think you are real close to what you need, and I encourage you to play with the script and not give up -- I'll bet that you can find the right sequence of expects such that you do not have to endure the timeout value.   I suppose that if you wanted to "cheat", you could simply tell expect to set the timeout to 1 second, but I think it would be worth it to try to figure out how to get the script working well.

                                   

                                  I'll be offline for a couple of days, so I may not see replies till Thursday or Friday, but I'll look back then and see where you are at with this.

                                   

                                  Good luck!

                                   

                                  Best,

                                   

                                  -steve

                                  1 2 Previous Next