Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

ssh sudo and stty -echo

Super Advisor Super Advisor

ssh sudo and stty -echo

Here is a sample of what I am trying to do

#!/bin/sh -xv

ssh backupserv2 stty -echo

sudo find /nsr -mtime -2 -exec ls -l {} \;

stty echo


I am running a script on one platform it does a ssh command over to another system and runs the same script which does a find command with sudo (there are files sub directories that have restrictions), but the sudo comes back and prompts for the password but when the user types in the password it is in clear text so I have been playing around with the stty -echo command I get the following error

+ echo Sudo will ask you for a passwd use your login passwd
+ [ backupserv1 = backupserv1 ]
+ stty -echo
stty: : Invalid argument
+ sudo find /nsr -mtime -7 -type f -print

Anyone know of a trick that I can hide the keying in of the password.
Honored Contributor Honored Contributor

Re: ssh sudo and stty -echo

Latest sudo versions support -S option. It accepts the password from std. input. Also you can as follows, but this has security risk involved. At most you can have read only access for a file.

ssh "some_host" "sudo "something" < ./abc

File abc contains the password.

There is no substitute to HARDWORK
Trusted Contributor Trusted Contributor

Re: ssh sudo and stty -echo

Have you tried putting the "stty -echo" in a script on the remote machine and running that script via ssh?
I think the command "ssh backupserv2 stty -echo" (as you have it above) would execute the stty -echo command on the remote host, then drop the ssh connection, so what's the point in that?
What does this button do?
Respected Contributor Respected Contributor

Re: ssh sudo and stty -echo

This is how we handle similar situations:

First off - make a file that contains the server(s) you want to work with: /roots/bin/all.sites

Second - write a simple for loop:
for i in $(cat /roots/bin/all.sites)
echo $i >> /tmp/sudo.txt
ssh ${i} sudo find /nsr -mtime -2 -exec ls -l {} \; >> /tmp/sudo.txt

caveat: I'm not sure about the sudo command part of this loop as I just copied it from your original, but something of this order should work and you should be able to view the file /tmp/sudo.txt for problems or errors.

Hope this helps

"All there is to thinking is seeing something noticeable which makes you see something you weren't noticing which makes you see something that isn't even visible." - Norman Maclean
Super Advisor Super Advisor

Re: ssh sudo and stty -echo

I found my own answer. On the host box prior to the ssh command to the client, I run the stty -echo after the sudo commands (the password keyin is null on the screen) on the client and I return to the host box I do the stty echo and things are back to normal. I still get the invalid arg but I redirect that to /dev/null so the display is clean throughout.
Occasional Visitor

Re: ssh sudo and stty -echo

I just solved this problem this morning. Use ssh -t . The password is not echoed back to the terminal.