Target
Need to develop a monitoring program under Linux that uses the network to log on to another Linux server A to execute commands and get command execution results, and then analyze the data
Basic Programme
Because there are no Linux computers, you can only use VMware to install Linux virtual machine B, such as Ubuntu 12.04LTS
Because of the network security problem of the LAN, Linux virtual machine B cannot communicate directly with Linux Server A, so the two cannot be connected to the shared network through the switch. So consider connecting the two cables directly to form a private local area network.
There are many ways for virtual machines to communicate with the outside world, such as Nat, bridged (http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646007.html), Because Linux server A needs to be frequently replaced and its network defaults to DHCP, in order to avoid manually configuring IP latency, consider building a DHCP server on virtual machine B, automatically assigning IP to Linux Server A, and automatically knowing the IP of Linux Server A, In this mode, the network mode of virtual machine can only adopt bridged mode.
Installing the DHCP service
You need to access the external network when you install the software, so set the virtual machine mode to NAT (the easiest way to access the Internet)
A very good feature of Ubuntu is that if you execute a command that does not exist, it will recommend a related package that prompts you to install so just enter a fuzzy matching command to find the software you want to install
sailing@ubuntu:~$ ping-c 1 baidu.com
PING baidu.com (220.181.111.86) bytes of data.
Bytes from 220.181.111.86:icmp_req=1ttl=128 time=6.18 ms
---baidu.com ping statistics---
1 packets transmitted, 1 received, 0%packet loss, time 0ms
RTT Min/avg/max/mdev = 6.180/6.180/6.180/0.000ms
sailing@ubuntu:~$ DHCP
No command ' DHCP ' found, did you mean:
Command ' DHCPX ' from package ' Irpas ' (multiverse)
Command ' dhcpd ' from package ' isc-dhcp-server ' (main)
Command ' dhcpd ' from package ' Isc-dhcp-server-ldap ' (universe)
Command ' HCP ' from package ' Lam4-dev ' (universe)
Command ' chcp ' from package ' Nilfs-tools ' (universe)
Command ' DCCP ' from package ' Dcap ' (universe)
Command ' DICP ' from package ' dish ' (universe)
Dhcp:command not found
sailing@ubuntu:~$ DHCPD
The program ' DHCPD ' can is found in thefollowing packages:
*isc-dhcp-server
*isc-dhcp-server-ldap
Try:sudo Apt-get Install <selectedpackage>
sailing@ubuntu:~$ sudo apt-get install dhcp3-server
Reading Package Lists ... Done
Building Dependency Tree
Reading state information ... Done
The following extra packages would beinstalled:
Isc-dhcp-server
Suggested Packages:
Isc-dhcp-server-ldap
The following NEW packages would beinstalled:
Dhcp3-server Isc-dhcp-server
0 upgraded, 2 newly installed, 0 to Removeand upgraded.
Need to get 431 KB of archives.
Configuring a DHCP listening network card
The configuration file is isc-dhcp-server instead of Dhcp3-server probably because of the 12.04 version of the problem
Interfaces Specifies the network card for monitoring that can be obtained according to the ifconfig command
Sailing@ubuntu:~/rmc_diag/release-v3$ifconfig | grep "Link EnCap"
eth0 Link encap:ethernet hwaddr 00:0c:29:da:44:87
Lo Link encap:local Loopback
Virbr0 Link encap:ethernet HWADDR6E:81:A5:7C:24:CF
sailing@ubuntu:~/rmc_diag/release-v3$ Cat/etc/default/isc-dhcp-server
# Defaults for DHCP Initscript
# sourced BY/ETC/INIT.D/DHCP
# installed At/etc/default/isc-dhcp-serverby the maintainer scripts
。。。。。。
# on what interfaces should the DHCP server (DHCPD) serve DHCP requests?
# Separatemultiple interfaces with spaces, e.g. "eth0 eth1".
interfaces= "Eth0"
sailing@ubuntu:~/rmc_diag/release-v3$ Ll/etc/default/isc-dhcp-server
-rw-r--r--1 root 323 Sep 07:49/etc/default/isc-dhcp-server
sailing@ubuntu:~/rmc_diag/release-v3$ Ll/etc/default/dhcp3-server
Ls:cannot access/etc/default/dhcp3-server:no such file or directory
Configuring a DHCP server
Which is the DHCP configuration directory. There's a disagreement about how to decide for a while that it's/ETC/DHCP3.
sailing@ubuntu:~$ ls/etc/dhcp*
/ETC/DHCP:
dhclient.conf DHCLIENT-EXIT-HOOKS.D DHCLIENT-ENTER-HOOKS.D
/ETC/DHCP3:
Dhclient-enter-hooks.d
The specific syntax of the DHCP configuration file can be referred to http://www.linuxmanpages.com/man8/dhcpd.8.php
Except for the following, all other comments are dropped with #
# If This DHCP server was the official dhcpserver for the local
# Network, the authoritative directiveshould be uncommented.
authoritative;
# A slightly different configuration for aninternal subnet.
Subnet 192.168.2.0 netmask 255.255.255.0 {
Range 192.168.2.10 192.168.2.255;
Option domain-name-servers192.168.2.1,192.168.2.2;
Option Domain-name "Ubtw.net";
Option routers 192.168.2.3;
Option broadcast-address 192.168.2.255;
Default-lease-time 600;
Max-lease-time 7200;
}
Configuring IP for a DHCP server
The DHCP server cannot assign IP to itself so its own IP needs to be statically configured and configured before starting the DHCP service
On the Internet are introduced through the following configuration file to turn IP, but I did not succeed found that there is a conflict with the graphical interface so only with the graphical interface configuration
sailing@ubuntu:~$ cat/etc/network/interfaces
Auto Lo
Iface Lo inet Loopback
The following configuration is done through the graphical interface to be closed and then opened before it can take effect through the upper-right switch
Start the DHCP service
The command to start DHCP is Sudo/etc/init.d/isc-dhcp-serverstart instead of Sudo/etc/init.d/dhcp3-server start
sailing@ubuntu:~$ sudo/etc/init.d/isc-dhcp-server Start
Rather than invoking Init scripts through/etc/init.d, use the service (8)
Utility, e.g service Isc-dhcp-server start
Since The script you are attempting toinvoke has been
Upstart job, you may also use the Start (8) utility, e.g. start isc-dhcp-server
Isc-dhcp-server start/running, process28781
Prompt should use service command OK let's change one.
sailing@ubuntu:~$ sudo serviceisc-dhcp-server start
Isc-dhcp-server start/running, process28867
Looks like the launch was successful from the log look at the process list confirm
sailing@ubuntu:~$ PS aux | grep DHCP
116 1226 0.0 0.0 3352 880? S Sep22 0:00/usr/sbin/dnsmasq-ulibvirt-dnsmasq--strict-order--bind-interfaces--pid-file=/var/run/libvirt/network /default.pid--conf-file=--except-interfacelo--listen-address 192.168.122.1--dhcp-range 192.168.122.2,192.168.122.254--dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases--dhcp-lease-max=253-- Dhcp-no-override
Sailing 28952 0.0 0.0 4388 828 pts/1 s+ 07:40 0:00 grep--color=autodhcp
No, no, there's no anomaly log. Why? The original related log is recorded in the L/var/log/syslog
sailing@ubuntu:~$ Tail/var/log/syslog
Sep23 07:43:35 Ubuntu dhcpd:no Subnet declaration for eth0 (192.168.2.1).
Sep 07:43:35 Ubuntu dhcpd: * * ignoringrequests on eth0. If not what
Sep 07:43:35 ubuntu Dhcpd:you want, please write a subnet declaration
Sep 07:43:35 ubuntu dhcpd:in your dhcpd.conf file for the Networksegment
Sep 07:43:35 ubuntu dhcpd:to which interface eth0 is attached. **
Sep 07:43:35 ubuntu dhcpd:
Sep 07:43:35 ubuntu dhcpd:
Sep 07:43:35 Ubuntu dhcpd:notconfigured to listen in any interfaces!
Sep23 07:43:35 ubuntu kernel: [38761.728278] init:isc-dhcp-server main process (29266) terminated with status 1
Sep23 07:43:35 ubuntu kernel: [38761.728307] Init:isc-dhcp-server respawning, toofast
So that's it. DHCPD started and then quit why said no subnet ah I was in the/etc/dhcp3/dhcpd.conf configured AH is not the use of/etc/dhcp3/dhcpd.conf. Found that there is also a default/etc/dhcp/dhcpd.conf it does not define the subnet domain tested to find that the correct configuration file is/etc/dhcp/dhcpd.conf
If/etc/dhcp/dhcpd.conf has any syntax error to start DHCP can be found in the/var/log/syslog related log debugging until no error and the process list has DHCP services
sailing@ubuntu:~$ Tail/var/log/syslog
。。。。。。。。
Sep 07:53:54 ubuntu dhcpd:wrote 0leases to leases file.
sailing@ubuntu:~$ PS aux | grep DHCP
116 1226 0.0 0.0 3352 880? S Sep22 0:00/usr/sbin/dnsmasq-ulibvirt-dnsmasq--strict-order--bind-interfaces--pid-file=/var/run/libvirt/network /default.pid--conf-file=--except-interfacelo--listen-address 192.168.122.1--dhcp-range 192.168.122.2,192.168.122.254--dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases--dhcp-lease-max=253-- Dhcp-no-override
DHCPD 29381 0.0 0.2 4944 2840? Ss 07:53 0:00/usr/sbin/dhcpd-f-q-4-pf/run/dhcp-server/dhcpd.pid-cf/etc/dhcp/dhcpd.conf eth0
Sailing 29394 0.0 0.0 4388 832 pts/1 s+ 07:55 0:00 grep--color=autodhcp
Now that the DHCP server is up and running, the next step is to test
Linux virtual machine test
A very good feature of virtual machines is that direct copy can install a new virtual machine
such as the IP configuration of the DHCP server also uses the graphical interface configuration
Close and then open to see the assigned IP
Ping tests at both server and client pass
sailing@ubuntu:~$ Ping 192.168.2.10
PING 192.168.2.10 (192.168.2.10) bytes of data.
Bytes from 192.168.2.10:icmp_req=1ttl=64 time=0.820 ms
。
^c
---192.168.2.10 ping statistics---
3 Packets transmitted, 3 received, 0%packet loss, time 2001ms
RTT Min/avg/max/mdev =0.498/0.608/0.820/0.149 ms
windowshost Host Test
Host hosts have two network cards a wireless network card a wired network card needs to use the wired network card and Linux Server a communication
Configure the wired network card represented by the local connection as DHCP mode but cannot be assigned to IP Ah, trying to configure static IP or not. The current host host's wired network card does not have a down state with the Linux Server a connection port. After docking still cannot be allocated to IP even if configured as static IP can not ping the DHCP server
What exactly does the host use to bridge the virtual machine that provides the DHCP service? The virtual machine configuration option is auto, no match.
Search on the Internet finally found the place to be able to set VMware's editing Options Virtual network editor >vmnet0 which bridge to the network card select the Wired network card
Finally DHCP is assigned to IP after the change but host can ping the virtual machine and the virtual machine does not ping the host does not make sense AH host can ping the virtual machine to explain the link is the AH is the virtual machine inside the routing problem. The check repeatedly doesn't feel like a routing problem because the virtual machine where the DHCP server resides and another virtual machine can ping each other. It is the ping message on host that is blocked like a firewall.
On Win7, the following settings: Control Panel system and security Windows Firewall Advanced settings Inbound rules file and Printer Sharing (ICMPv4 echo request), select Yes, enable
At this point, host and virtual machine finally ping through
External linux Server a test
Configure external Server A to DHCP mode and the host network cable that hosts the virtual machine directly to the Linux server A is well distributed to the IP and the two can ping each other
The communication link between Linux Server A and virtual Server B is completed
How to automatically know the IP assigned to Linux Server A
The assigned IP address is recorded in the/var/lib/dhcp/dhcpd.leases
Linux Server A will be updated frequently so that each assigned address will change
Attempt to configure an allocated IP address pool to 2 host consuming a frequently changing Linux server A takes one but fails
The IP address assigned before Linux Server A cannot be automatically freed during the lease period so that new Linux Server A cannot be assigned to an available IP
Careful analysis found that each assigned out of the IP has attribute client-hostname, this property to find the last lease item can get the corresponding IP
sailing@ubuntu:~$ cat/var/lib/dhcp/dhcpd.leases
# The format of this file is documented inthe Dhcpd.leases (5) manual page.
# This lease file is written BYISC-DHCP-4.1-ESV-R4
Lease 192.168.2.10 {
Starts 1 2013/09/23 15:11:01;
Ends 1 2013/09/23 15:21:01;
TSTP 1 2013/09/23 15:21:01;
CLTT 1 2013/09/23 15:11:01;
binding state active;
Next binding State free;
Hardware Ethernet 00:0c:29:c1:95:39;
Client-hostname "Ubuntu";
}
Server-duid "\000\001\000\001\031\322}\273\000\014) \332d\207";
Lease 192.168.2.10 {
Starts 1 2013/09/23 15:14:44;
Ends 1 2013/09/23 15:24:44;
CLTT 1 2013/09/23 15:14:44;
binding state active;
Next binding State free;
Hardware Ethernet 00:0c:29:c1:95:39;
Client-hostname "Ubuntu";
}
sailing@ubuntu:~$ ll/var/lib/dhcp/dhcpd.leases
-rw-r--r--1 dhcpd dhcpd 2554 Sep 08:45/var/lib/dhcp/dhcpd.leases
sailing@ubuntu:~$ Tail/var/log/syslog
Sep 10:31:13 ubuntu dhcpd:dhcprequestfor 192.168.2.10 from 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:31:13 ubuntu dhcpd:dhcpack on192.168.2.10 to 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:36:00 ubuntu dhcpd:dhcprequestfor 192.168.2.10 from 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:36:00 ubuntu dhcpd:dhcpack on192.168.2.10 to 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:40:08 ubuntu dhcpd:dhcprequestfor 192.168.2.10 from 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:40:08 ubuntu dhcpd:dhcpack on192.168.2.10 to 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:44:32 ubuntu dhcpd:dhcprequestfor 192.168.2.10 from 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:44:32 ubuntu dhcpd:dhcpack on192.168.2.10 to 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:48:58 ubuntu dhcpd:dhcprequestfor 192.168.2.10 from 00:0c:29:c1:95:39 (Ubuntu) via eth0
Sep 10:48:58 ubuntu dhcpd:dhcpack on192.168.2.10 to 00:0c:29:c1:95:39 (Ubuntu) via eth0