简体   繁体   English

Python pxssh执行iptables不起作用

[英]Python pxssh execute iptables not working

I'm using pxssh to establish an SSH connection to a server. 我正在使用pxssh建立到服务器的SSH连接。 To connection can be establish and I can run simple commands such as ls -l . 可以建立连接,并且可以运行ls -l类的简单命令。

What I need now is to create iptable entries via that SSH connection. 我现在需要的是通过该SSH连接创建iptable条目。 I've tried the following 我尝试了以下

s = pxssh.pxssh()

print(ip)
if not s.login(ip, username, auth_password):
    Log("SSH session failed on login")
    Log(str(s))
else:
    Log("SSH session login successful")
    cmd = 'sudo iptables -I INPUT -p udp -m udp --dport 53 -j ACCEPT;'
    s.sendline(cmd)
    s.prompt()         
    print(s.before) 
    s.logout()

which runs without error, but when connection to the server, no iptable entry had been created!? 哪个运行都没有错误,但是当连接到服务器时,没有创建iptable条目!

Try modifying your python script like this: 尝试像这样修改您的python脚本:

cmd = '/usr/bin/sudo /usr/sbin/iptables -I INPUT -p udp -m udp --dport 53 -j ACCEPT'
s.sendline(cmd)

You should change the sudo and iptables path if it's different on your OS 如果操作系统上的sudo和iptables路径不同,则应更改它

Also try printing the s.sendline(cmd) to see what actually is executed via the ptyhon script on the server, just to be sure that the correct iptables command is executed 还可以尝试打印s.sendline(cmd)来查看通过服务器上的ptyhon脚本实际执行了什么,只是要确保执行了正确的iptables命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM