Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Extra newline in Expect output

483 views
Skip to first unread message

Justin Zamora

unread,
Jan 24, 2016, 10:34:28 PM1/24/16
to
I have an Expect script that runs a command on an remote server using ssh and sudo. The script works great, with one small quirk: there is a superfluous newline at the beginning of the output. Some testing seems to indicate that this is happening as part of the "interact" command am using to catch EOF. How can I suppress this newline?

Here is an example (note the blank line that I don't want).
$ ./test.tcl
----- server1 -----

*******************************************************************************
* *
* This is the message of the day. *
* *
*******************************************************************************
$

Here is the script I am using. As I said, it has been working for years with no problems except for the quirk with the extra newline, which I am finally getting around to trying to eliminate.

#!/usr/bin/expect

puts "----- server1 -----"

log_user 0

spawn -noecho /usr/bin/ssh -t -q -o BatchMode=yes -o StrictHostKeyChecking=no server1 /usr/bin/sudo /bin/cat motd
expect "Password:"
send "NotMyRealPassword\r"

set ssh_open 1
interact {
-o eof { set ssh_open 0; return}
}
if {$ssh_open} {
send "logout\r"
expect eof
wait
}

log_user 1
return 0



Uwe Klein

unread,
Jan 25, 2016, 7:50:06 AM1/25/16
to
Am 25.01.2016 um 04:34 schrieb Justin Zamora:
> I have an Expect script that runs a command on an remote server using ssh and sudo. The script works great, with one small quirk: there is a superfluous newline at the beginning of the output. Some testing seems to indicate that this is happening as part of the "interact" command am using to catch EOF. How can I suppress this newline?
>
> Here is an example (note the blank line that I don't want).
> $ ./test.tcl
> ----- server1 -----
>
> *******************************************************************************
> * *
> * This is the message of the day. *
> * *
> *******************************************************************************
> $
>
> Here is the script I am using. As I said, it has been working for years with no problems except for the quirk with the extra newline, which I am finally getting around to trying to eliminate.
>
> #!/usr/bin/expect
>
> puts "----- server1 -----"
>
> log_user 0
>
> spawn -noecho /usr/bin/ssh -t -q -o BatchMode=yes -o StrictHostKeyChecking=no server1 /usr/bin/sudo /bin/cat motd
> expect "Password:"
> send "NotMyRealPassword\r"

#something like ( you need to check on successfull login anyway, right?)
expect \n \
TIMEOUT {puts TIMEOUT; exit
} EOF {puts EOF!; exit

Justin Zamora

unread,
Jan 25, 2016, 11:11:04 PM1/25/16
to
On Monday, January 25, 2016 at 7:50:06 AM UTC-5, Uwe Klein wrote:
> > spawn -noecho /usr/bin/ssh -t -q -o BatchMode=yes -o StrictHostKeyChecking=no server1 /usr/bin/sudo /bin/cat motd
> > expect "Password:"
> > send "NotMyRealPassword\r"
>
> #something like ( you need to check on successfull login anyway, right?)
> expect \n \
> TIMEOUT {puts TIMEOUT; exit
> } EOF {puts EOF!; exit
> }

I found the solution. It turns out I was getting \r instead of \n, so adding "expect \r" after sending the password:

Thanks for the help.
0 new messages