2025年5月

使用sshpass实现ssh、scp等免密登录

ssh 登陆不能在命令行中指定密码,sshpass 的出现则解决了这一问题。它允许你用 -p 参数指定明文密码,然后直接登录远程服务器,它支持密码从命令行、文件、环境变量中读取。

安装:yum install -y sshpass

sshpass [-f|-d|-p|-e] [-hV] command parameters

-f filename   Take password to use from file
-d number     Use number as file descriptor for getting password
-p password   Provide password as argument (security unwise)
-e            Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-P prompt     Which string should sshpass search for to detect a password prompt
-v            Be verbose about what you're doing
-h            Show help (this screen)
-V            Print version information

如:sshpass -p 'root' scp /opt/file.txt root@192.168.126.135:/home