Jump to content

About This Club

This is a huge topic. This club is for anyone who enjoys programming or is looking for some code to copy to use for themselves.
  1. What's new in this club
  2. Version 1.0.0

    0 downloads

    pretty nice presentation in effort to better understand ansible
  3. Lambda functions are intended as a shorthand for defining functions that can come in handy to write concise code without wasting multiple lines defining a function. They are also known as anonymous functions, since they do not have a name unless assigned one. Why are lambda functions useful? Okay, so lambda functions are quirky and somewhat cool, but what’s the point of having them? Can’t we just use regular function definitions all the time? Technically, yes — but that doesn’t mean lambda functions are useless. Their primary benefit lies in their simplicity and convenience. When writing code — especially Python code — readability is of the utmost importance. Lambda functions are a brilliant way to make code more readable and concise.
  4. I have played aroune with Sublime Text, Atom, Eclipse IDE and I have been recently told to try Visual Studio Code. I have been trying not to since it has the name Microsoft in the name but I'll be honest, its pretty nice. I learned that this is very much like Atom or Sublime. Atom slows down with large projects where VS Code doesn't slow down at all. Some extensions I installed to help me out include: Rest Client – quick and easy testing of REST API (not as feature rich as POSTMAN, but good for quick testing) Python Prettier – For making JSON look indented (having issues installing based on a corrupt zip file) YAML – for ansible ANSIBLE Indented block highlighting – for highlighting the JSON block you are in (useful for F5 AS3) Project Manager – for easily switching between multiple projects There are so many videos to help you get going with VS Code.
  5. Here is the script backup.sh #!/bin/bash #Created by Dennis Hosang 20130812 #day of week variable DOW=$(date +%A) #username variable USERNAME="netadm1n" #hostname variable HOST=$(hostname|cut -f1 -d.) #backup /home sudo rsync --delete -azvv --exclude-from '/backups/home_exclude' --log-file=/backups/${DOW}/rsync_log /home/ 10.6.56.244:/backups/${HOST}/${DOW}/home/ #backup /boot sudo rsync --delete -azvv --log-file=/backups/${DOW}/rsync_log /boot/ 10.6.56.244:/backups/${HOST}/${DOW}/boot/ #empty trash rm -rf /home/${USERNAME}/.local/share/Trash/Files/* #backup /root sudo rsync -avz --delete --exclude-from '/backups/root_exclude' --log-file=/backups/${DOW}/rsync_log / 10.6.56.244:/backups/${HOST}/${DOW}/root/ #backup /home #sudo rsync --delete -azvv --exclude-from '/backups/home_exclude' --log-file=/backups/${DOW}/rsync_log /home/ 10.6.56.244:/backups/${HOST}/${DOW}/home/ Just added it to my crontab # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command # 37 14 * * * root sh /backups/backup.sh
  6. This script shows what you have scheduled via cron #!/bin/bash # System-wide crontab file and cron job directory. Change these for your system. CRONTAB='/etc/crontab' CRONDIR='/etc/cron.d' # Single tab character. Annoyingly necessary. tab=$(echo -en "\t") # Given a stream of crontab lines, exclude non-cron job lines, replace # whitespace characters with a single space, and remove any spaces from the # beginning of each line. function clean_cron_lines() { while read line ; do echo "${line}" | egrep --invert-match '^($|\s*#|\s*[[:alnum:]_]+=)' | sed --regexp-extended "s/\s+/ /g" | sed --regexp-extended "s/^ //" done; } # Given a stream of cleaned crontab lines, echo any that don't include the # run-parts command, and for those that do, show each job file in the run-parts # directory as if it were scheduled explicitly. function lookup_run_parts() { while read line ; do match=$(echo "${line}" | egrep -o 'run-parts (-{1,2}\S+ )*\S+') if [[ -z "${match}" ]] ; then echo "${line}" else cron_fields=$(echo "${line}" | cut -f1-6 -d' ') cron_job_dir=$(echo "${match}" | awk '{print $NF}') if [[ -d "${cron_job_dir}" ]] ; then for cron_job_file in "${cron_job_dir}"/* ; do # */ [[ -f "${cron_job_file}" ]] && echo "${cron_fields} ${cron_job_file}" done fi fi done; } # Temporary file for crontab lines. temp=$(mktemp) || exit 1 # Add all of the jobs from the system-wide crontab file. cat "${CRONTAB}" | clean_cron_lines | lookup_run_parts >"${temp}" # Add all of the jobs from the system-wide cron directory. cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}" # */ # Add each user's crontab (if it exists). Insert the user's name between the # five time fields and the command. while read user ; do crontab -l -u "${user}" 2>/dev/null | clean_cron_lines | sed --regexp-extended "s/^((\S+ +){5})(.+)$/\1${user} \3/" >>"${temp}" done < <(cut --fields=1 --delimiter=: /etc/passwd) # Output the collected crontab lines. Replace the single spaces between the # fields with tab characters, sort the lines by hour and minute, insert the # header line, and format the results as a table. cat "${temp}" | sed --regexp-extended "s/^(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(.*)$/\1\t\2\t\3\t\4\t\5\t\6\t\7/" | sort --numeric-sort --field-separator="${tab}" --key=2,1 | sed "1i\mi\th\td\tm\tw\tuser\tcommand" | column -s"${tab}" -t rm --force "${temp}"
  7. '; echo ' '; echo ' DNS Dig Test'; echo '
  8. PRE-REQ Place these in the footer of your page above the closing tag. jQuery http://code.jquery.com/jquery-latest.min.js jQuery Form Plugin //cdnjs.cloudflare.com/ajax/libs/jquery.form/3.32/jquery.form.js jQuery Validate //cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js THE CODE The form is simple HTML with a few input fields, a spam protection field, and a hidden confirmation message triggered via the jQuery Validate plugin AJAX success call. Name* Email* Phone Message* Name the small house pet that says "meow"* Your message was sent succssfully! I will be in touch as soon as I can. Something went wrong, try refreshing and submitting the form again. Here is some base CSS for the form, you will need to make adjustments to match your sites styling. form { margin:0 } form label { margin-bottom:.2em; font-size:1.3rem; line-height:1.3rem; font-size:13px; line-height:13px; color:#e6e6e1; text-shadow:0px -1px #202020 } form label.error { margin-bottom:1em; font-size:1.2rem; line-height:1.2rem; font-size:12px; line-height:12px; color:#c0392b } form input[type=text], form textarea { margin-bottom:1.25em; font-family:"Inconsolata", sans-serif; font-size:1.4rem; line-height:1.4rem; font-size:14px; line-height:14px; box-shadow:none; -moz-box-shadow:none; -webkit-box-shadow:none; background:#e6e6e6; border:1px solid #191919; -moz-border-radius:0.2em; -webkit-border-radius:0.2em; border-radius:0.2em } form input[type=text]:focus, form textarea:focus { border-color:#191919; box-shadow:none; -moz-box-shadow:none; -webkit-box-shadow:none } form input[type=text], form textarea { background:#fff } form input[type=text].error, form textarea.error { background:#e6e6e6; border-color:#c0392b } fieldset { border:0px; margin:0; padding:0 } .required { color:#e9266d } #success, #error { display:none } #success span, #erro span { display:block; position:absolute; top:0; width:100% } #success span p, #error span p { margin-top:6em } #success span p { color:#9bd32d; } #error span p { color:#c0392b; } The PHP needed is just a basic form mail script. Change mail@yourdomain.com to the email the form should send to, and You have a message sent from your site to whatever subject you would like the email to have. This script will reside on your server as process.php and not get inserted into your page. <?php $to = "mail@yourdomain.com"; $from = $_REQUEST['email']; $name = $_REQUEST['name']; $headers = "From: $from"; $subject = "You have a message sent from your site"; $fields = array(); $fields{"name"} = "name"; $fields{"email"} = "email"; $fields{"phone"} = "phone"; $fields{"message"} = "message"; $body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $send = mail($to, $subject, $body, $headers); ?> Using jQuery Validate we can assure that the form is filled out properly before being sent in, and also prevent a bit of spam using a custom function. jQuery.validator.addMethod('answercheck', function (value, element) { return this.optional(element) || /^\bcat\b$/.test(value); }, "type the correct answer -_-"); // validate contact form $(function() { $('#contact').validate({ rules: { name: { required: true, minlength: 2 }, email: { required: true, email: true }, message: { required: true }, answer: { required: true, answercheck: true } }, messages: { name: { required: "come on, you have a name don't you?", minlength: "your name must consist of at least 2 characters" }, email: { required: "no email, no message" }, message: { required: "um...yea, you have to write something to send this form.", minlength: "thats all? really?" }, answer: { required: "sorry, wrong answer!" } }, submitHandler: function(form) { $(form).ajaxSubmit({ type:"POST", data: $(form).serialize(), url:"process.php", success: function() { $('#contact :input').attr('disabled', 'disabled'); $('#contact').fadeTo( "slow", 0.15, function() { $(this).find(':input').attr('disabled', 'disabled'); $(this).find('label').css('cursor','default'); $('#success').fadeIn(); }); }, error: function() { $('#contact').fadeTo( "slow", 0.15, function() { $('#error').fadeIn(); }); } }); } }); }); REFERENCE http://ajtroxell.com/build-a-simple-php-jquery-and-ajax-powered-contact-form/ /applications/core/interface/file/attachment.php?id=467">PHP_jQuery_and_AJAX_Powered_Contact_Form.zip
  9. Here is a great Perl script thanks to Zed Pobre that lists all groups and what members are in each. #!/usr/bin/perl -T # # Lists members of all groups, or optionally just the group # specified on the command line # # Copyright © 2010-2013 by Zed Pobre (zed@debian.org or zed@resonant.org) # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # use strict; use warnings; $ENV{"PATH"} = "/usr/bin:/bin"; my $wantedgroup = shift; my %groupmembers; my $usertext = `getent passwd`; my @users = $usertext =~ /^(+):/gm; foreach my $userid (@users) { my $usergrouptext = `id -Gn $userid`; my @grouplist = split(' ',$usergrouptext); foreach my $group (@grouplist) { $groupmembers{$group}->{$userid} = 1; } } if($wantedgroup) { print_group_members($wantedgroup); } else { foreach my $group (sort keys %groupmembers) { print "Group ",$group," has the following members:n"; print_group_members($group); print "n"; } } sub print_group_members { my ($group) = @_; return unless $group; foreach my $member (sort keys %{$groupmembers{$group}}) { print $member,"n"; } }
  10. You can see if you have perl installed (you should since it comes on every linux distro I used) by typing perl -v Perl Script #!/usr/bin/perl use strict; use warnings; use Filesys::DiskSpace; my $dir = "/home"; my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir; my $df_free = (($avail) / ($avail+$used)) * 100.0; my $out = sprintf("Disk space on $dir: %0.2fn",$df_free); print $out;
  11. Many times we need to update the serial number on multiple zone files and here is a script that we created for that purpose. It's written in korn shell (ksh).. you can check what shell your default is by typing echo $SHELL ** Downloadable Only as a Subscribed User ** snupdate.ksh.txt snupdate.ksh.txt
  12. If you have ever had to manage a bind server you'll understand when I say that the named can get overwhelming with entries. I created this awk script to go through the named.conf file and export the data into a csv file that you can easily sort and view easily. ** Downloadable Only as a Subscribed User ** name2csv.txt name2csv.txt
  13.  
×
×
  • Create New...