Programing

TTY를 무시하지 않고 암호를 su / sudo / ssh에 전달하는 방법은 무엇입니까?

lottogame 2020. 6. 21. 19:41
반응형

TTY를 무시하지 않고 암호를 su / sudo / ssh에 전달하는 방법은 무엇입니까?


suor sudo또는을 수행 할 C Shell 프로그램을 작성 중 ssh입니다. 그들은 모두 stdin이나 명령 줄이 아닌 콘솔 입력 (TTY)에 암호를 원합니다.

아무도 해결책을 알고 있습니까?

비밀번호없이 설정하는 sudo것은 옵션이 아닙니다.

는 옵션이 될 수 있지만 벗겨진 시스템에는 없습니다.


sudo의 경우 표준 입력에서 비밀번호를 승인하는 -S 옵션이 있습니다. man 항목은 다음과 같습니다.

    -S          The -S (stdin) option causes sudo to read the password from
                the standard input instead of the terminal device.

이를 통해 다음과 같은 명령을 실행할 수 있습니다.

echo myPassword | sudo -S ls /tmp

ssh에 관해서는, 성공하지 않고 사용법을 자동화 / 스크립팅하려고 많은 시도를했습니다. 프롬프트없이 암호를 명령에 전달하는 기본 제공 방법이없는 것 같습니다. 다른 사람들이 언급했듯이, " expect "유틸리티는이 딜레마를 해결하기위한 것 같지만 궁극적으로 올바른 개인 키 인증을 설정하는 것이 자동화를 시도 할 때 올바른 방법입니다.


대화 상자를 통해 암호를 묻는 Applescript를 작성한 다음 다음과 같이 사용자 정의 bash 명령을 작성합니다.

echo <password> | sudo -S <command>

이것이 도움이되는지 확실하지 않습니다.

sudo가 사전 암호화 된 암호를 수락하면 스크립트 내에서 암호를 암호화하고 일반 텍스트 암호를 에코하는 것에 대해 걱정할 필요가 없습니다. 그러나 이것은 나와 내 상황에 효과적입니다.


들어 ssh당신은 사용할 수 있습니다 sshpass: sshpass -p yourpassphrase ssh user@host.

sshpass를 먼저 다운로드해야합니다 :)

$ apt-get install sshpass
$ sshpass -p 'password' ssh username@server

나는 가지고있다 :

ssh user@host bash -c "echo mypass | sudo -S mycommand"

나를 위해 작동합니다.


sudo의 경우 다음과 같이 할 수도 있습니다.

sudo -S <<< "password" command

연결할 대상 호스트에서 공개 / 개인 키를 설정하면됩니다. 첫 번째 단계는 다음을 실행하여 로컬 호스트에서 스크립트를 실행하는 사용자에 대한 ssh 키를 생성하는 것입니다.

ssh-keygen
Enter file in which to save the key (/home/myuser/.ssh/id_rsa): <Hit enter for default>
Overwrite (y/n)? y

그런 다음 빈 암호를 입력하십시오. 그런 다음 ssh 키를 연결할 대상 호스트에 복사하십시오.

ssh-copy-id <remote_user>@<other_host>
remote_user@other_host's password: <Enter remote user's password here>

ssh 키를 등록 ssh remote_user@other_host하면 로컬 호스트에서 자동 응답을 수행 할 수 있습니다 .


이 문제에 대한 일반적인 해결책은 수퍼 유저 액세스가 필요한 작업을 수행하는 도우미 앱을 설정하는 것입니다. http://en.wikipedia.org/wiki/Setuid

Sudo는 오프라인에서 사용하기위한 것이 아닙니다.

나중에 편집 : SSH는 개인-공개 키 인증과 함께 사용할 수 있습니다. 개인 키에 암호가 없으면 암호를 묻지 않고 ssh를 사용할 수 있습니다.


더 나은 선택이 없다면 (다른 사람들이 제안한대로) man socat이 도울 수 있습니다.

   (sleep 5; echo PASSWORD; sleep 5; echo ls; sleep 1) |
   socat - EXEC:'ssh -l user server',pty,setsid,ctty

          EXEC’utes an ssh session to server. Uses a pty for communication
          between socat and ssh, makes it ssh’s  controlling  tty  (ctty),
          and makes this pty the owner of a new process group (setsid), so
          ssh accepts the password from socat.

pty, setsid, ctty의 모든 복잡성이 필요하며 오랫동안 잠을 잘 필요가 없지만 잠을 잘 필요가 있습니다. echo = 0 옵션은 ssh의 명령 행에서 원격 명령을 전달하는 것처럼 볼만한 가치가 있습니다.


어쩌면 expect명령을 사용할 수 있습니까? :

expect -c 'spawn ssh root@your-domain.com;expect password;send "your-password\n";interact

이 명령은 암호를 자동으로 제공합니다.


expect리눅스 유틸리티를 살펴보십시오 .

stdin의 간단한 패턴 일치를 기반으로 stdio에 출력을 보낼 수 있습니다.


Set SSH up for Public Key Authentication, with no pasphrase on the Key. Loads of guides on the net. You won't need a password to login then. You can then limit connections for a key based on client hostname. Provides reasonable security and is great for automated logins.


ssh -t -t me@myserver.io << EOF
echo SOMEPASSWORD | sudo -S do something
sudo do something else
exit
EOF

I had the same problem. dialog script to create directory on remote pc. dialog with ssh is easy. I use sshpass (previously installed).

   dialog --inputbox "Enter IP" 8 78 2> /tmp/ip

   IP=$(cat /tmp/ip)


   dialog --inputbox "Please enter username" 8 78 2> /tmp/user

   US=$(cat /tmp/user)


   dialog --passwordbox "enter password for \"$US\" 8 78 2> /tmp/pass

   PASSWORD = $(cat /tmp/pass)


   sshpass -p "$PASSWORD" ssh $US@$IP mkdir -p /home/$US/TARGET-FOLDER


   rm /tmp/ip

   rm /tmp/user

   rm /tmp/pass

greetings from germany

titus


Building on @Jahid's answer, this worked for me on macOS 10.13:

ssh <remote_username>@<remote_server> sudo -S <<< <remote_password> cat /etc/sudoers

Hardcoding a password in an expect script is the same as having a passwordless sudo, actually worse, since sudo at least logs its commands.


한 가지 방법은 read -s 옵션을 사용하는 것입니다.이 방법으로 암호 문자가 화면에 다시 에코되지 않습니다. 일부 사용 사례에 대한 작은 스크립트를 작성했으며 내 블로그에서 볼 수 있습니다 : http://www.datauniv.com/blogs/2013/02/21/a-quick-little-expect-script/


사용하다:

echo password | sudo command

예:

echo password | sudo apt-get update; whoami

도움이 되길 바랍니다 ..


스크립트를 예상하기 위해 비밀번호를 매개 변수로 제공 할 수 있습니다.


echo <password> | su -c <command> <user> 

작동합니다.


su -c "Command" < "Password"

도움이 되길 바랍니다.

참고 URL : https://stackoverflow.com/questions/233217/how-to-pass-the-password-to-su-sudo-ssh-without-overriding-the-tty

반응형