Using socket module in website integrated python 3 IDE - python-3.x

import socket
a = socket.gethostbyname(socket.gethostname())
print(a)
When I run this code in the IDE in my pc, it shows the Ipv4 address of my pc.
But when I run this code in the integrated IDE in sololearn, it shows some other Ipv4 address but not mine. How to modify the code to show the Ip address of the user and not the server?

socket.gethostname()
Return a string containing the hostname of the machine where the
Python interpreter is currently executing.
Note: gethostname() doesn’t always return the fully qualified domain
name; use getfqdn() for that.
I've never heard of sololearn, but the python documentation is crystal clear on this one. It's possible your local name resolver is configured strangely, but that is beyond the scope of this question.

Related

Issue getting mac address in NodeJS [duplicate]

I am working on a project using mongodb, express and nodejs to build an intranet based webapp. The goal of the project is to acquire a user mac address upon authentication and run a remote ssh. I am however finding it difficult to get the remote pc mac address. The clients and server are meant to be on the same local subnet. I tried using the node getmac module, but apparently it only gives host server's mac,
var macAddress = require('getmac');
require('getmac').getMac(function(err, macAddress){
if (err) throw err
console.log(macAddress)
});
I also tried the macfromip module but with that you have to predefine the host IP to get the mac of the remote computer.
var macfromip = require('macfromip');
macfromip.getMac('192.168.1.100', function(err, data){
if(err){
console.log(err);
}
console.log(data);
});
Is there any other way i could get the user's mac address?
Doesn't seem like you can get the MAC address from a IP.
The documentation of getMac doesn't have anything related to .getMac('192.168.1.100')
A way I would see this working is having a database with the IPs and their MAC address.
This would require something to save those each MAC address in the database. Probably a script running on startup on each computer.
Someone asked this question on security.stackexchange.com. As it is on the same network, if you use the command ping on the command line, the MAC address of that IP would be registered on ARP list.
ping your_ip_address
And with this you should be able to get the MAC Addresses:
arp -a
With that in mind, I found these 2 nodejs packages that might interest you:
https://www.npmjs.com/package/ping and https://www.npmjs.com/package/node-arp
I imagine you would have to do this client side and pass it back.
By the sounds of this SO answer you can't do it easily in in Javascript but the answer suggests:
Using Java (with a signed applet)
Using signed Javascript, which in FF (and Mozilla in general) gets higher privileges than normal JS (but it is fairly complicated to set up)

Python Pyro4 - Get client IP

I want the server to get the caller's IP in any way. E.g. client calls function on server that checks his IP.
Also I want to know how to get my own external IP using pure Python or built-ins.
Answers are in the manual.
Pyro4.current_context
Pyro4.socketutil.getIpAddress

Finding server in LAN

How to find, in python, server without having it's IP in LAN?
I assume that port will be configured in file so its doesn't have to find port. I tried to search on google but I couldn't find anything useful or that could help me with it.
The server IP will be changing because it will not run constantly on the same computer.
So basically I got app with server that is on random computer in network, and I want to find its IP from another computer.
I would be really thankful for either explanation how to do it or link that could help me to do it.
Not certain if this is what you want to do, I think you want to find the IP of a server by running some python execution on the server?
You could try :
from subprocess import call
call (["ipconfig"])
This will dump the IP config and you can parse out the IP address. This assumes a windows server. You would have to modify the call for your OS.
Updated :
Check this out : TCP Communications or UDP Communications ... it looks like what you are looking for. You will still have the mess of determining the available addresses on the network (arp -a), and testing each one - this would be your client side app. Your server side app, when it receives the right query on the TCP or UDP port, would then determine it's address (ipconfig) and return the value.

Ryu: convert datapath to switch IP address

Currently, I'm writing an application on top of Ryu, the Open-source OpenFlow Controller.
To create an OF-Config connection (or OVSDB connection), I think I have to get the IP address of each switch connected to the Ryu controller. However, I cannot find the API that converts the Datapath object or datapath id to the IP address of the switch.
So, if there is such an API, I want to know about it. If not, I'm looking forward to receiving some comments about the way to make the connections without the IP addresses.
#set_ev_cls(event.EventSwitchEnter)
def switch_features_handler(self, ev):
address = ev.switch.dp.address
dpid = ev.switch.dp.id
"address" is a tuple of (ip_address, port) and "dpid" is the datapath id.
Byungjoon are you using mininet?
If you are, all the switches are instantiated with the localhost ip address (This is mininet's default behavior). The controller distinguishes between switches using the tcp port.
As far as I know, you only need to know the dpid of the switch in order to send OF messages. This is what the sample l2-learning switch is doing: https://github.com/osrg/ryu/blob/master/ryu/app/simple_switch_13.py
I am also try to communicate with the switches using Ryu controller. I am using the above sample as my basic code and adding on top of it. It is not done yet (so you might see some bugs) but it's good starting point. Here is the link: https://github.com/Ehsan70/RyuApps/blob/master/l2.py
for latest version of ryu, you should use following code.
#set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def _switch_features_handler(self, ev):
print(ev.msg.datapath.address)

Same server, same program but started once using one network card and after with another

I have a Linux server with multiple ips (so, multiple eth0, eth0:0, eth0:1 etc).
The script I'm trying to start is a php CLI script which is downloading stuff from an another server API, and I would like to change the IP based on different parameters. Once the script is started, I don't need anymore to change the ip OF THAT SPECIFIC script until his end.
Do you have any clue if it is possible to achieve it?
My other solution was to install Xen or OpenVZ and create N different VPS per each IP, but as you can see is definitely a PITA :-)
You don't specify how you connect to the other server, but with sockets you can try socket_bind.
EDIT:
With curl you can try curl_setopt.
CURLOPT_INTERFACE The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.
I know how to do it in C - you use bind() on your socket before you call connect(), and you bind to the IP address assigned to the desired interface, passing 0 for port. I don't know how to do it in PHP.

Resources