[英]SSH into Remote machine and edit file as a root user using Python
I am working on a Python script that can SSH into multiple remote CentOS machines and change the value of 'ONBOOT' from 'yes' to 'no' in '/etc/sysconfig/network-scripts/ifcfg-eth1'. 我正在研究一个Python脚本,该脚本可以SSH到多台远程CentOS计算机中,并将“ / etc / sysconfig / network-scripts / ifcfg-eth1”中“ ONBOOT”的值从“ yes”更改为“ no”。 I can SSH into remote machines using Paramiko with my user credentials.
我可以使用带有用户凭据的Paramiko SSH到远程计算机。 In order to edit the '/etc/sysconfig/network-scripts/ifcfg-eth1' file I have to become a sudo user and then only I can edit the file.
为了编辑“ / etc / sysconfig / network-scripts / ifcfg-eth1”文件,我必须成为sudo用户,然后只有我才能编辑该文件。 The Problem I am facing with my script is I cannot login directly as a root user into CentOS.
我的脚本面临的问题是我无法以root用户身份直接登录CentOS。 I should first login with my user credentials and Then change to root using 'sudo -s' and password.
我应该先使用用户凭据登录,然后使用“ sudo -s”和密码更改为root。 Is there any way to ssh into the remote machine with my user credentials and change to root user and edit the file.?
有什么方法可以使用我的用户凭据ssh到远程计算机,然后更改为root用户并编辑文件。
First, make a connection using ssh.connect of paramiko and then 首先,使用paramiko的ssh.connect建立连接,然后
import paramiko
ssh = paramiko.SSHClient()
ssh.connect("hostname", username = "username", password = "password")
cmd = "echo {} | sudo -S {}".format("password", "touch /opt/giri")
ssh.exec_command(cmd)
The logined user is a normal user with sudo permission. 登录的用户是具有sudo权限的普通用户。 So first the script logins as "normal user" and then executes the command with sudo permission.
因此,脚本首先以“普通用户”身份登录,然后以sudo权限执行命令。
-S option of the sudo command to make sudo get the its from stdinread the password from the standard input instead of using the terminal device. sudo命令的-S选项使sudo从stdin读取标准输入的密码,而不使用终端设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.