openDCIM is an free and open source solution for DataCenter Infrastructure Management. It is already used by a few organizations, and is quickly improving due to the efforts of its developers.
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 :)
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;
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:
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.
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).
Για να δημιουργήσετε μια αντίστροφη σειρά αριθμών με το 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 χρησιμοποιείται για να τυπώνει τον επόμενο αριθμό σε καινούρια γραμμή, αλλιώς εμφανίζονται συνεχόμενα.