This is the fourth article written by AFeng in Changchun. Changchun local technical public number, welcome all kinds of attention.

It’s been another week, and the days have flown by.

This week seems to be very busy. Apart from the daily code, I did the technical selection, drew the product prototype, and made my first NTF. What I spent the most energy on is the struggle against the company’s network environment.

I have done so many years of development, here is a heartfelt joke:

No matter in Beijing or Changchun, no matter in which company, no matter in large or small factories, the network environment of the company is speechless, and it takes time to adjust the scene. Sometimes I don’t know whether I’m a developer or a network manager.

Of course, ridicule is ridicule, after all, the domestic network environment is complex, plus a variety of security indicators, the network management brothers and sisters you hard.

What happened to the company’s Internet

Because the development of the company has confidential content, so the internal network environment must be safe. When I came to the company, the back-end development of their own set up Mevan private server, interface testing through the internal network environment is no problem.

But after all, the front end must be linked to the external environment for testing, and the map and sharing functions within the project also need the external environment, so the company opened an external environment for testing. Uploading code and related documents requires an Intranet connection.

After using it for a few days, I wrote a short shell to switch wireless networks without using mouse clicks to interact. After all, switching at least five times a day is too much repetitive work.

The networkSetup directive is mainly used:

Networksetup - setairPortNetwork en0 [wifi name] [wifi password]Copy the code

The networksetup command can be used to query the system IP address:

networksetup -getinfo "Wi-Fi"
Copy the code

Much simpler than using ifconfig output, since we don’t normally need that much detail.

ZSHRC file (yes, my terminal is using ZSH, if you are using bash or another terminal, you can find the corresponding configuration file)

Alias wifi_internet="networksetup - setairportNetwork en0 [wifi name] [wifi password]" alias wifi_test="networksetup -setairportNetwork en0 [Intranet wifi name] [wifi password]"Copy the code

Be sure to note that this is a shell script and that there is no space before or after the equals sign

In this case, you only need to enter wifi_Internet and wifi_test on the terminal to change the network.

I always leave the terminal on when I’m working on a project, so it’s much easier to switch networks and make development comfortable.

Things are getting out of hand

What happened was that suddenly there were trojans in the routing environment of the company.

The company made an immediate decision to stop the wireless network that could only connect to the internal network, but only the network cable of the desktop configured by the company could be used to connect to the internal network.

After writing your own code, you have to copy it to a secure USB drive, insert it into your desktop, copy and paste the code, and synchronize the code repository.

I suddenly died on the spot, do not give up the ping of the entire route under the IP, confirm that can not link. Hence beg the leader to be accommodating, finally want to come a few static IP that can link, and want to log in the security information after the link, can use wifi to connect Intranet.

Although it is much more troublesome, fortunately, I can upload the code on my own laptop, and the comfortable environment before is no longer, so I have to follow the existing logic and rewrite the script linking the network to reduce repetitive work.

There are a few points to make here.

Link to the network

  • Before connecting to the Intranet, set a static IP address. Therefore, you need to run two commands.
  • Set the IP to at least the admin user, so add sudo and enter the local user password.
$Networksetup - setairPortNetwork en0 [Intranet wifi name] [Intranet wifi password]
$Sudo networksetup -setmanual Wi-Fi [static IP] [subnet mask, 255.255.255.0 by default]
Copy the code

Link to the network

  • To connect to the Internet, connect to the Internet first, and then reset the IP address to dynamically obtain the IP address
  • If you do not reset DHCP dynamic access, your computer will use the static IP address and static gateway you set to connect to the external environment
$Networksetup - setairPortNetwork en0
$ sudo networksetup -setdhcp Wi-Fi
Copy the code

The alias simplified

All that’s left is to configure another alias to simplify the invocation.

Alias wifi_test="networksetup -setairportNetwork en0 [Intranet wifi name] [Intranet wifi password] &&sudo networksetup -setmanual Wi-Fi [static IP address] [Subnet mask, 255.255.255.0 by default] [Gateway IP address, ]" alias wifi_Internet ="networksetup - setairportNetwork en0 [wifi name] [wifi password] && sudo networksetup -setdhcp Wi-Fi"Copy the code

The middle && symbol represents sequential execution

Ah, everything is comfortable again

conclusion

Through this experience, master the network configuration of the command, in fact, these configurations are written in the network configuration file, “all files”, is really magical.

New people are not easy, you are welcome to strong attention, Changchun local technology public number – A Fung in Changchun ━(‘ ∀´) Blue!

The article quotes the following

  • Mac networksetup command
  • Linux shell &, &&, | and | | usage