What I'm trying to do is execute an ssh command that prints out the /etc/shadow file. Obviously that file requires root or sudo permission, and I don't have root creds, therefore I have to use sudo.

Here is the command I've worked out:

expect -c 'spawn -noecho ssh -q -t -S /tmp/t2.ssh dummy "sudo cat /etc/shadow";expect "assword"; send "password99\r";interact'

It outputs exactly what I need, but it also outputs the prompt for the sudo password on the very first line:

[sudo] password for student99:

Without using other programs (like grep, awk, tail, etc) is there a way to modify the expect command so that it prints only the output of the cat command and not the [sudo] password for student99: prompt?

Thanks to @larsks, my final working command is:

expect -c 'spawn -noecho ssh -q -t -S /tmp/t2.ssh dummy "sudo cat /etc/shadow";log_user 0;expect "assword"; send "password99\r";interact'

You can disable output logging by setting log_user 0. From the docs:

log_user -info|0|1
    By default, the send/expect dialogue is logged to stdout (and a logfile if
    open). The logging to stdout is disabled by the command "log_user 0" and
    reenabled by "log_user 1". Logging to the logfile is unchanged.

    The -info flag causes log_user to return a description of the most recent
    non-info arguments given. 
  • This is it! Thank you! My final command is: expect -c 'spawn -noecho ssh -q -t -S /tmp/t2.ssh dummy "sudo cat /etc/shadow";log_user 0;expect "assword"; send "password99\r";interact' – beechfuzz May 22 '19 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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