I want to execute few linux commands using python these are my commands.
modprobe ipv6
ip tunnel add he-ipv6 mode sit remote 216.218.221.6 local 117.211.75.3 ttl 255
ip link set he-ipv6 up
ip addr add 2001:470:18:f3::2/64 dev he-ipv6
ip route add ::/0 dev he-ipv6
ip -f inet6 addr
216.218.221.6
117.211.75.3
2001:470:18:f3::2/64
these ip's are the inputs from the user. Commands also need root privileges. My Code upto now.
import os
print("Enter Server Ipv4 Address")
serverip4=input()
print("Enter Local Ipv4 Address")
localip4=input()
print("Enter Client Ipv6 Address")
clientip4=input()
asked Mar 30, 2015 at 6:02
Muneeb K
4771 gold badge10 silver badges21 bronze badges
2 Answers 2
I guess, subprocess would be best choice in this scenario as you want to get all command results and use it. You can refer this page for that: https://docs.python.org/2/library/subprocess.html
Here is the code:
import subprocess
#To use the sudo -> echo "password" | sudo <command>
ipv6_command_list = "echo 'password' | sudo 'ip tunnel add he-ipv6 mode sit remote 216.218.221.6 local 117.211.75.3 ttl 255'"
ip_link_list = "echo 'password' | sudo 'ip link set he-ipv6 up'"
ip_addr_list = "echo 'password' | sudo 'ip addr add 2001:470:18:f3::2/64 dev he-ipv6'"
ip_route_list = "echo 'password' |sudo 'ip route add ::/0 dev he-ipv6'"
ip_inet_list = "echo 'password' | sudo 'ip -f inet6 addr'"
for ip_command in [ip_link_list,ip_addr_list,ip_route_list,ip_inet_list]:
proc = subprocess.check_output(ip_command, shell=True)
answered Mar 30, 2015 at 20:58
Sunil Kapil
1,05013 silver badges12 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Like this:
import sys
import os
os.system("ip tunnel add he-ipv6 mode sit remote %s local %s ttl 255" % (whicheveripvariableisfirst), (whicheveripvariableisnext)))
If you need it run at sudo level then put sudo in the command section or make sure to run the python script as sudo.
Comments
lang-py
subprocessmodule.