1

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()
4
  • Just out of curiosity, why do you believe that Python is better suited for this than a shell script? Commented Mar 30, 2015 at 6:03
  • I am Learning Python .. So, just .. Trying things :).. Commented Mar 30, 2015 at 6:05
  • 1
    You should look into the subprocess module. Commented Mar 30, 2015 at 6:16
  • I am doing . But i am not getting how to add user input. Commented Mar 30, 2015 at 6:23

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.