aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: a45f1fe55254263e40b7e3cf876e572e06b0c5f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Installation

### Module Dependencies

* argparse (if Python version is <= 2.6)
* netattr
* netifaces

#### Note
As long as your `setuptools` installation is reasonably up to date, `setup.py` will take care of installing any required dependencies.

### 1. Clone the cidrchk git repository

```
git clone https://github.com/jhunkeler/cidrchk.git
```

### 2. Install the script

```
cd cidrchk
sudo python setup.py install
```

If you are unable to install `cidrchk` as root, please use the following notation instead:

```
python setup.py install --user
```

# What is cidrchk?

`cidrchk` is a simple Python script with only one purpose: To inform a user whether or not their computer is connected to a particular network.

# How do I use it?

Let's assume you depend on autofs to automatically mount NFS directories at-will.

However, when you are off-site or not connected to your institution's VPN, you quickly realize attempting to access these data areas causes significant delays (i.e. a *five minute* default timeout)

Consider the following **.cshrc** example:

```
setenv PATH ${HOME}/bin:${PATH}
setenv MYDATA /remote/data1
alias badidea "cd ${MYDATA}"
```

What happens if you attempt to execute your favorite alias?

```
$ badidea
[ no output, waiting for autofs to timeout ]
```

Whoops! Things didn't go as planned, so let's take a look at the same **.cshrc** example using `cidrchk`:

```
setenv PATH ${HOME}/bin:${PATH}
setenv MYDATA /remote/data1

set _OFFSITE = `cidrchk 10.0.0.0/20 66.55.32.0/20 >/dev/null`
setenv OFFSITE ${status}
unset _OFFSITE

alias badidea "cd ${MYDATA}"

if ( ${OFFSITE} ) then
    unalias badidea
endif
```

In the above example the following is *true*: 

If 10.0.0.0/20 represents your institution's VPN address space and 66.55.32.0/20 represents your company's local intranet, and your home IP was 192.168.1.101, cidrchk returned a non-zero value indicating your computer was "off-site".

Furthermore, if your computer's IP address was 66.55.45.10 (i.e. you are in your cubicle), cidrchk would return zero indicating your computer was "on-site".

## Other Possibilities

Issuing `-v` to cidrchk will echo the return value to the console, resulting in slightly cleaner code:

```
setenv OFFSITE `cidrchk -v 10.0.0.0/20 66.55.32.0/20`

if ( ${OFFSITE} ) then
    # do something clever to prevent personal hardship
endif
```

The downside to parsing a string rather than an integer is logical operators will not work as expected. `${OFFSITE} > "0"` is not the same as evaluating  `${OFFSITE} > 0`.

## What about BASH?

The required notation is nearly identical to TCSH:

```
_OFFSITE=$(cidrchk 10.0.0.0/20 66.55.32.0/20 >/dev/null)
export OFFSITE=$?

if (( ${OFFSITE} )); then
    #do something clever to prevent personal hardship
fi
```

# Options

```
usage: cidrchk [-h] [--ignore IGNORE] [--debug] [--verbose] cidr [cidr ...]

Detects whether or not any ethernet devices match to a defined CIDR range.

positional arguments:
  cidr                  IP range(s) to detect

optional arguments:
  -h, --help            show this help message and exit
  --ignore IGNORE, -i IGNORE
                        IP range(s) to ignore (Default: link-local and
                        localhost)
  --debug, -d
  --verbose, -v
```

# Bug Reporting

Submit bug reports via this project's issue tracker: https://github.com/jhunkeler/cidrchk/issues

Please remember to include your computer's operating system, the name of the shell you executed cidrchk from, the output of `cidrchk -d`, and any relevant code snippets you may have.