Send command and capture output

Posted by Broud on Fri 01 Jan 2010 12:35 PM — 4 posts, 19,972 views.

Netherlands #0
I'm trying to make a script that send a series of commands to the mud, and captures its consecutive output to a notepad file. I've been looking at the scripting and LUA pages, but i'm overwhelmed. I've been able to send open a notepad file, sends the commands, but how do I redirect the generated output to the notepad file? I don't know how long the output will be from the several commands. This is what I have so far:

Note "Running script."
AppendToNotepad("HEADING")
Send ("command 1 Broud")
?CAPTURE OUTPUT for 1?
Send ("command 2 Broud")
?CAPTURE OUTPUT for 2?
....
Send ("command 19 Broud")
?CAPTURE OUTPUT for 19?

I'm doing a series of 19 commands to log several items to the notepad file. So... my next question is, how do i put i a for-do loop for the commands... :)

Australia Forum Administrator #1
You can't directly do what you are describing, for the simple reason that the MUD server does not reply immediately to your commands. It has other players to deal with, and may send other stuff to you. For example, if you type:


inventory


You may see something like this *before* the inventory:


The guard arrives from the south.

<24hp 145m 105mv> <#21000> 

Hailiwyn chats, 'Check out what the fruit vendor is selling!'


What you really need to do is send the command, and then have a trigger that waits for the reply, the trigger being something that matches a keyword in the reply.

For example, if your inventory looks like this:


You are carrying:
     a large burlap sack
     a rusty iron sword
     some moldy cheese

<20hp 100m 100mv> <20hp 100m 100mv> 


Then the keyword might be "You are carrying:" to start an inventory list, and a completely blank line to end it.

This thread may help you:

Template:post=4957
Please see the forum thread: http://gammon.com.au/forum/?id=4957.


That shows how you can make a script that send commands, and then pauses (using triggers and coroutines) to wait for certain output. You still need to have a good idea of what the wanted lines look like.
Australia Forum Administrator #2
I have made a video showing the general idea for capturing multiple lines (and waiting for things to arrive).

More details are at:

Template:post=9965
Please see the forum thread: http://gammon.com.au/forum/?id=9965.
Amended on Tue 26 Nov 2013 03:44 AM by Nick Gammon
Netherlands #3
Thanks for your replies. I'll work on it with you suggestions. And happy new year :)