我使用以下 bash 关闭远程计算机,但是对于每台计算机,我都必须输入密码。是否可以自动输入密码?
#!/bin/bash
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
答案1
你不需要。因为你登录时root
根本没有理由使用sudo
,所以直接运行poweroff
或(更好)shutdown -h
。
如果由于某种问题中未解释的原因而您确实需要sudo
,请使用sudo
的-S
选项。来自man sudo
:
-S The -S (stdin) option causes sudo to read
the password from the standard input
instead of the terminal device. The
password must be followed by a newline
character.
例如:
#!/bin/bash
ssh -t [email protected] "echo PASSWORD1 | sudo -S poweroff"
ssh -t [email protected] "echo PASSWORD2 | sudo -S poweroff"
答案2
为了避免输入密码,请使用ssh-keygen用于身份验证密钥的创建和上传公钥到您所有的服务器。顺便说一句,terdon 说得对,如果您是 root 用户,请不要使用sudo
。
ssh [email protected] poweroff
ssh [email protected] poweroff