Modify static IP remotely

First introduce how to set a static IP

On April 26, 2018, ubuntu 18.04 was released, and the SERVER version was downloaded and installed immediately. Use the VM14 version of the virtual machine and start using DHCP to obtain an IP address. There is no accident and you can directly access the Internet. However, when changing the network mode of the VM to bridge mode, when trying to set the virtual machine to a fixed IP, a fault occurs, and the address has not been obtained, and the Internet cannot be accessed. There are often countless tests, and the following experience can be used for reference.

Starting from 17.10, ubuntu has given up the configuration of fixed IP in /etc/network/interfaces, even if the configuration will not take effect, it will be changed to netplan mode, and the configuration will be written in /etc/netplan/01-netcfg.yaml or similar names In the yaml file, after the 18.04 server version is installed, the configuration file is: /etc/netplan/50-cloud-init.yaml. After modifying the configuration, there is no need to restart. Execute the netplan apply command to make the configuration take effect directly. The previous command to restart the network service /etc/init.d/networking restart or services network restrart will also be prompted as an invalid command.

sudo vi /etc/netplan/01-network-manager-all.yaml, the configuration file can be modified as follows.

network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:   #配置的网卡名称
      dhcp4: no    #dhcp4关闭
      dhcp6: no    #dhcp6关闭
      addresses: [192.168.1.55/24]   #设置本机IP及掩码
      gateway4: 192.168.1.254   #设置网关
      nameservers:
          addresses: [114.114.114.114, 8.8.8.8]   #设置DNS

Here is a brief introduction to [192.168.1.55/24], where 192.168.1.55 is the set static IP. The IP address followed by /24 indicates that the mask bit is 24 bits, and the subnet mask is the IP address of 255.255.255.0. There are up to 254 host bits.

Points to note:
1. The above configuration file has 11 lines in total, of which the 2nd, 3rd, 6th, and 7th lines can be left out. It has been tested that the network can work normally without these four lines. The ens33 in the 5th line is a virtual network card, which can be Use ifconfig -a to view the network card of the machine.
2. In the configuration file, there must be a space behind the colon: sign, if there is no space, an error will be prompted when running netplan apply.
3. The key is to see that the configuration is divided into five levels in total, with at least one space behind each layer. The
first layer-network:
the second layer-- ethernets:
the third layer-- ens33:
the fourth layer- ---addresses: [192.168.1.55/24]
fourth layer----gateway4: 192.168.1.254
fourth layer---nameservers:
fifth layer----addresses: [114.114.114.114, 8.8. 8.8]

A similar error occurred: line8 column 6:cloud not find expected ':' #The prompt is a colon: there is no space after it A
similar error occurred: netplan found character that cannot start any token, #The prompt is that the configuration document has not been written according to five levels, it must be The next layer should be one space or more than the previous layer.

Enable to take effect

sudo netplan apply

At this point, you can set a static IP, and then combine ssh and sftp to realize remote modification

directly on the code

    def updateIP(self):
        try:
            IP = self.lineEdit_50.text()
            name = self.lineEdit_51.text()
            if IP == '' or name == '':
                QMessageBox.warning(self, "警告", "请正确输入修改的IP!")
                return
            self.createFilr(IP,name)
            hostname = self.lineEdit.text()
            username = '****'
            password = '****'
            rootdir = '01-network-manager-all.yaml'
            dir_path = '/etc/netplan/'
            scp = paramiko.Transport((hostname, 22))
            scp.connect(username=username, password=password)
            sftp = paramiko.SFTPClient.from_transport(scp)
            ssh = paramiko.SSHClient()
            # 允许连接不在know_hosts文件中的主机
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # 第一次登录的认证信息
            # 连接服务器
            ssh.connect(hostname=hostname, port=22, username=username, password=password)
          #修改配置文件权限,777是最高权限 
            command = "chmod 777 /etc/netplan/ -R"
            stdin, stdout, stderr = ssh.exec_command('echo %s|sudo -S %s' % (password, command))
           
            res, err = stdout.read(), stderr.read()
            result = res if res else err
            sftp.put(rootdir,dir_path+rootdir)
#启用生效
            command = "netplan apply"
            stdin, stdout, stderr = ssh.exec_command('echo %s|sudo -S %s' % (password, command))
           
            res, err = stdout.read(), stderr.read()
            result = res if res else err
      
            scp.close()
            ssh.close()
        
            QMessageBox.warning(self, "提示", "修改成功!")
            return 0
        except:
            QMessageBox.warning(self, "警告", "修改失败!")

# 创建网络配置文件
    def createFilr(self,IP,name):
        file = open('01-network-manager-all.yaml', 'wb')
        str_f = '# Let NetworkManager manage all devices on this system\n' + 'network:\n' + '  version: 2\n' + '  # renderer: NetworkManager\n' \
                + '  ethernets:\n' + '          '+ name +':\n' \
                + '                  addresses: [' + IP + '/24]\n'
        b_f = str.encode(str_f)
        file.write(b_f)
        file.close()

Here, pyqt is combined to create a window tool, which is more convenient to modify IP remotely

Guess you like

Origin blog.csdn.net/sinat_35773915/article/details/132034220