Let’s say that we have a Bash script that accepts a parameter that is supposed to be an IP. How do we verify that input, before proceeding with any further actions? Here’s what I do so far.
Breakdown of the conditions
First, check that exactly three dots exist in the string:
if [ `echo $IsIP | grep -o '\.' | wc -l` -ne 3 ]; then
echo "Parameter '$1' does not look like an IP Address.";
exit 1;
fi
Then, check that exactly four parts exist if the string is broken down by dots:
if [ `echo $IsIP | tr '.' ' ' | wc -w` -ne 4 ]; then
echo "Parameter '$1' does not look like an IP Address.";
exit 1;
fi
Then, check that all four parts are numeric:
for OCTET in `echo $IsIP | tr '.' ' '; do
if ! [[ $OCTET =~ ^[0-9]+$ ]]; then
echo "Parameter '$1' does not look like an IP Address.";
exit 1;
fi
done
Finally check that all four parts are in the range 0 to 255:
for OCTET in `echo $IsIP | tr '.' ' '; do
if [[ $OCTET -lt 0 || $OCTET -gt 255 ]]; then
echo "Parameter '$IsIP' does not look like in IP Address (octet '$OCTET' in not in range 0-255).";
fi
done
So the parameter passed to the script must fulfill the following four criteria:
- Contain exactly three dots.
- Contain exactly four parts, if broken down by dots.
- All four parts are numeric.
- All four parts are between 0 and 255.
A Bash function
Here’s the entire thing in the form of a Bash function, with an additional comment on what went wrong:
# Verify that the parameter passed is an IP Address:
function is_IP() {
if [ `echo $1 | grep -o '\.' | wc -l` -ne 3 ]; then
echo "Parameter '$1' does not look like an IP Address (does not contain 3 dots).";
exit 1;
elif [ `echo $1 | tr '.' ' ' | wc -w` -ne 4 ]; then
echo "Parameter '$1' does not look like an IP Address (does not contain 4 octets).";
exit 1;
else
for OCTET in `echo $1 | tr '.' ' '`; do
if ! [[ $OCTET =~ ^[0-9]+$ ]]; then
echo "Parameter '$1' does not look like in IP Address (octet '$OCTET' is not numeric).";
exit 1;
elif [[ $OCTET -lt 0 || $OCTET -gt 255 ]]; then
echo "Parameter '$1' does not look like in IP Address (octet '$OCTET' in not in range 0-255).";
exit 1;
fi
done
fi
return 0;
}
Example output of the function when saved as a standalone script:
marios@yovan ~ $ ./is_IP 1234
Parameter '1234' does not look like an IP Address (does not contain 3 dots).
marios@yovan ~ $ ./is_IP 1234...
Parameter '1234...' does not look like an IP Address (does not contain 4 octets).
marios@yovan ~ $ ./is_IP 1.2.3.A
Parameter '1.2.3.A' does not look like in IP Address (octet 'A' is not numeric).
marios@yovan ~ $ ./is_IP 1.2.3.300
Parameter '1.2.3.300' does not look like in IP Address (octet '300' in not in range 0-255).
marios@yovan ~ $ ./is_IP 1.2.3.4
marios@yovan ~ $
If someone has any better ways to check this, or improvements on the above, please step forward :)
Fork these scripts on GitHub.
Από 1η Ιανουαρίου 2013, ελευθερώνονται ως Κοινό Κτήμα (Public Domain) όλα τα έργα δημιουργών που πέθαναν το 1942.
Η σύντομη λίστα των Ελλήνων δημιουργών συμπεριλαμβάνει:
Ποιητές – Λογοτέχνες
Ζωγράφοι – Γλύπτες
Μουσικοί
I wanted to create a fairly big MySQL database (~30GB on disk) for tests. I created a “test“ database, and a “table2“ table, with a “content“ field with datatype TEXT. Here’s what I run from Bash:
marios@mysql-server:~$ time for i in `seq 1 500000`; \
do echo $i of 500000; \
mysql -u root -D test -e \
"INSERT INTO table2 (content) VALUES ('`cat /var/log/messages | tr -d \'`')"; \
done
This took a few hours to finish, while creating data at a rate of 100MBytes per minute on my test server. The size of the database was 30GB.
This did the job but it was too slow. The better way to do it, would be to run the above command for far less repetitions, say 100000, to create a smaller table, then copy that table in the same database as many times as required for the entire database to reach the desirable size, like:
mysql> create table2 like table;
insert into table2 select * from table;
You can run multiple instances of Thunderbird with different user profiles for each instance, by launching it from command line as:
[marios@bate ~]$ thunderbird -no-remote -P work &
[marios@bate ~]$ thunderbird -no-remote -P personal &
The -P option defines the profile name. If you want to create a new profile, use -P without a name.
NOTE: This article is a short version of Run multiple copies of Thunderbird at the same time from the Mozilla Knowledge Base.
Websites that use HTTP authentication, can use one of a couple of authentication methods, like Basic or Digest. When first opened, the website returns a 401 message, along with a string that contains the type of authentication and the Realm, and expects from the user to enter their credentials. Here’s how to login with Python to a website that uses Digest.
First, verify the authentication type and get the Realm with curl. The option -I only fetches the headers:
[marios@zindilis.net]$ curl -I http://example.com
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Digest realm="Example Realm", nonce="1a7278f234efe7894dfd823", algorithm=MD5, qop="auth"
The useful parts from the output above is that the HTTP Authentication method is Digest and that the realm is Example Realm.
Now the Python part:
import urllib2
URL = 'http://example.com'
Realm = 'Example Realm'
Username = 'marios'
Password = 'p@ssw0rd'
authhandler = urllib2.HTTPDigestAuthHandler()
authhandler.add_password(Realm, URL, Username, Password)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
page_content = urllib2.urlopen(URL)
The page_content variable now contains the contents of the webpage returned after the authentication, and can be read with something like for line in page_content or parsed as HTML/XML.
Upgrade from an earlier version to 1.19.0 breaks some installations, with error:
"Revision::fetchFromConds". Database returned error "1054:
Unknown column 'rev_sha1' in 'field list' (localhost)".
This has been reported as a bug, and fixed in the development branch. Temporary fixes: downgrade back to 1.18.3 (tar.gz) or upgrade to a snapshot of the master branch of 1.20alpha.
Cisco devices will commit a command that you typed in configuration mode when you hit Ctrl+Z to fall back to enable mode.
For example, while configuring an interface, if you type shutdown and then press Ctrl+Z to exit configuration mode, the shutdown command is executed. Note, that this does not happen in PacketTracer, but it does happen in GNS3, and -of course- in real devices.
Ένας παλιός υπολογιστής, με παρωχημένο υλικό και ταπεινές δυνατότητες, μπορεί κάλλιστα να εξυπηρετήσει διάφορους χρήσιμους σκοπούς, αν έχει εγκατεστημένη μια σωστά ρυθμισμένη διανομή Linux. Αυτός ο οδηγός περιγράφει πώς μπορείτε να ρυθμίσετε ένα σύστημα με CentOS 6 ώστε να λειτουργεί ως δρομολογητής δικτύου (router).
Διαβάστε το άρθρο…
Δύο μικρά tips:
- Για να δημιουργήσετε μια αντίστροφη σειρά αριθμών με το
seq, από έναν μεγαλύτερο σε έναν μικρότερο ακέραιο, χρησιμοποιήστε το ως seq ΜΕΓΑΛΟΣ ΒΗΜΑ ΜΙΚΡΟΣ, για παράδειγμα:
marios@yovan ~ $ seq 10 -1 1
10
9
8
7
6
5
4
3
2
1
- Για να μορφοποιήσετε ακέραιους με με διαφορετικό πλήθος ψηφίων με μηδενικά ώστε να εμφανίζονται με το ίδιο πλάτος, εκτυπώστε τους με την
printf, συντάσσοντάς την ως printf "%0ΠΛΑΤΟΣd" ΑΚΕΡΑΙΟΣ. Για παράδειγμα:
printf "%04d" 1
0001
Συνδυασμός των δύο:
marios@yovan ~ $ for i in `seq 10 -1 1`; do echo `printf "%04d" $i`; done
0010
0009
0008
0007
0006
0005
0004
0003
0002
0001
Το echo χρησιμοποιείται για να τυπώνει τον επόμενο αριθμό σε καινούρια γραμμή, αλλιώς εμφανίζονται συνεχόμενα.
Cisco switches will allow you to add a Vlan to one or more interfaces, even if the Vlan does not exist on the device. MAC addresses will then appear in the MAC database as expected, but traffic will not flow.