Python 如何使用Paramiko生成sudo命令
我对sudo使用paramiko的命令有一些问题Python 如何使用Paramiko生成sudo命令,python,ssh,sudo,paramiko,Python,Ssh,Sudo,Paramiko,我对sudo使用paramiko的命令有一些问题 f、 前sudo-apt-get-update 这是我的密码: try: import paramiko except: try: import paramiko except: print "There was an error with the paramiko module" cmd = "sudo apt-get update" ssh = paramiko.SSHClient()
f、 前sudo-apt-get-update 这是我的密码:
try:
import paramiko
except:
try:
import paramiko
except:
print "There was an error with the paramiko module"
cmd = "sudo apt-get update"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect("ip",username="lexel",password="password")
print "succesfully conected"
except:
print "There was an Error conecting"
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.write('password\n')
stdin.flush()
print stderr.readlines()
print stdout.readlines()
这是一个快速代码。我知道我需要添加sys.exit(1)和所有这些,但这只是为了演示
我将此作为参考:
有命令。它使用Paramico进行ssh连接。您的代码是:
#fabfile.py
from fabric.api import run, sudo
def update():
"""Run `sudo apt-get update`.
lorem ipsum
"""
sudo("apt-get update")
def hostname():
"""Run `hostname`"""
run("hostname")
用法:
发件人:
除了通过fab工具使用外,还可以使用织物的组件
导入到其他Python代码中,为
SSH协议套件的级别高于。
Paramiko(该结构本身利用)
我也遇到了同样的问题,我解决了这个问题: 在sudo文件中,只需添加以下内容: 默认值:您的用户名!要求 或者根据需要删除默认值
还要确保您的用户有权使用sudo运行该命令。这实际上是的一个副本。我建议你读一下这个问题的答案:)。我回答的另一个非常类似的问题可能是重复的
$ fab update -H example.com
[example.com] Executing task 'update'
[example.com] sudo: apt-get update
...snip...
[example.com] out: Reading package lists... Done
[example.com] out:
Done.
Disconnecting from example.com... done.
$ fab --display update
Displaying detailed information for task 'update':
Run `sudo apt-get update`.
lorem ipsum
$ fab --list
Available commands:
hostname Run `hostname`
update Run `sudo apt-get update`.