如何使用 Paramiko 制作 sudo 命令 [英] How to make a sudo command using Paramiko
本文介绍了如何使用 Paramiko 制作 sudo 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 paramiko 的 sudo 命令时遇到了一些问题
f.ex sudo apt-get 更新
I am having some problems with commands that have sudo using paramiko
f.ex 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) 和所有这些,但这只是为了演示
This is a quick code. I know that I need to add sys.exit(1) and all that but this is just to demostration
我用这个作为参考:Jessenoller.com
推荐答案
我遇到了同样的问题,我解决了这个问题:
I was having the same problem, and I fix with this:
在您的 sudo 文件中,只需添加:
In your sudo file, just add this:
默认值:your_username !requiretty
Defaults:your_username !requiretty
或删除默认要求.
还要确保您的用户有权使用 sudo 运行命令.
Also make sure your user have permission to run the command with sudo.
这篇关于如何使用 Paramiko 制作 sudo 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文