Shell script detects IP address

Source: Internet
Author: User

Shell script:

A text file that contains some commands or declarations and conforms to a certain format

Format requirements: First line shebang mechanism

#!/bin/bash

#!/usr/bin/python

#!/usr/bin/perl

The purpose of the shell script is:

? Automation common Commands

? perform system administration and troubleshooting

? Create a simple application

? working with text or files

First step: Use a text editor to create a text file

? The first line must include the shell declaration sequence: #!

#!/bin/bash

? Add comments

Comments start with #

? Step two: Run the script

Give execute permission to specify the absolute or relative path of the script on the command line

Run the interpreter directly and run the script as a parameter of the interpreter program.

Next I write a script called hostping.sh to detect if an IP address is connected to the host

#/bin/bash

Read-p "Please input ipaddress!:" IP//Note Enter an IP address for the parameter

[[! $ip =~ ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]]]//Notes [[]] two brackets are the exact conditions used to support the decision of the IP address after the command of the extended expression, because we know that the IP address format is XXX.XXX.XXX.XXX The maximum can not exceed 255 so we match 0-99,100-199,200-249,250-255 the number of paragraphs, so that we can do not less than the IP address.

&& echo "Please enter your IP address!" && exit 20//If the IP address entered is not the correct format of the IP address, we will directly output enter a correct IP address and return a 20 value.

' Ping $ip-w1-c1 &>/dev/null ' && echo ' This IP address can accessed! ' | | echo "This IP address was not accessed!"

Apply IP This variable to detect whether the IP address is connected, if connected to the output of connected message, otherwise is not connected.

Shell script detects IP address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.