I have simulated network latency with netem and It's great. Now I want to simulate unplugged network cable or when server goes down. I need this to make testing of my application easier and I couldn't find anything on the web that would help me. My servers are virtual CentOS instances and they are on Virtualbox. I want to do this from a php web page.
Just bring the interface down. For example, with eth0:
ip link set eth0 down
To bring the interface back up:
ip link set eth0 up
When using the top voted answer on a machine you are connecting via SSH, you will bring down the network and have no means of bringing it back up. The following is a way to bring it down and bring it back up while on a SSH connection.
Example using interface
If you run:
ip link set ens32 down
And you are connecting through SSH, you will be unable to bring it up again since you have disabled your network and therefore cannot SSH to your server.
I use the following script to simulate 20 seconds of downtime:
#!/bin/sh ip link set ens32 down sleep 20 ip link set ens32 up
Save it as downtime.sh, make sure the file has execute rights, and then run:
On your virtualbox console window, you should have a Machine menu pull down. Under that one there are several actions. One of which is Power off. You need to select that one for an immediate power down. This is the closest thing to yanking the power cord of a physical server.