have this expect script that tries to automate some tasks using influxdb.
#!/usr/bin/expect -f
spawn influx -username admin -password "XXXX"
expect ">"
send -- "use \"vivienda1-tmp-db\"\r"
expect ">"
send -- "show databases\r"
expect ">"
send -- "DROP database \"vivienda1-tmp-db\"\r"
expect ">"
send -- "DROP database \"vivienda2-tmp-db\"\r"
expect ">"
send -- "exit\r"
It seems something is running asynchronously and does not respond well to the expected symbol (>). Every command in influx should return with that symbol but it seems, from the debuf information below it doesn't. It removes the first database but leaves the last one untouched.
expect -d output is here. It seems to send DROP commands before the output from SHOW is even shown. I am not quite familiar with expect scripts but seems to me like it is executing commands asynchronously. Is it?
spawn influx -username admin -password "XXXX"
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {11870}
expect: does "" (spawn_id exp4) match glob pattern ">"? no
Connected to http://localhost:8086 version 1.6.4
InfluxDB shell version: 1.6.4
expect: does "Connected to http://localhost:8086 version 1.6.4\r\nInfluxDB shell version: 1.6.4\r\n" (spawn_id exp4) match glob pattern ">"? no
>
expect: does "Connected to http://localhost:8086 version 1.6.4\r\nInfluxDB shell version: 1.6.4\r\n> " (spawn_id exp4) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "Connected to http://localhost:8086 version 1.6.4\r\nInfluxDB shell version: 1.6.4\r\n>"
send: sending "use "vivienda1-tmp-db"\r" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern ">"? no
> use "vivienda1-tmp-db"
expect: does " \u001b[1G\u001b[0K> use "vivienda1-tmp-db"\u001b[25G\r\n" (spawn_id exp4) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \u001b[1G\u001b[0K>"
send: sending "show databases\r" to { exp4 }
expect: does " use "vivienda1-tmp-db"\u001b[25G\r\n" (spawn_id exp4) match glob pattern ">"? no
Using database vivienda1-tmp-db
> show databases
expect: does " use "vivienda1-tmp-db"\u001b[25G\r\nUsing database vivienda1-tmp-db\r\n> \u001b[1G\u001b[0K> show databases\u001b[17G\r\n" (spawn_id exp4) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " use "vivienda1-tmp-db"\u001b[25G\r\nUsing database vivienda1-tmp-db\r\n>"
send: sending "DROP database "vivienda1-tmp-db"\r" to { exp4 }
expect: does " \u001b[1G\u001b[0K> show databases\u001b[17G\r\n" (spawn_id exp4) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \u001b[1G\u001b[0K>"
send: sending "DROP database "vivienda2-tmp-db"\r" to { exp4 }
expect: does " show databases\u001b[17G\r\n" (spawn_id exp4) match glob pattern ">"? no
name: databases
name
----
_internal
vivienda1
expect: does " show databases\u001b[17G\r\nname: databases\r\nname\r\n----\r\n_internal\r\nvivienda1\r\n" (spawn_id exp4) match glob pattern ">"? no
vivienda2
vivienda3
vivienda2-tmp-db
vivienda1-tmp-db
expect: does " show databases\u001b[17G\r\nname: databases\r\nname\r\n----\r\n_internal\r\nvivienda1\r\nvivienda2\r\nvivienda3\r\nvivienda2-tmp-db\r\nvivienda1-tmp-db\r\n" (spawn_id exp4) match glob pattern ">"? no
> DROP database "vivienda1-tmp-db"
expect: does " show databases\u001b[17G\r\nname: databases\r\nname\r\n----\r\n_internal\r\nvivienda1\r\nvivienda2\r\nvivienda3\r\nvivienda2-tmp-db\r\nvivienda1-tmp-db\r\n> \u001b[1G\u001b[0K> DROP database "vivienda1-tmp-db"\u001b[35G\r\n" (spawn_id exp4) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " show databases\u001b[17G\r\nname: databases\r\nname\r\n----\r\n_internal\r\nvivienda1\r\nvivienda2\r\nvivienda3\r\nvivienda2-tmp-db\r\nvivienda1-tmp-db\r\n>"
send: sending "exit\r" to { exp4 }```
What is going on here?
If you look carefully at the debug you can see that influxdb is writing the following after each command:
\r\n> \u001b[1G\u001b[0K>
This corresponds to carriage-return newline, the "> " prompt, then an
escape sequence escape[1G
and anther escape[0K
, then the "> "
prompt again. This is probably some misguided attempt to set colours or
similar in the terminal.
What you can try first is to run the influxdb with a different terminal
type, to see if it stops doing this sort of pointless stuff. Try adding
before the spawn
:
set env(TERM) dumb
If that doesn't help, try matching for the end of the second escape sequence each time, so that you only match one prompt:
expect "K> "