Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
#!/bin/bash

# more commands here

expect << EOD
spawn telnet localhost 9151
expect -exact "Trying 127.0.0.1...\r
Connected to localhost.\r
Escape character is '^]'.\r"
send -- "AUTHENTICATE $COOKIE\r"
expect -exact "250 OK\r"
send -- "SIGNAL NEWNYM\r"
expect -exact "250 OK\r"
send -- "GETINFO circuit-status\r"
expect "250 OK\r"
send -- "QUIT\r"
expect -exact "250 closing connection\r
Connection closed by foreign host.\r"
EOD

# more commands here

This is about Expect. The script above establishes a telnet connection to the tor client and after clearing the current circuits, it gets the new circuit status.

GETINFO circuit-status outputs something like

250+circuit-status=
7 BUILT $D313CCBD93E756A53CD667D0A1A97E82B7740067~melco,$DA24B9CD2AA8C02B9068A168C420DC302D969937=TorExit1488,$C00DE13988B4ABC93B43617C0FADAA8E1D4A0293=nabtor BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2014-02-09T13:11:29.264485
5 BUILT $9DD5F90D641D835C4FCA7153148B156E6FD49CEE=darwinfish,$B013CA64C82EDC616BB3EC5DBE35BB7344EDFC2A=sipsrelay,$A59E1E7C7EAEE083D756EE1FF6EC31CA3D8651D7=chaoscomputerclub19 BUILD_FLAGS=IS_INTERNAL,NEED_CAPACITY,NEED_UPTIME PURPOSE=GENERAL TIME_CREATED=2014-02-09T13:11:05.263323
4 BUILT $5B8CCA69DFD88B0281D5E67C7764CA6B5177F210=IchGebDirNeuland,$A587BBB611657B0743CD9E6E70B6497BE209FFD2=RelayW,$D313B081A3EFC5492BE95AFE39F715413DD35586=ToileLibre BUILD_FLAGS=IS_INTERNAL,NEED_CAPACITY,NEED_UPTIME PURPOSE=GENERAL TIME_CREATED=2014-02-09T13:11:04.263266
2 BUILT $9DD5F90D641D835C4FCA7153148B156E6FD49CEE=darwinfish,$F16658975D729B8C4100A6DC649C5EDCAD1687A8=afo8,$35F51DCE73B988CBAE06B06312B4D1271979FE3B=thoreau BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2014-02-09T13:11:02.272687
.
250 OK

I checked the man page, Exploring Expect - A Tcl-based Toolkit for Automating Interactive Programs (O'Reilly) and SO but I cannot make expect_out(buffer) work with set, set env nor puts.

How can I put it in a variable for use in my bash script?

How can I parse a part of it (e.g. C00DE13988B4ABC93B43617C0FADAA8E1D4A0293 above) and put it in a variable for bash?

share|improve this question
add comment

1 Answer

You could build a wrapper in bash, redirect the output of expect to a file, parse the file and set variables. Or variations thereof.

mark5:~/test# cat test.xp
send "bla\n"
mark5:~/test# expect test.xp
bla
mark5:~/test# expect test.xp > test.out
mark5:~/test# cat test.out
bla
mark5:~/test# #now parse test.out
share|improve this answer
    
And how do you redirect the output of expect to a file? That said, I want to do it from expect to bash. –  1.61803 Feb 9 at 23:04
    
See my edit... Why would you not be able to redirect? Create a shell script that runs expect test.xp > test.out and parse what's in test.out after that –  Marki Feb 9 at 23:12
    
Oh I thought you were referring to redirecting here document I posted above. Wrapping is cumbersome. I'd prefer to handle expect's output directly as the sources mentioned state, although I couldn't figure it out. –  1.61803 Feb 10 at 15:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.