我的远程主机是Ubuntu。我的意图是以非root用户身份运行一些sudo命令(例如:ip route add<>)。 这是我的代码片段:
def add_route_in_extrtr(rtrip,route,nexthop,action='add',ostype='ubuntu',user='test',pwd='test123'):
"""
Adding Routes in ExtRtr(Ubuntu)
"""
env.host_string = rtrip
env.user = user
env.password = pwd
env.sudo_user = user
if action == 'add':
sudo("echo hello")
sudo("ip route add %s via %s" %(route,nexthop),pty=False)
if action == 'update':
sudo("ip route del %s" %(route))
sudo("ip route add %s via %s" %(route,nexthop),pty=False)
现在调用上面的函数时,我一直看到下面的失败:
[172.28.184.38] sudo: echo hello
2016-02-18 09:25:45,221 [INFO] paramiko.transport - Connected (version 2.0, client OpenSSH_6.6.1p1)
2016-02-18 09:25:45,343 [INFO] paramiko.transport - Authentication (publickey) failed.
2016-02-18 09:25:45,373 [INFO] paramiko.transport - Authentication (password) successful!
[172.28.184.38] out: hello
[172.28.184.38] out:
[172.28.184.38] sudo: ip route add 55.55.55.0/24 via 1.102.1.254
[172.28.184.38] out: RTNETLINK answers: Operation not permitted
[172.28.184.38] out:
Fatal error: sudo() received nonzero return code 2 while executing!
Requested: ip route add 55.55.55.0/24 via 1.102.1.254
Executed: sudo -S -p 'sudo password:' -u "test" /bin/bash -l -c "ip route add 55.55.55.0/24 via 1.102.1.254"
Aborting.
在Ubuntu服务器上,我按照Fabric sudo执行的方式运行cmd,它会抛出上述错误。
test@ext-rtr:~$ sudo -S -p 'sudo password:' -u "test" /bin/bash -l -c "ip route add 55.55.55.0/24 via 1.102.1.254"
RTNETLINK answers: Operation not permitted
此外,路由不存在于linux路由表中。我在调用此函数之前检查了
根据'sudo'源代码和文档,我找不到我可能遗漏的东西。我通过挖掘stackoverflow存档尝试了一些帮助,但无法通过此失败。任何帮助,或者如果你能指出我错过了什么/做错了什么,非常感谢