LeX*_*XeL 5 python ssh sudo paramiko
我使用paramiko
f.ex sudo apt-get update的sudo 命令遇到了一些问题
这是我的代码:
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()
Run Code Online (Sandbox Code Playgroud)
这是一个快速代码.我知道我需要添加sys.exit(1)以及所有这些,但这仅仅是为了演示
我用这个作为参考: Jessenoller.com
Fabric有sudo命令。它使用 Paramico 进行 ssh 连接。你的代码是:
#fabfile.py\nfrom fabric.api import run, sudo\n\ndef update():\n """Run `sudo apt-get update`.\n\n lorem ipsum\n """\n sudo("apt-get update")\n\ndef hostname():\n """Run `hostname`"""\n run("hostname")\nRun Code Online (Sandbox Code Playgroud)\n\n$ fab update -H example.com\n[example.com] Executing task \'update\'\n[example.com] sudo: apt-get update\n...snip...\n[example.com] out: Reading package lists... Done\n[example.com] out: \n\nDone.\nDisconnecting from example.com... done.\n\n$ fab --display update\nDisplaying detailed information for task \'update\':\n\n Run `sudo apt-get update`.\n\n lorem ipsum\n\n$ fab --list\nAvailable commands:\n\n hostname Run `hostname`\n update Run `sudo apt-get update`.\nRun Code Online (Sandbox Code Playgroud)\n\n来自文档:
\n\n\n\n除了通过 fab 工具使用之外,Fabric\xe2\x80\x99s 组件还可以导入到其他 Python 代码中,为 SSH 协议套件提供一个比 Paramiko 提供的更高级别的 Pythonic 接口。 (Fabric 本身就利用了它。)
\n