1

I want to simulate a file download via http(s) from within a Python script. The script is used to test a router/modem appliance in my local LAN.

Currently, I have a virtual machine's network configured to use the appliance as default gateway and DNS. The python script on my test machine sshs into the VM using the Phython's paramiko library. From within the python script using this ssh connection I run wget https://some.websi.te/some_file.zip. That seems like overkill but I don't want to reconfigure the network on my machine running the test script.

Is there a way to eliminate the extra VM (the one that runs wget via ssh) and still simulate the download? Of course that should run within my python test script on my local machine?

In other words can I get python to use another gateway than the system default from within a script?

1
  • No, routing and HTTP happen on very different layers of the OSI stack, Commented Sep 22, 2017 at 11:16

1 Answer 1

2

I can provide 2 ways to do want you want: a bad way, and a clean way. Let's talk about the bad way:

If your script is using "the default gateway" of the system, may be, by setting up a dedicated route for your destination used by your script into your system to avoid your script to use the default gateway, may be enough to solve your problem. This is a bad way because not only your script will be impacted, all the traffic generated from your host to that destination will be impacted.

Let's talk about the clean way. I suppose your are running Linux, but I think you may be able to do the same in all OS that are supporting multi-routing tables feature into their kernel.

Here is the summary of the steps you need to do:

  • to prepare the second (or third ...) routing table using the ip command and the 'table table_name' option
  • to create a rule traffic selector using ip rule using the uidrange option. This option allows you to select the traffic based on the UID of the user.

When you have done these steps, you can test your setup by creating a user account within the range of UID selected into ip-rule command and test your routing setup.

The last step, is to modify your python script by switching your uid to the uid selected in ip-rule, and existing from it when you have finish to generate your traffic.

To summarize the clean way, it is to do the same as the bad way, but just for a limited number of users uid of your Linux system by using the multi-routing tables kernel feature, and to switch to the selected user into your script to use the special routing setup.

The multi-routing tables feature of kernel is the only feature I think that is able to merge 2 different systems, as you requested it, your host and its VM, without the need of VM. And you can activate the feature by switching from one user to another one into your script of any language.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay will experiment a little and post a guide, if I succeed. Thanks.

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.