我正在编写一个Python脚本来拒绝远程服务器的基于密码的SSH登录(Ubuntu18.04)。我将在我的主机系统(Windows)中运行Python脚本。因此,我已经使用paramiko模块和初始化SSH客户端与远程主机建立了SSH连接。
为了拒绝基于密码的SSH登录,我应该编辑/etc/ssh/sshd_config文件,并将行“PasswordAuthentication是”改为“PasswordAuthentication no”--注意,我的远程系统允许无密码sudo。我只需要在sshd_config文件中做一个替换。
下面是我尝试过的命令:
ssh_client.exec_command("sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config")我碰到了一个错误:couldn't open temporary file .sedVg2nV6: Permission Denied
以前有人遇到过这种情况吗?除了sed之外,还有其他方法来替换配置文件的一部分吗?sed只适用于.txt文件吗?
发布于 2021-01-04 21:41:26
我觉得你应该改变一下
ssh_client.exec_command("sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config")
至
ssh_client.exec_command("sudo sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config") ( sed命令将使用根权限执行)。
https://stackoverflow.com/questions/65570100
复制相似问题