简体   繁体   中英

Python pxssh execute iptables not working

I'm using pxssh to establish an SSH connection to a server. To connection can be establish and I can run simple commands such as ls -l .

What I need now is to create iptable entries via that SSH connection. 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!?

Try modifying your python script like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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