"
13 | sys.exit()
14 |
15 | # modified from http://stackoverflow.com/a/2979208
16 | def entropy(string):
17 | # get probability of chars in string
18 | prob = [ float(string.count(c)) / len(string) for c in dict.fromkeys(list(string)) ]
19 |
20 | # calculate the entropy
21 | entropy = - sum([ p * math.log(p) / math.log(2.0) for p in prob ])
22 |
23 | return entropy
24 |
25 | c_entropy = entropy(sys.argv[1])
26 | p_length = len(sys.argv[1])
27 | ttl_entropy = entropy(sys.argv[1]) * len(sys.argv[1])
28 |
29 | print 'passwd length: ', p_length
30 | print 'entropy/char: ', c_entropy
31 | print 'actual entropy: ', ttl_entropy, 'bits'
32 |
--------------------------------------------------------------------------------
/experiments/README.md:
--------------------------------------------------------------------------------
1 | # scriptlets: experiments
--------------------------------------------------------------------------------
/experiments/bash_manipulation.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # bash_manipulation.sh - learning bash expansion/manipulation works (bash 4+)
4 |
5 | # http://stackoverflow.com/a/5163260
6 | # https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html
7 | # $1, $2, $3, ... are the positional parameters.
8 | # "$@" is an array-like construct of all positional parameters, {$1,$2,$3,...}
9 | # "$*" is the IFS expansion of all positional parameters, $1 $2 $3 ....
10 | # $# is the number of positional parameters.
11 | # $- current options set for the shell.
12 | # $$ pid of the current shell (not subshell).
13 | # $_ most recent parameter (or abs path of the command to start curr shell)
14 | # $IFS is the (input) field separator.
15 | # $? is the most recent foreground pipeline exit status.
16 | # $! is the PID of the most recent background command.
17 | # $0 is the name of the shell or shell script.
18 |
19 | echo "STRING MANIPULATION"
20 | echo "--------------------------------------------------------"
21 | echo "Example 1: Convert entire string to uppercase:"
22 | oldvariable=mississippi
23 | newvariable=${oldvariable^^}
24 | echo "newvariable=\${oldvariable^^} = ${newvariable^^}"
25 | echo "--------------------------------------------------------"
26 |
27 | echo "Example 2: Convert only first character to uppercase:"
28 | oldvariable=mississippi
29 | newvariable=${oldvariable^}
30 | echo "newvariable=\${oldvariable^} = ${newvariable^}"
31 | echo "--------------------------------------------------------"
32 |
33 | echo "Example 3: Convert entire string to lowercase:"
34 | oldvariable=MISSISSIPPI
35 | newvariable=${oldvariable,,}
36 | echo "newvariable=\${oldvariable,,} = ${newvariable,,}"
37 | echo "--------------------------------------------------------"
38 |
39 | echo "Example 4: Convert only first character to lowercase:"
40 | oldvariable=MISSISSIPPI
41 | newvariable=${oldvariable,}
42 | echo "newvariable=\${oldvariable,} = ${newvariable,}"
43 | echo "--------------------------------------------------------"
44 |
45 | echo "Example 5: Convert specific characters to uppercase:"
46 | oldvariable=mississippi
47 | newvariable=${oldvariable^^[mi]}
48 | echo "newvariable=\${oldvariable^^[mi]} = ${newvariable}"
49 | echo "--------------------------------------------------------"
50 |
51 | echo "Example 6: Convert specific characters to lowercase:"
52 | oldvariable=MISSISSIPPI
53 | newvariable=${oldvariable,,[MI]}
54 | echo "newvariable=\${oldvariable,,[MI]} = ${newvariable}"
55 | echo "--------------------------------------------------------"
56 |
57 |
58 | echo "FILENAME/PATH MANIPULATION & PARAMETER EXPANSION"
59 | FILE=/usr/share/lib/secrets.tar.gz
60 |
61 | echo "Example 1: Path without first directory"
62 | echo "\${FILE#*/} = ${FILE#*/}"
63 | echo "--------------------------------------------------------"
64 |
65 | echo "Example 2: Filename with all directories removed"
66 | echo "\${FILE##*/} = ${FILE##*/}"
67 | echo "--------------------------------------------------------"
68 |
69 | echo "Example 3: Filename with all directories removed using basename"
70 | echo "\$(basename FILE) = $(basename $FILE)"
71 | echo "--------------------------------------------------------"
72 |
73 | echo "Example 4: Only filename extensions"
74 | echo "\${FILE#*.} = ${FILE#*.}"
75 | echo "--------------------------------------------------------"
76 |
77 | echo "Example 5: Only the *last* filename extension"
78 | echo "\${FILE##*.} = ${FILE##*.}"
79 | echo "--------------------------------------------------------"
80 |
81 | echo "Example 6: Path as directories, no filename"
82 | echo "\${FILE%/*} = ${FILE%/*}"
83 | echo "--------------------------------------------------------"
84 |
85 | echo "Example 7: Path as directories, no filename using dirname"
86 | echo "\$(dirname $FILE = $(dirname $FILE)"
87 | echo "--------------------------------------------------------"
88 |
89 | echo "Example 8: List only the first directory in the path"
90 | echo "\${FILE%%/*} = ${FILE%%/*}"
91 | echo "--------------------------------------------------------"
92 |
93 | echo "Example 9: Path with the last extension removed"
94 | echo "\${FILE%.*} = ${FILE%.*}"
95 | echo "--------------------------------------------------------"
96 |
97 | echo "Example 10: Path with all extensions removed"
98 | echo "\${FILE%%.*} = ${FILE%%.*}"
99 | echo "--------------------------------------------------------"
100 |
101 | #EOF
102 |
--------------------------------------------------------------------------------
/experiments/curl_grab_headers.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # curl_grab_headers.sh - grab specific headers, was to experiment to get http
4 | # status codes in bash for a script at one timee
5 |
6 | var=( content_type filename_effective ftp_entry_path http_code http_connect local_ip local_port num_connects num_redirects redirect_url remote_ip remote_port size_download size_header time_appconnect time_connect time_namelookup time_pretransfer time_redirect time_starttransfer time_total url_effective size_request size_upload speed_download speed_upload ssl_verify_result time_appconnect time_connect time_namelookup time_pretransfer time_redirect time_starttransfer time_total url_effective )
7 |
8 | # according to the man page (https://curl.haxx.se/docs/manpage.html) only one
9 | # variable can be used at a time; "If this option is used several times, the
10 | # last one will be used." which is inefficent, but this is just a test
11 | for i in "${var[@]}"
12 | do
13 | resp=$(curl -Iso /dev/null -w "%{$i}" https://www.google.com/robots.txt)
14 | printf "%-20s\t%s\n" "$i" "$resp"
15 | #sleep 1
16 | done
17 |
18 | #EOF
19 |
--------------------------------------------------------------------------------
/experiments/json_parse_with_python.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # json_parse_with_python - parse json output with python in a shell script
4 |
5 | # example output
6 | #{
7 | # "ip": "166.70.163.62",
8 | # "hostname": "166-70-163-62.xmission.com",
9 | # "city": "Midvale",
10 | # "region": "Utah",
11 | # "country": "US",
12 | # "loc": "40.6092,-111.8819",
13 | # "org": "AS6315 Xmission, L.C.",
14 | # "postal": "84047"
15 | #}
16 |
17 | # grab ip info in json format
18 | headers=$(curl -s http://ipinfo.io)
19 |
20 | # fields also contain 'postal', but don't care about it
21 | fields=(ip hostname city region country loc org)
22 |
23 | for i in ${fields[@]}
24 | do
25 | printf "%-20s %s\n" "$i" "$(echo $headers | \
26 | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["'$i'"]';)"
27 | done
28 |
29 | #EOF
30 |
--------------------------------------------------------------------------------
/experiments/processes.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # processes.sh - manipulation of linux processes
4 |
5 | echo "PID = $$"
6 |
7 | pid=$$
8 | if [ -z $pid ]; then
9 | read -p "PID: " pid
10 | fi
11 |
12 | ps -p ${pid:-$$} -o ppid=
13 |
14 | #EOF
15 |
--------------------------------------------------------------------------------
/experiments/progress_example.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # progress_example.sh - feedback of progress while waiting for work to complete
4 |
5 | pidile=/var/run/appname/appname.pid
6 | if [ ! -e $pidfile ]; then
7 | touch $pidfile
8 | fi
9 |
10 | CNT=0
11 |
12 | # watch and wait for the process to terminate
13 | echo -n "Waiting for appname to close cleanly"
14 | while [ -e $pidfile ];
15 | do
16 | echo -n "."
17 | sleep 1
18 |
19 | let CNT=CNT+1
20 |
21 | # process has not terminated in 3 minutes, kill it
22 | if [ $CNT -gt 180 ]; then
23 | rm -f $pidfile
24 | #kill -9 appname
25 | echo "termination timeout exceeded, killing appname"
26 | exit 1
27 | fi
28 | done
29 | echo "the appname has been terminated"
30 |
31 | #EOF
32 |
--------------------------------------------------------------------------------
/experiments/randomize_hostname.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # randomize_hostname.sh - randomize hostname
4 |
5 | # author : Chad Mayfield (chad@chd.my)
6 | # license : gplv3
7 |
8 | # NOTE: This is ***totally untested***, don't use this yet!
9 |
10 | if [ $UID -ne 0 ]; then
11 | echo "ERROR: You must run this as root to change the hostname!"
12 | exit 1
13 | fi
14 |
15 | if [ "$1" == "permanent"]; then
16 | perm=1
17 | fi
18 |
19 | # store our original hostname
20 | h=/etc/.original_hostname
21 | svc_file=/etc/systemd/system/hostname.service
22 |
23 | # our list of possible hostnames
24 | names=(lust gluttony greed sloth wrath envy pride prudence justice
25 | temperance courage faith hope charity)
26 |
27 | create_service() {
28 | cat << EOF > /etc/systemd/system/hostname.service
29 | [Unit]
30 | Description=Randomize hostname on boot
31 |
32 | [Service]
33 | Type=oneshot
34 | ExecStart=/bin/bash /usr/local/bin/randomize_hostname.sh permanent
35 |
36 | [Install]
37 | WantedBy=multi-user.target
38 | EOF
39 | }
40 |
41 | if [[ $OSTYPE =~ "darwin" ]]; then
42 | # get random element
43 | idx=$( jot -r 1 0 $((${#names[@]} - 1)) )
44 | sel=${names[idx]}
45 |
46 | # find old hostname (or hostname/uname -a)
47 | orig_hostname=$(sysctl kern.hostname | awk '{print $2}')
48 | if [ -e $h ]; then
49 | if [ $(cat $h) != "$orig_hostname" ]; then
50 | echo "Storing original hostname..."
51 | echo "$orig_hostname" > $h
52 | fi
53 | fi
54 |
55 | if [ $perm -eq 1 ]; then
56 | # for permanent hostname change like this:
57 | hostname -s $sel
58 | else
59 | # temporarily change hostname (or hostname $sel)
60 | echo "scutil –-set HostName $sel"
61 | fi
62 |
63 | if [ $(hostname) == "$sel" ]; then
64 | echo "Successfully changed hostname to: $sel"
65 |
66 | if [ $perm -eq 1 ]; then
67 | echo "Please reboot this machine to complete the change."
68 | fi
69 | else
70 | echo "ERROR: Unable to change hostname! Please try again."
71 | exit 2
72 | fi
73 |
74 | elif [[ $OSTYPE =~ "linux" ]]; then
75 | # get random element
76 | sel=${names[$RANDOM % ${#names[@]} ]}
77 |
78 | # old hostname (or hostname/uname -n/ cat /proc/sys/kernel/hostname)
79 | orig_hostname=$(sysctl kernel.hostname | awk '{print $3}')
80 | if [ -e $h ]; then
81 | if [ $(cat $h) != "$orig_hostname" ]; then
82 | echo "Storing original hostname..."
83 | echo "$orig_hostname" > $h
84 | fi
85 | fi
86 |
87 | if [ $perm -eq 1 ]; then
88 | # create systemd.service file /lib/systemd/system/hostname.service
89 | # should put system-wide custom services in /etc/systemd/system
90 | # or /etc/systemd/user or ~/.config/systemd/user for user mode.
91 | if [ ! -f $svc_file ]; then
92 | create_service_file
93 | #chown /etc/systemd/system/hostname.service
94 | #chmod /etc/systemd/system/hostname.service
95 | fi
96 |
97 | if [ -f /etc/redhat-release ]; then
98 | # Red Hat variant
99 | sed -i "s/^HOSTNAME=.*$/HOSTNAME=$sel/g" /etc/sysconfig/network
100 | elif [[ $(lsb_release -d) =~ [Debian|Ubuntu] ]]; then
101 | # Debian/Ubuntu variants
102 | echo $sel > /etc/hostname
103 | else
104 | echo "ERROR: Unknown distro!"
105 | fi
106 |
107 | # modify /etc/hosts
108 | #sed -i "s/$oldhostname/$sel/g"
109 | else
110 | # temporarily change hostname
111 | echo "hostname $sel"
112 | fi
113 |
114 | if [ $(hostname) == "$sel" ]; then
115 | echo "Successfully changed hostname to: $sel"
116 |
117 | if [ $perm -eq 1 ]; then
118 | :
119 | #echo "Please reboot this machine to complete the change."
120 | fi
121 | else
122 | echo "ERROR: Unable to change hostname! Please try again."
123 | exit 2
124 | fi
125 |
126 | else
127 | echo "ERROR: Unknown OS!"
128 | fi
129 |
130 | #EOF
131 |
--------------------------------------------------------------------------------
/experiments/redirect_logging.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # redirect_logging.sh - redirect logging example
4 |
5 | #xhost + &> /dev/null
6 |
7 | # logging routine.
8 | >/var/log/mylog.log
9 | chmod 666 /var/log/mylog.log
10 |
11 | # assign descriptor 3 as a duplicate STDOUT link, then out to logfile
12 | # good explanation: http://tldp.org/LDP/abs/html/io-redirection.html
13 | exec 3>&1
14 | exec 1>/var/log/my.log 2>&1
15 |
16 | eko () {
17 | # echo args to stdout and log file
18 | echo "$@" >&3
19 | echo "$@"
20 | }
21 |
22 | eko "Hello World!"
23 |
24 | # disable writing to logfile by group/other
25 | chmod 644 /var/log/mylog.log
26 |
27 | #EOF
28 |
--------------------------------------------------------------------------------
/experiments/uptime.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | # uptime.pl - an experiment in re-writing uptime for different os's
4 |
5 | use Data::Dumper qw(Dumper);
6 | #use Config;
7 | #print "$Config{'osname'}\n";
8 | # perl oses: http://alma.ch/perl/perloses.htm
9 |
10 | if ( $^O =~ "linux" ) {
11 | # example uptime from debian linux command line (`uptime`)
12 | # 15:35:50 up 20:01, 1 user, load average: 0.04, 0.02, 0.09
13 |
14 | open(P, '/proc/uptime');
15 | my $uptime = ;
16 | close(P);
17 | $uptime = (split(m/\s+/, $uptime))[0];
18 |
19 | } elsif ( $^O =~ "darwin") {
20 | # example uptime from macOS Sierra (`sysctl -n kern.boottime`)
21 | # { sec = 1491070408, usec = 756788 } Sat Apr 1 12:13:28 2017
22 | # or using the actual (`uptime`) utility
23 | # 15:54 up 23:16, 3 users, load averages: 1.24 1.41 1.47
24 |
25 | # get current time
26 | my @datetime = (split / /, `date`);
27 | chomp(@datetime);
28 | my $currtime = $datetime[4];
29 |
30 | # get up (time)
31 | my @uptime = (split / /, `sysctl -n kern.boottime`, 9);
32 | chomp(@uptime);
33 | my $hours = $uptime[6];
34 | my $boottime = $uptime[3];
35 |
36 | # TODO: get users
37 | # TODO: get load average
38 |
39 | # print it out together
40 | print "$currtime\n";
41 | printf "Last boot date: %s\n", $boottime;
42 |
43 | # printf "Last boot date: %s (%.2f hours up)\n", $boottime, $hours;
44 | # print Dumper \@datetime;
45 | print Dumper \@uptime;
46 |
47 | } else {
48 | print "ERROR: Unknown OS!\n";
49 | }
50 |
--------------------------------------------------------------------------------
/experiments/userinfo.youtah.php:
--------------------------------------------------------------------------------
1 |
2 |
3 | User Information
4 |
5 |
6 |
7 |
17 |
19 |
20 |
21 | User Information
22 |
23 | Collected via Perl
24 | Environment Variables in Perl
25 |
26 | Collected via PHP
27 |
28 | DATE/TIME STAMP: ".date('l, F d, Y @ H:i:s A T')."
";
46 | echo "YOUR IP ADDRESS: ".getip()."
";
47 | echo 'HTTP_ACCEPT '. $_SERVER["HTTP_ACCEPT"]."
";
48 | echo 'HTTP_ACCEPT_LANGUAGE '. $_SERVER["HTTP_ACCEPT_LANGUAGE"]."
";
49 | echo 'HTTP_CONNECTION '. $_SERVER["HTTP_CONNECTION"]."
";
50 | echo 'HTTP_COOKIE '. $_SERVER["HTTP_COOKIE"]."
";
51 | echo 'HTTP_HOST '. $_SERVER["HTTP_HOST"]."
";
52 | echo 'HTTP_KEEP_ALIVE '. $_SERVER["HTTP_KEEP_ALIVE"]."
";
53 | echo 'HTTP_USER_AGENT '. $_SERVER["HTTP_USER_AGENT"]."
";
54 | echo 'REMOTE_ADDR '. $_SERVER["REMOTE_ADDR"]."
";
55 | echo 'REMOTE_PORT '. $_SERVER["REMOTE_PORT"]."
";
56 | //echo 'SERVER_ADDR '. $_SERVER["SERVER_ADDR"]."
";
57 | //echo 'PHP_SELF '. $_SERVER["PHP_SELF"]."
";
58 | //echo "SERVER SCRIPT_FILENAME: ".$_SERVER["SCRIPT_FILENAME"]."
";
59 | echo "PHP_SELF: ".$_SERVER["PHP_SELF"]."
";
60 | echo "argv: ".$_SERVER["argv"]."
";
61 | echo "argc: ".$_SERVER["argc"]."
";
62 | echo "GATEWAY_INTERFACE: ".$_SERVER["GATEWAY_INTERFACE"]."
";
63 | echo "SERVER_NAME: ".$_SERVER["SERVER_NAME"]."
";
64 | echo "SERVER_SOFTWARE: ".$_SERVER["SERVER_SOFTWARE"]."
";
65 | echo "SERVER_PROTOCOL: ".$_SERVER["SERVER_PROTOCOL"]."
";
66 | echo "REQUEST_METHOD: ".$_SERVER["REQUEST_METHOD"]."
";
67 | echo "REQUEST_TIME: ".$_SERVER["REQUEST_TIME"]."
";
68 | echo "QUERY_STRING: ".$_SERVER["QUERY_STRING"]."
";
69 | //echo "DOCUMENT_ROOT: ".$_SERVER["DOCUMENT_ROOT"]."
";
70 | echo "HTTP_ACCEPT: ".$_SERVER["HTTP_ACCEPT"]."
";
71 | echo "HTTP_ACCEPT_CHARSET: ".$_SERVER["HTTP_ACCEPT_CHARSET"]."
";
72 | echo "HTTP_ACCEPT_ENCODING: ".$_SERVER["HTTP_ACCEPT_ENCODING"]."
";
73 | echo "HTTP_ACCEPT_LANGUAGE: ".$_SERVER["HTTP_ACCEPT_LANGUAGE"]."
";
74 | echo "HTTP_CONNECTION: ".$_SERVER["HTTP_CONNECTION"]."
";
75 | //echo "HTTP_HOST: ".$_SERVER["HTTP_HOST"]."
";
76 | echo "HTTP_REFERER: ".$_SERVER["HTTP_REFERER"]."
";
77 | echo "HTTP_USER_AGENT: ".$_SERVER["HTTP_USER_AGENT"]."
";
78 | //echo "HTTPS: ".$_SERVER["HTTPS"]."
";
79 | echo "REMOTE_ADDR: ".$_SERVER["REMOTE_ADDR"]."
";
80 | echo "REMOTE_HOST: ".$_SERVER["REMOTE_HOST"]."
";
81 | echo "REMOTE_PORT: ".$_SERVER["REMOTE_PORT"]."
";
82 | //echo "SCRIPT_FILENAME: ".$_SERVER["SCRIPT_FILENAME"]."
";
83 | //echo "SERVER_ADMIN: ".$_SERVER["SERVER_ADMIN"]."
";
84 | echo "SERVER_PORT: ".$_SERVER["SERVER_PORT"]."
";
85 | //echo "SERVER_SIGNATURE: ".$_SERVER["SERVER_SIGNATURE"]."
";
86 | echo "PATH_TRANSLATED: ".$_SERVER["PATH_TRANSLATED"]."
";
87 | echo "SCRIPT_NAME: ".$_SERVER["SCRIPT_NAME"]."
";
88 | echo "REQUEST_URI: ".$_SERVER["REQUEST_URI"]."
";
89 | echo "PHP_AUTH_DIGEST: ".$_SERVER["PHP_AUTH_DIGEST"]."
";
90 | echo "PHP_AUTH_USER: ".$_SERVER["PHP_AUTH_USER"]."
";
91 | echo "PHP_AUTH_PW: ".$_SERVER["PHP_AUTH_PW"]."
";
92 | echo "AUTH_TYPE: ".$_SERVER["AUTH_TYPE"]."
";
93 | echo "HOST BY ADDRESS: ".gethostbyaddr($_SERVER['REMOTE_ADDR']);
94 | ?>
95 | Go Home
96 |
97 |
98 |
99 | Collected via JavaScript
100 |
101 |
111 |
112 |
135 |
136 |
147 |
148 |
151 |
152 |
156 | Go Home
157 |
158 |
159 |
160 |
161 |
--------------------------------------------------------------------------------
/fixssh.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | # fixssh.py - fix bad ssh keys from server OVER-deployment, a quick hack and
4 | # useless re-engineering of 'ssh-keygen -R '
5 |
6 | # NOTE: Reinvent the wheel, yup I did it. I fequently deploy a lot of servers
7 | # which are DHCP/DNS'd so when the SSH keys regenerate I always used to get key
8 | # errors, so I have this gem.... before I discovered 'ssh-keygen -R '
9 |
10 | import sys, os
11 |
12 | def main():
13 | # print usage if no args are given
14 | sname = os.path.basename(sys.argv[0])
15 | if len(sys.argv) == 1:
16 | print "Usage: " + sname + " "
17 | sys.exit()
18 |
19 | # assign first arg as hostname
20 | for arg in sys.argv[1:]:
21 | h = arg
22 |
23 | # define files we'll work on
24 | knownhosts = "~/.ssh/known_hosts"
25 | removedhosts = "~/.ssh/removed_hosts"
26 |
27 | # open and remove h from knownhost
28 | f = open(knownhosts, 'r')
29 | lines = f.readlines()
30 | f.close()
31 | f = open(knownhosts, 'w')
32 | for line in lines:
33 | if line != h + "\n":
34 | f.write(line)
35 | f.close()
36 |
37 | # check if removed was successful
38 | if h in open(knownhosts).read():
39 | print "ERROR: Failed to removed "+ h + " from " + knownhosts
40 | sys.exit()
41 | else:
42 | print "SUCCESS: Removed " + h + " from " + knownhosts
43 |
44 | # write h to a backup file removedhosts
45 | with open(removedhosts, 'a+') as myfile:
46 | myfile.write(h + "\n")
47 |
48 | # check if addition was successful
49 | if h in open(removedhosts).read():
50 | print "SUCCESS: Added " + h + " to " + removedhosts
51 | else:
52 | print "ERROR: Failed to add " + h + " to " + removedhosts
53 |
54 | if __name__ == "__main__":
55 | main()
56 |
--------------------------------------------------------------------------------
/fixssh.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # fixssh.sh - fix bad ssh keys from server OVER-deployment, a quick hack and
4 | # useless re-engineering of 'ssh-keygen -R '
5 |
6 | # NOTE: Reinvent the wheel, yup I did it. I fequently deploy a lot of servers
7 | # which are DHCP/DNS'd so when the SSH keys regenerate I always used to get key
8 | # errors, so I have this gem.... before I discovered 'ssh-keygen -R '
9 |
10 | if [ $* -ne 1 ]; then
11 | echo "Usage: $0 "
12 | exit 1
13 | fi
14 |
15 | remove="$*"
16 | knownhosts="~/.ssh/known_hosts"
17 | removedhosts="~/.ssh/removed_hosts"
18 |
19 | if [ $(grep -c $remove $knownhosts) -ge 1 ]; then
20 | for server in $remove; do
21 | # copy the key to remove to $removedhosts
22 | cat $hostfile | grep $remove > $removedhosts
23 | echo "SUCCESS: Added $remove to $removedhosts"
24 |
25 | # remove the key ($remove)
26 | cat $hostfile | grep -v $server > ${knownhosts}.$$
27 | mv -fp ${knownhosts}.$$ $knownhosts
28 | #sed -i '/$remove/'d $knownhosts
29 | echo "SUCCESS: Removed $remove from $knownhosts"
30 | done
31 | else
32 | echo "ERROR: $remove not found in $knownhosts"
33 | fi
34 |
35 | #EOF
36 |
--------------------------------------------------------------------------------
/getaddrbyhost.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl -w
2 |
3 | # getaddrbyhost.pl - lookup a hosts ip by hostname
4 |
5 | use strict;
6 | use Socket;
7 | use Data::Dumper;
8 |
9 | if ( @ARGV != 1 ) {
10 | print "ERROR: You must supply a hostname!\n";
11 | exit;
12 | }
13 |
14 | my @address = inet_ntoa(scalar(gethostbyname($ARGV[0])));
15 |
16 | #print Dumper(\@address);
17 |
18 | my $count = 1;
19 | foreach my $n (@address) {
20 | printf("%-15s %s\n", "IPAddress".$count.":", $n);
21 | shift(@address);
22 | $count++;
23 | }
24 |
25 | #EOF
26 |
--------------------------------------------------------------------------------
/gethostbyaddr.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl -w
2 |
3 | # gethostnamebyaddr.pl - show hostname/alias based on ip address
4 |
5 | use strict;
6 | use Socket;
7 | use Data::Dumper;
8 |
9 | if ( @ARGV != 1 ) {
10 | print "ERROR: You must supply an ip address!\n";
11 | exit;
12 | }
13 |
14 | my $ip = $ARGV[0];
15 | my @i = gethostbyaddr(inet_aton($ip), AF_INET);
16 |
17 | # define var based on array and shift off stack
18 | my $hn = $i[0];
19 | shift(@i);
20 | my $alias = $i[0];
21 | shift(@i);
22 | my $addrtype = $i[0];
23 | shift(@i);
24 | my $len = $i[0];
25 | shift(@i);
26 |
27 | # print it all out
28 | printf("%-15s %s\n", "Hostname:", $hn);
29 | printf("%-15s %s\n", "Alias:", $alias);
30 | #printf("%-15s %s\n", "Type:", $addrtype);
31 | #printf("%-15s %s\n", "Length:", $len);
32 |
33 | #foreach (@i) {
34 | # print " = ", inet_ntoa($_), "\n";
35 | #}
36 |
37 | #EOF
38 |
--------------------------------------------------------------------------------
/github_short_url.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # github_short_url.sh - create a short github url
4 |
5 | # HOWTO: https://github.blog/2011-11-10-git-io-github-url-shortener/
6 |
7 | if [ $# -lt 1 ] || [ $# -gt 2 ]; then
8 | echo "ERROR: You must supply a github url to shorten"
9 | exit 1
10 | fi
11 |
12 | short=()
13 | url="$1"
14 | vanity="$2"
15 | shortener="https://git.io"
16 |
17 | if ! [[ "$url" =~ (github.(com|blog)|githubusercontent.com) ]]; then
18 | echo "ERROR: Only github.com URLs are allowed!"
19 | exit 1
20 | else
21 | if [ ! -z ${vanity+x} ]; then
22 | # vanity url was requested
23 | if [[ $OSTYPE =~ "darwin" ]]; then
24 | # work around for ancient version of bash on macOS
25 | while IFS= read -r line; do
26 | short+=( "$line" )
27 | done < <( curl -si "$shortener" -H "User-Agent: curl/7.58.0" -F "url=${url}" -F "code=${vanity}" | grep -E "(Status|Location): " )
28 | else
29 | # mapfile, only for bash v4.0+
30 | mapfile -t short < <( curl -i "$shortener" -H "User-Agent: curl/7.58.0" -F "url=${url}" -F "code=${vanity}" | grep -E "(Status|Location): " )
31 | fi
32 | else
33 | if [[ $OSTYPE =~ "darwin" ]]; then
34 | # work around for ancient version of bash on macOS
35 | while IFS= read -r line; do
36 | short+=( "$line" )
37 | done < <( curl -si "$shortener" -H "User-Agent= curl/7.58.0" -F "url=${url}" | grep -E "(Status|Location): " )
38 | else
39 | # mapfile, only for bash v4.0+
40 | mapfile -t short < <( curl -H "User-Agent= curl/7.58.0" -i "$shortener" -F "url=${url}" | grep -E "(Status|Location): " )
41 | fi
42 | fi
43 |
44 | if [[ ${short[0]} =~ "201" ]]; then
45 | echo "Link created: $(echo "${short[1]}" | awk '{print $2}')"
46 | else
47 | # echo "ERROR: Link creation failed! Code $(echo ${short[0]} | sed 's|Status: ||g')"
48 | echo "ERROR: Link creation failed! Code ${short[0]//Status: /}"
49 | fi
50 | fi
51 |
52 | #EOF
--------------------------------------------------------------------------------
/launch_tmux.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # launch_tmux.sh - launch my tmux dev env on different machines
4 |
5 | # author : Chad Mayfield (chad@chd.my)
6 | # license : gplv3
7 | # date : 02/12/2019
8 |
9 | # ADDITIONAL INFO:
10 |
11 | # INFO: to identify panes easily;
12 | # Ctrl+B + Q (within tmux)
13 | # --- or ---
14 | # tmux display -pt "${TMUX_PANE:?}" '#{pane_index}'
15 | # view panes: #{session_name}:#{window_index}.#{pane_index}
16 |
17 | if [[ $OSTYPE =~ "linux" ]]; then
18 | # get current IP address, to make sure we're on the correct network
19 | IP="$(ip -br -4 addr | grep UP | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")"
20 |
21 | # check hostnames and apply create env for each machine
22 | if [[ "$(hostname)" =~ "phobos" ]] && [[ "$IP" =~ "73.196" ]]; then
23 | # start a new 'main' session detached
24 | tmux new-session -s "main" -d -n "dev1"
25 |
26 | # set the status bar for this session
27 | #tmux set status-bg default
28 | #tmux set status-fg white
29 | #tmux set status-left '#[fg=green]#S'
30 | tmux set status-right '#[fg=red,bold]w#{window_index}.p#{pane_index} #[fg=default,nobold](#(whoami)@#h) %H:%M:%S %d-%b-%y'
31 | #tmux set status-right '#{session_name}:#{window_index}.#{pane_index} (#(ehoami)@#h) %H:%M:%S %d-%b-%y'
32 | #tmux set status-left-length 20
33 | tmux set status-right-length 80
34 |
35 | ######## create a new window: dev1 (dev1 environment)
36 | #tmux new-window -t main:0 -n "dev1"
37 | #tmux rename-window dev
38 |
39 | # split windows into panes
40 | tmux split-window -h
41 | tmux split-window -h
42 | #tmux select-layout even-horizontal
43 | tmux select-pane -t 1
44 | tmux split-window -v -p 50
45 | tmux select-pane -t 3
46 | tmux split-window -v -p 50
47 | tmux select-pane -t 3
48 | tmux split-window -v -p 20
49 |
50 | # run these commands in created panes
51 | tmux send-keys -t 0 C-z 'cd ~/Code/' Enter
52 | tmux send-keys -t 1 C-z 'ssh file' Enter
53 | tmux send-keys -t 2 C-z 'ssh file' Enter
54 | tmux send-keys -t 3 C-z 'top' Enter
55 | tmux clock-mode -t 4
56 | tmux send-keys -t 5 C-z 'ssh file' Enter
57 | tmux select-pane -t main:0
58 |
59 | ######## create a new window: dev2 (dev2 environment)
60 | tmux new-window -t main:1 -n "dev2"
61 |
62 | # split windows into panes
63 | tmux split-window -h
64 | tmux split-window -h
65 | #tmux select-layout even-horizontal
66 | tmux select-pane -t 1
67 | tmux split-window -v -p 50
68 | tmux select-pane -t 3
69 | tmux split-window -v -p 50
70 |
71 | # run these commands in created panes
72 | tmux send-keys -t 0 C-z 'cd ~/Code/ && ls' Enter
73 | tmux select-pane -t main:1
74 |
75 | ######## create a new window: k8s1 (work with k8s cluster)
76 | tmux new-window -t main:2 -n "k8s"
77 |
78 | # split windows into panes
79 | tmux split-window -h
80 | tmux select-layout even-horizontal
81 | tmux select-pane -t 0
82 | tmux split-window -v -p 50
83 | tmux select-pane -t 2
84 | tmux split-window -v -p 50
85 |
86 | ######## create a new window: admin (for sysadmin-y things)
87 | tmux new-window -t main:3 -n "admin"
88 |
89 | # split windows into panes
90 | tmux split-window -h
91 | tmux split-window -h
92 | tmux select-layout even-horizontal
93 | tmux select-pane -t 0
94 | tmux split-window -v -p 50
95 | tmux select-pane -t 2
96 | tmux split-window -v -p 50
97 | tmux select-pane -t 4
98 | tmux split-window -v -p 50
99 |
100 | # run these commands in created panes
101 | tmux send-keys -t 0 C-z 'top' Enter
102 | tmux send-keys -t 1 C-z 'last' Enter
103 | tmux send-keys -t 2 C-z 'who' Enter
104 | tmux send-keys -t 3 C-z 'ssh file' Enter
105 | tmux select-pane -t 0
106 |
107 | ######## create a new window: misc
108 | tmux new-window -t main:4 -n "misc"
109 |
110 | # split windows into panes
111 | tmux split-window -h
112 | tmux split-window -h
113 | tmux select-layout even-horizontal
114 | tmux select-pane -t 0
115 | tmux split-window -v -p 50
116 | tmux select-pane -t 2
117 | tmux split-window -v -p 50
118 | tmux select-pane -t 4
119 | tmux split-window -v -p 50
120 |
121 | # run these commands in created panes
122 | tmux send-keys -t 0 C-z 'echo command goes here' Enter
123 | tmux send-keys -t 1 C-z 'ssh file' Enter
124 | tmux select-pane -t 0
125 |
126 | # switch focus back to main window, pane 0
127 | tmux select-window -t 0
128 | tmux select-pane -t 0
129 |
130 | # attach to main session
131 | tmux -2 attach-session -t main
132 |
133 | # check hostnames and apply create env for each machine
134 | elif [[ "$(hostname)" =~ (deimos|ISFL) ]] && [[ "$IP" =~ "76.133" ]]; then
135 | # start a new 'main' session detached
136 | tmux new-session -s "main" -d -n "dev"
137 |
138 | # set the status bar for this session
139 | tmux set status-right '#[fg=red,bold]w#{window_index}.p#{pane_index} #[fg=default,nobold](#(whoami)@#h) %H:%M:%S %d-%b-%y'
140 | tmux set status-right-length 80
141 |
142 | ######## create a new window: dev (dev environment)
143 | # split windows into panes
144 | tmux split-window -h
145 | #tmux select-layout even-horizontal
146 | tmux select-pane -t 1
147 | tmux split-window -v -p 50
148 |
149 | # run these commands in created panes
150 | tmux send-keys -t 0 C-z 'cd ~/Code/' Enter
151 | tmux send-keys -t 1 C-z 'ssh file' Enter
152 | # dont forget to add user to sudoers like this;
153 | # username ALL=(ALL) NOPASSWD: /usr/local/bin/sysinfo.sh
154 | tmux send-keys -t 2 'if ! /usr/local/bin/sysinfo.sh; then curl -sSL https://git.io/fhQAQ | sudo bash; fi' Enter
155 | tmux select-pane -t main:0
156 |
157 | ######## create a new window: admin (for sysadmin-y things)
158 | tmux new-window -t main:1 -n "admin"
159 |
160 | # split windows into panes
161 | tmux split-window -h
162 | tmux select-layout even-horizontal
163 | tmux select-pane -t 0
164 | tmux split-window -v -p 50
165 | tmux select-pane -t 2
166 | tmux split-window -v -p 50
167 |
168 | # run these commands in created panes
169 | tmux send-keys -t 0 C-z 'ssh file' Enter
170 | tmux send-keys -t 1 C-z 'who' Enter
171 | tmux send-keys -t 2 C-z 'last' Enter
172 | tmux send-keys -t 3 'if ! /usr/local/bin/sysinfo.sh; then curl -sSL https://git.io/fhQAQ | sudo bash; fi' Enter
173 | tmux select-pane -t 0
174 |
175 | ######## create a new window: k8s1 (work with k8s cluster 1)
176 | tmux new-window -t main:2 -n "k8s1"
177 |
178 | # split windows into panes
179 | tmux split-window -h
180 | tmux select-layout even-horizontal
181 | tmux select-pane -t 0
182 | tmux split-window -v -p 50
183 | tmux select-pane -t 2
184 | tmux split-window -v -p 50
185 |
186 | # run these commands in created panes
187 | #tmux send-keys -t 0 C-z 'ssh k8s-node1' Enter
188 | #tmux send-keys -t 1 C-z 'ssh k8s-node2' Enter
189 | #tmux send-keys -t 2 C-z 'ssh k8s-node3' Enter
190 | #tmux send-keys -t 3 C-z 'ssh k8s-node4' Enter
191 | #tmux select-pane -t 0
192 |
193 | ######## create a new window: k8s2 (work with k8s cluster 2)
194 | tmux new-window -t main:3 -n "k8s2"
195 |
196 | # split windows into panes
197 | tmux split-window -h
198 | tmux select-layout even-horizontal
199 | tmux select-pane -t 0
200 | tmux split-window -v -p 50
201 | tmux select-pane -t 2
202 | tmux split-window -v -p 50
203 |
204 | # run these commands in created panes
205 | #tmux send-keys -t 0 C-z 'ssh n1' Enter
206 | #tmux send-keys -t 1 C-z 'ssh n2' Enter
207 | #tmux send-keys -t 2 C-z 'ssh n3' Enter
208 | #tmux send-keys -t 3 C-z 'ssh n4' Enter
209 | #tmux select-pane -t 0
210 |
211 | ######## create a new window: misc
212 | tmux new-window -t main:4 -n "misc"
213 |
214 | # split windows into panes
215 | tmux split-window -h
216 | tmux select-layout even-horizontal
217 | tmux select-pane -t 0
218 | tmux split-window -v -p 50
219 | tmux select-pane -t 2
220 | tmux split-window -v -p 50
221 |
222 | # run these commands in created panes
223 | tmux send-keys -t 0 C-z 'ssh file' Enter
224 | tmux select-pane -t 0
225 |
226 | # switch focus back to main window, pane 0
227 | tmux select-window -t 0
228 | tmux select-pane -t 0
229 |
230 | # attach to main session
231 | tmux -2 attach-session -t main
232 | else
233 | echo "ERROR: Not implemented (UNKNOWN HOST/NETWORK)!"
234 | exit 1
235 | fi
236 | elif [[ $OSTYPE =~ "darwin" ]]; then
237 |
238 | # grab internal IPv4 address and check it
239 | IP="$(ifconfig en0 | grep 'inet ' | awk '{print $2}')"
240 |
241 | if [[ "$(hostname -s)" =~ "MBP" ]] && [[ "$IP" =~ "7.10" ]]; then
242 | # start a new 'main' session detached
243 | tmux new-session -s "main" -d -n "dev"
244 |
245 | # set the status bar for this session
246 | #pmset -g batt | egrep "([0-9]+\%).*" -o | cut -f1 -d';'
247 | tmux set status-right '#[fg=red,bold]w#{window_index}/p#{pane_index} #[fg=default,nobold](#(whoami)@#h) %H:%M:%S %d-%b-%y'
248 | tmux set status-right-length 80
249 |
250 | ######## create a new window: dev1 (dev1 environment)
251 | # split windows into panes
252 | tmux split-window -h
253 | #tmux select-layout even-horizontal
254 | tmux select-pane -t 1
255 | tmux split-window -v -p 50
256 |
257 | # run these commands in created panes
258 | tmux send-keys -t 0 C-z 'cd ~/Code/' Enter
259 | tmux send-keys -t 0 'if ! /usr/local/bin/sysinfo.sh; then curl -sSL https://git.io/fhQAQ | bash; fi; ls' Enter
260 | tmux send-keys -t 1 C-z 'ssh file' Enter
261 | tmux send-keys -t 1 "ls" C-m
262 | tmux send-keys -t 2 C-z 'ssh file' Enter
263 | tmux send-keys -t 2 "uptime" C-m
264 | tmux select-pane -t main:0
265 |
266 | ######## create a new window: plex (plex environment)
267 | tmux new-window -t main:1 -n "plex"
268 |
269 | # split windows into panes
270 | tmux split-window -h
271 | #tmux select-layout even-horizontal
272 | tmux select-pane -t 1
273 | tmux split-window -v -p 50
274 |
275 | # run these commands in created panes
276 | tmux send-keys -t 0 C-z 'ssh plex' Enter
277 | tmux send-keys -t 0 "cd /mnt/plex/ && ls -l" C-m
278 | tmux send-keys -t 1 C-z 'ssh plex' Enter
279 | tmux send-keys -t 1 "dfh" C-m
280 | tmux send-keys -t 2 C-z 'ssh plex' Enter
281 | tmux send-keys -t 2 "docker logs plex" C-m
282 | tmux select-pane -t main:1
283 |
284 | ######## create a new window: transmission (transmission environment)
285 | tmux new-window -t main:2 -n "transmission"
286 |
287 | # split windows into panes
288 | tmux split-window -h
289 | #tmux select-layout even-horizontal
290 | tmux select-pane -t 1
291 | tmux split-window -v -p 50
292 |
293 | # run these commands in created panes
294 | tmux send-keys -t 0 C-z 'ssh transmission' Enter
295 | tmux send-keys -t 0 "cd ~/completed/ && ls" C-m
296 | tmux send-keys -t 1 C-z 'ssh transmission' Enter
297 | tmux send-keys -t 1 "cd ~/completed/" C-m
298 | tmux send-keys -t 2 C-z 'ssh transmission' Enter
299 | tmux send-keys -t 2 "dfh" C-m
300 | tmux select-pane -t main:2
301 |
302 | ######## create a new window: k8s-prod (work with production k8s cluster)
303 | tmux new-window -t main:3 -n "k8s-prod"
304 |
305 | # split windows into panes
306 | tmux split-window -h
307 | tmux select-layout even-horizontal
308 | tmux select-pane -t 0
309 | tmux split-window -v -p 50
310 | tmux select-pane -t 2
311 | tmux split-window -v -p 50
312 | tmux select-pane -t main:3
313 |
314 | # run these commands in created panes
315 | tmux send-keys -t 0 C-z 'ssh k8s-node1' Enter
316 | tmux send-keys -t 0 "kubectl get nodes && kubectl get po --all-namespaces" C-m
317 | tmux send-keys -t 1 C-z 'ssh k8s-node2' Enter
318 | tmux send-keys -t 2 C-z 'ssh k8s-node3' Enter
319 | tmux send-keys -t 3 C-z 'ssh k8s-node4' Enter
320 |
321 | ######## create a new window: k8s-prod (work with production k8s cluster)
322 | tmux new-window -t main:4 -n "k8s-test"
323 |
324 | # split windows into panes
325 | tmux split-window -h
326 | tmux select-layout even-horizontal
327 | tmux select-pane -t 0
328 | tmux split-window -v -p 50
329 | tmux select-pane -t 2
330 | tmux split-window -v -p 50
331 | tmux select-pane -t main:4
332 |
333 | # run these commands in created panes
334 | tmux send-keys -t 0 C-z 'ssh k8s-test-node1' Enter
335 | tmux send-keys -t 0 "kubectl get nodes && kubectl get po --all-namespaces" C-m
336 | tmux send-keys -t 1 C-z 'ssh k8s-test-node2' Enter
337 | tmux send-keys -t 2 C-z 'ssh k8s-test-node3' Enter
338 | tmux send-keys -t 3 C-z 'ssh k8s-test-node4' Enter
339 |
340 | ######## create a new window: admin (server/network administration)
341 | tmux new-window -t main:5 -n "admin"
342 |
343 | # split windows into panes
344 | tmux split-window -h
345 | tmux select-layout even-horizontal
346 | tmux select-pane -t 0
347 | tmux split-window -v -p 50
348 | tmux select-pane -t 2
349 | tmux split-window -v -p 50
350 | tmux select-pane -t main:5
351 |
352 | # run these commands in created panes
353 | tmux send-keys -t 0 C-z 'ssh file' Enter
354 | # tmux send-keys -t 0 "awk '/^md/ {printf \"%s: \", $1}; /blocks/ {print $NF}' /proc/mdstat;" C-m
355 | tmux send-keys -t 1 C-z 'ssh nuc1' Enter
356 | tmux send-keys -t 1 "df -h && uptime && ls" C-m
357 | tmux send-keys -t 2 C-z 'ssh ns1' Enter
358 | tmux send-keys -t 2 "tail -n 30 /var/log/dnsmasq/dnsmasq.log" C-m
359 | tmux send-keys -t 3 C-z 'ssh ns2' Enter
360 | tmux send-keys -t 3 "pihole -c -e" C-m "curl -s http://${PIHOLE}/admin/api.php?summaryRaw | jq '.' | grep -E 'dns_queries_today|ads_blocked|percent'" C-m
361 | tmux select-pane -t main:5
362 |
363 | ######## create a new window: misc
364 | tmux new-window -t main:6 -n "misc"
365 |
366 | # split windows into panes
367 | tmux split-window -h
368 | tmux select-layout even-horizontal
369 | tmux select-pane -t 1
370 | tmux split-window -v -p 50
371 | tmux select-pane -t main:6
372 |
373 | # run these commands in created panes
374 | tmux send-keys -t 0 C-z 'cd ~' Enter
375 | tmux send-keys -t 1 C-z 'cd ~' Enter
376 | tmux send-keys -t 2 C-z 'cd ~' Enter
377 | tmux send-keys -t 3 C-z 'cd ~' Enter
378 |
379 | # switch focus back to main window, pane 0
380 | tmux select-window -t 0
381 | tmux select-pane -t 0
382 |
383 | # attach to main session
384 | tmux -2 attach-session -t main
385 | else
386 | echo "ERROR: Not implemented (UNKNOWN HOST/NETWORK)!"
387 | exit 1
388 | fi
389 | else
390 | # TODO: Add cygwin?
391 | echo "ERROR: Unknown \$OSTYPE, bailing out now!"
392 | exit 1
393 | fi
394 |
395 | #EOF
396 |
--------------------------------------------------------------------------------
/macgen.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | # macgen.py script to generate a MAC address for vm http://red.ht/2pnu0Xy
4 |
5 | # TODO: extend functionality
6 |
7 | import random
8 |
9 | def randomMAC():
10 | mac = [ 0x00, 0x16, 0x3e,
11 | random.randint(0x00, 0x7f),
12 | random.randint(0x00, 0xff),
13 | random.randint(0x00, 0xff) ]
14 | return ':'.join(map(lambda x: "%02x" % x, mac))
15 | #
16 | print randomMAC()
17 |
--------------------------------------------------------------------------------
/measure_latency.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # measure_latency.sh - a dirty measure of latency via ping
4 |
5 | # NOTE: This was only written so I could test latency quickly if I needed
6 | # to while in the terminal (which is where I spend all my time). If
7 | # you really want to view latency over time, use smokeping
8 |
9 | usage() {
10 | echo " e.g. $0 "
11 | }
12 |
13 | if [ $# -ne 2 ]; then
14 | echo "ERROR: You must supply a hostname/IP to measure & a packet count!"
15 | usage
16 | exit 1
17 | fi
18 |
19 | server=$1
20 | count=$2
21 | success=0
22 |
23 | if [[ $OSTYPE =~ "darwin" ]]; then
24 | # mac ping has different options that on linux
25 | while read line
26 | do
27 | # check if we successfully ran
28 | if [[ $line =~ "round-trip" ]]; then
29 | latency=$(echo $line | awk -F "/" '/round-trip/ {print $7}')
30 | echo "latency to $server with $count packets is: $latency"
31 | success=1 && exit 0
32 | fi
33 |
34 | # check packet loss if we didn't run correctly
35 | if [[ $line =~ "transmitted" ]] && [[ $success -ne 1 ]]; then
36 | pct=$(echo $line | awk '/transmitted/ {print $(NF-2)}' | \
37 | awk -F. '{print $1}')
38 |
39 | # check packet loss
40 | if [ $pct -eq 100 ]; then
41 | loss=$(echo $line |awk '/tted/ {print $(NF-2),$(NF-1),$NF}')
42 | echo "latency measurement failed: $loss"
43 | fi
44 | fi
45 | done < <(ping -q -c $count -i 0.2 -t 3 $server)
46 |
47 | elif [[ $OSTYPE =~ "linux" ]]; then
48 | # http://homepage.smc.edu/morgan_david/cs70/assignments/ping-latency.htm
49 | while read line
50 | do
51 | # check if we successfully ran
52 | if [[ $line =~ "rtt" ]]; then
53 | latency=$(echo $line | awk -F "/" '/rtt/ {print $5 " ms"}')
54 | echo "latency to $server with $count packets is: $latency"
55 | success=1 && exit 0
56 | fi
57 |
58 | # check packet loss if we didn't run correctly
59 | if [[ $line =~ "transmitted" ]] && [[ $success -ne 1 ]]; then
60 | pct=$(echo $line |awk '/transmitted/ {print $6}' | sed 's/%//g')
61 |
62 | # check packet loss
63 | if [ $pct -eq 100 ]; then
64 | loss=$(echo $line |awk '/mitted/ {print $6" "$7" "$8}' | \
65 | sed 's/,//g')
66 | echo "latency measurement failed: $loss"
67 | fi
68 | fi
69 | done < <(ping -q -c $count -i 0.2 -w 3 $server)
70 |
71 | else
72 | echo "ERROR: Unknown OS ($OSTYPE)"
73 | fi
74 |
75 | #EOF
76 |
--------------------------------------------------------------------------------
/mount_iso.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # date: 04/19/2009
4 | # author: Chad Mayfield (http://www.chadmayfield.com/)
5 | # license: gpl v3 (http://www.gnu.org/licenses/gpl-3.0.txt)
6 |
7 | # I have to mount ISO files at work all the time and quite often
8 | # at home as well. I created this utility to automate the process
9 | # of creating a mount point and mounting the ISO.
10 |
11 | #+-- Check to make sure that user is root
12 | if [ "$(id -u)" != "0" ]; then
13 | echo "ERROR: You must be root to run this utility!"
14 | exit 1;
15 | fi
16 |
17 | usage() {
18 | echo "Usage: $0 "
19 | exit 1;
20 | }
21 |
22 | #+-- Check arguments, if none print usage
23 | if [ $# -eq 0 ]; then
24 | usage
25 | fi
26 |
27 | isonamewithpath=$2 #+-- Save the full path of the iso
28 | isonameonly=${2##*/} #+-- Strip off the path from the iso
29 | mntdirname=`echo $isonameonly | sed -e 's/\.[a-zA-Z0-9_-]\+$//'`
30 | ismounted=`cat /proc/mounts | grep -c $mntdirname`
31 |
32 | mountiso() {
33 | #+-- Check if /mnt/$dirname is already there
34 | if [ ! -d /mnt/$mntdirname ]; then
35 | mkdir -p /mnt/$mntdirname && echo "SUCCESS: Created directory: /mnt/$mntdirname"
36 | fi
37 |
38 | #+-- Check if ISO is already mounted, if not mount it
39 | if [ $ismounted -ge 1 ]; then
40 | echo "ERROR: File $isonamewithpath already mounted!"
41 | else
42 | mount -o loop $isonamepath /mnt/$mntdirname && echo "SUCCESS: File $isonameonly mounted"
43 | #cd /mnt/$dirname
44 | fi
45 | }
46 |
47 | unmountiso() {
48 | if [ -ge 1 ]; then
49 | umount /mnt/$mntdirname
50 | echo "Unmounted $isonamepath"
51 | rm -rf /mnt/$mntdirname
52 | echo "Removed /mnt/$mntdirname"
53 | else
54 | echo "ERROR: File $isonamepath is not mounted"
55 | exit 1;
56 | fi
57 | }
58 |
59 | if [ $# -eq 2 ]; then
60 | if [ "$1" = "mount" ]; then
61 | mountiso
62 | elif [ "$1" = "unmount" ]; then
63 | unmountiso
64 | else
65 | echo "ERROR: Invalid option specified: $1"
66 | fi
67 | else
68 | usage
69 | fi
70 |
71 |
--------------------------------------------------------------------------------
/myrepos_status.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # status_myrepos.sh - get status of repos that have changes to commit under current tree
4 |
5 | for i in $(find . -name .git | sed 's/\/.git//g' | sort)
6 | do
7 | cd $i
8 |
9 | if [ -e .git ]; then
10 | repo=$(git config --local -l | grep "remote.origin.url" | awk -F "=" '{print $2}')
11 |
12 | # only show repos that have changes
13 | if [ $(git status -s | wc -l | awk '{print $1}') -gt 0 ]; then
14 | echo -e "Repo : \033[1m$repo\033[0m"
15 | echo -e "Path : \033[0;34m$(pwd)\033[0m"
16 | git status -s
17 | fi
18 | fi
19 |
20 | cd - > /dev/null 2>&1
21 | done
22 |
23 | #EOF
24 |
--------------------------------------------------------------------------------
/myrepos_update.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # update_myrepos.sh - update all my repos under the current tree
4 |
5 | # author : Chad Mayfield (chad@chd.my)
6 | # license : gplv3
7 |
8 | fail=0
9 | keys=( "$HOME/.ssh/hosting/src/id_ed25519" )
10 |
11 | bold=$(tput bold)
12 | normal=$(tput sgr0)
13 | black=$(tput setaf 0) # COLOR_BLACK/RGB:0,0,0
14 | red=$(tput setaf 1) # COLOR_RED/RGB:255,0,0
15 | green=$(tput setaf 2) # COLOR_GREEN/RGB:0,255,0
16 | yellow=$(tput setaf 3) # COLOR_YELLOW/RGB:255,255,0
17 | blue=$(tput setaf 4) # COLOR_BLUE/RGB:0,0,255
18 | magenta=$(tput setaf 5) # COLOR_MAGENTA/RGB:255,0,255
19 | cyan=$(tput setaf 6) # COLOR_CYAN/RGB:0,255,255
20 | white=$(tput setaf 7) # COLOR_WHITE/RGB:255,255,255
21 |
22 | for i in ${keys[@]}; do
23 | if ! [ -f "$i" ]; then
24 | echo "Key doesn't exist: $i"
25 | let fail+=1
26 | fi
27 | done
28 |
29 | if [ "$fail" -ne 0 ]; then
30 | echo "ERROR: Unable to find key(s)!"
31 | exit 1
32 | fi
33 |
34 | echo "Checking for ssh-agent..."
35 |
36 | # find all ssh-agent sockets
37 | #$find / -uid $(id -u) -type s -name *agent.\* 2>/dev/null()
38 |
39 | # set var if not set
40 | oursock="$HOME/.ssh/.ssh-agent.$HOSTNAME.sock"
41 |
42 | # is $SSH_AUTH_SOCK set
43 | if [ -z "$SSH_AUTH_SOCK" ]; then
44 | export SSH_AUTH_SOCK=$oursock
45 | else
46 | # it is, but check to make sure it's not keyring
47 | if ! [ "$SSH_AUTH_SOCK" = $oursock ]; then
48 | export SSH_AUTH_SOCK=$oursock
49 | fi
50 | fi
51 |
52 | # if we don't have a socket, start ssh-agent
53 | if [ ! -S "$SSH_AUTH_SOCK" ]; then
54 | echo "Not found! Starting ssh-agent..."
55 | eval $(ssh-agent -a "$SSH_AUTH_SOCK" >/dev/null)
56 | start_rv=$?
57 | echo $SSH_AGENT_PID > $HOME/.ssh/.ssh-agent.$HOSTNAME.sock.pid
58 |
59 | if [ "$start_rv" -eq 0 ]; then
60 | echo "Started: $SSH_AUTH_SOCK (PID: $SSH_AGENT_PID)"
61 | else
62 | echo "ERROR: Failed to start ssh-agent! (EXIT: $start_rv)"
63 | exit 1
64 | fi
65 | else
66 | echo "Found: $SSH_AUTH_SOCK"
67 | fi
68 |
69 | ## recreate pid
70 | #if [ -z $SSH_AGENT_PID ]; then
71 | # export SSH_AGENT_PID=$(cat $HOME/.ssh/.ssh-agent.$HOSTNAME.sock.pid)
72 | #fi
73 |
74 | # use the correct grammar for fun!
75 | if [ "${#keys[@]}" -eq 1 ]; then
76 | echo "Checking for key..."
77 | else
78 | echo "Checking for keys..."
79 | fi
80 |
81 | for i in "${keys[@]}"; do
82 | # grab key fingerprint
83 | cmp_key=$(ssh-keygen -lf $i)
84 |
85 | # if key fingerprint not found in fingerprint list, add it
86 | if [ $(ssh-add -l | grep -c "$cmp_key") -eq 0 ]; then
87 | echo "Key not found! Adding it..."
88 | ssh-add $i
89 | add_rv=$?
90 |
91 | if [ $add_rv -eq 0 ]; then
92 | echo "Key added."
93 | fi
94 | else
95 | echo "Key already added: $(echo $cmp_key | awk '{print $2}')"
96 | fi
97 | done
98 |
99 | # iterate through all child dirs to find git repos
100 | DIR="$(pwd)/"
101 | echo "Pulling updates... (${DIR})"
102 | for i in $(find "$DIR" -name .git | grep -vE "go/src/|.old_repos" | sed 's/\/.git//g' | sort)
103 | do
104 | (
105 | cd "$i"
106 |
107 | if [ -e .git ]; then
108 | repo=$(git config --local -l | grep "remote.origin.url" | awk -F "=" '{print $2}')
109 | echo " "
110 |
111 | if [[ $repo =~ "@" ]]; then
112 | repotype="SSH"
113 | else
114 | repotype="HTTPS"
115 | fi
116 |
117 | echo "====================================================="
118 | echo "${bold}Found repo ($repotype): ${yellow}$repo${normal}"
119 | echo "Pulling latest changes..."
120 | git pull
121 | #git pull --tags
122 | fi
123 | cd - > /dev/null 2>&1
124 | )
125 | done
126 |
127 | #EOF
128 |
--------------------------------------------------------------------------------
/polarhome.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 |
3 | # polarhome.pl - simplify ssh connection to polarhome.com servers without
4 | # having to remember usernames and ports (port info at bottom)
5 |
6 | # author : Chad Mayfield (chad@chd.my)
7 | # license : gplv3
8 |
9 | use warnings;
10 | use strict;
11 |
12 | if ( @ARGV != 1 ) {
13 | print "ERROR: You must supply a host to connect!\n";
14 | print " e.g. $0 \n";
15 | exit 1;
16 | }
17 |
18 | our $user;
19 | my $host = shift @ARGV;
20 | my $hostck = "-oStrictHostKeyChecking=no";
21 | my $ident = "-i ~/.ssh/id_rsa_polarhome";
22 |
23 | # our hash with host and port
24 | my %hosts = (
25 | vax => "705",
26 | freebsd => "715",
27 | solaris => "725",
28 | openbsd => "735",
29 | netbsd => "745",
30 | debian => "755",
31 | alpha => "765",
32 | aix => "775",
33 | hpux => "785",
34 | redhat => "795",
35 | ultrix => "805",
36 | qnx => "815",
37 | irix => "825",
38 | tru64 => "835",
39 | openindiana => "845",
40 | suse => "855",
41 | openstep => "865",
42 | mandriva => "875",
43 | ubuntu => "885",
44 | scosysv => "895",
45 | unixware => "905",
46 | dragonfly => "915",
47 | centos => "925",
48 | miros => "935",
49 | hurd => "945",
50 | minix => "955",
51 | raspberrypi => "975",
52 | # plan9 => "",
53 | );
54 |
55 | # debug
56 | #print "size of hash: " . keys( %hosts ) . " hosts.\n--------\n";
57 |
58 | # ik there's a better way to do this, but i'm too lazy to fight it right now
59 | my $count =0;
60 | # iterate through hash and check if @ARGV[0] exists
61 | while (my ($key, $value) = each(%hosts)) {
62 | if ( $key eq "$host" ) {
63 | # dynamically set username based on host
64 | if ( $key =~ m/(hpux|minix|openindiana|qnx|solaris|tru64)/ ) {
65 | $user = "user1";
66 | } elsif ( $key =~ m/(aix|alpha|solaris|ultrix|vax)/) {
67 | $user = "user2";
68 | } else {
69 | print "ERROR: You don't have an account on $host!\n";
70 | exit 1;
71 | }
72 | # ssh keys must be setup or this will not work
73 | exec("ssh -tt $ident $hostck -l $user -p $value $key.polarhome.com");
74 | } else {
75 | $count++
76 | }
77 |
78 | # debug
79 | #print "$key => $value\n";
80 | }
81 |
82 | if ( $count >= keys(%hosts) ) {
83 | print "ERROR: Host ($host) not found! Please check it and try again.\n";
84 | }
85 |
86 | # -----------------------------------------------------------------------------
87 | # POLARHOME HOSTNAME | PORT | DIRECT PORTS SERVICE PORTS
88 | # ========================================= =============
89 | # vax.polarhome.com 70x 2000-2999 ftp xx1
90 | # freebsd.polarhome.com 71x 10000-14999 telnet xx2
91 | # solaris.polarhome.com 72x 25000-29999 http xx3
92 | # openbsd.polarhome.com 73x 15000-19999 https xx4
93 | # netbsd.polarhome.com 74x 20000-24999 ssh xx5
94 | # debian.polarhome.com 75x 30000-34999 pop3 xx6
95 | # alpha.polarhome.com 76x 3000-3999 imap xx7
96 | # aix.polarhome.com 77x 35000-39999 usermin xx8
97 | # hpux.polarhome.com 78x 40000-44999 imaps xx9
98 | # redhat.polarhome.com 79x 5000-9999
99 | # ultrix.polarhome.com 80x 1025-1999
100 | # qnx.polarhome.com 81x 4000-4999
101 | # irix.polarhome.com 82x 45000-46999
102 | # tru64.polarhome.com 83x 47000-49999
103 | #openindiana.polarhome.com 84x
104 | # suse.polarhome.com 85x 59000-59999
105 | # openstep.polarhome.com 86x 52000-52999
106 | # mandriva.polarhome.com 87x 54000-55999
107 | # ubuntu.polarhome.com 88x 56000-58999
108 | # scosysv.polarhome.com 89x 61000-61999
109 | # unixware.polarhome.com 90x 60000-60999
110 | # dragonfly.polarhome.com 91x 62000-62999
111 | # centos.polarhome.com 92x 63000-63999
112 | # miros.polarhome.com 93x 64000-64100
113 | # hurd.polarhome.com 94x
114 | # minix.polarhome.com 95x
115 | #raspberrypi.polarhome.com 97x
116 | # plan9.polarhome.com 50000-51999
117 | # -----------------------------------------------------------------------------
118 |
119 | #EOF
120 |
--------------------------------------------------------------------------------
/powerbank.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # powerbank.sh - calculate number of charges of a powerbank
4 |
5 | # author : Chad Mayfield (chad@chd.my)
6 | # license : gplv3
7 |
8 | # TODO
9 | # + write the script!
10 | # algo: (labeled powerbank capacity x 3.7 / output voltage) * 0.85 / pb capacity / capacity of device = # recharges
11 | # example: (16000 x 3.7 / 5) x 0.85 / 1821 = 5.5 charges
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 | #EOF
--------------------------------------------------------------------------------
/randomize_mac.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # randomize_mac.sh - randomize mac address
4 |
5 | # author : Chad Mayfield (chad@chd.my)
6 | # license : gplv3
7 |
8 | # NOTE: This was meant to be a quick and easy way to get around things like
9 | # free wifi time limits, nothing more. It was a quick experiment to build a
10 | # tool myself like macchanger or spoofMAC. Even though it was an experiment
11 | # I began using it all the time.
12 |
13 | # if not already root, become root
14 | if [ $UID -ne 0 ]; then
15 | echo "ERROR: You must be root!"
16 | exit 1
17 | fi
18 |
19 | revert=0
20 | if [ $# -eq 1 ]; then
21 | if ! [[ $1 =~ "revert" ]]; then
22 | echo "ERROR: Invalid option, it must be --revert to revert back!"
23 | echo "to the default MAC address!"
24 | echo " e.g. $0 --revert"
25 | exit 1
26 | else
27 | revert=1
28 | fi
29 | fi
30 |
31 | # store original hw mac here
32 | sentinel=~/.default_hw_mac
33 |
34 | # we'll default to openssl for random hex generation
35 | command -v openssl >/dev/null 2>&1; i_can_haz_ssl=1 || { i_can_haz_ssl=0; }
36 |
37 | # let define a few default prefixes so our mac addresses look legitimate
38 | # entire list is here: http://standards.ieee.org/regauth/oui/oui.txt
39 | # Unicast: x2:, x6:, xA:, xE:
40 | # Multicast: x3:, x7:, xF:
41 | # NSA J125: 00:20:91
42 | # VMware: 00:50:56:00 to 00:50:56:3F
43 | # Xen: 00:16:3E
44 | # VirtualBox: 0A:00:27 (v5)
45 | # 08:00:27 (v4)
46 |
47 | # define prefix from above
48 | prefix=('08:00:27:' '0A:00:27:' '00:16:3E:' '00:50:56:' '00:20:91:')
49 |
50 | # the meat
51 | if [[ $OSTYPE =~ "darwin" ]]; then
52 | idx=$( jot -r 1 0 $((${#prefix[@]} - 1)) )
53 | sel=${prefix[idx]}
54 |
55 | default_iface=$(route -n get default | grep interface | awk '{print $2}')
56 | default_mac=$(ifconfig $default_iface | grep ether | awk '{print $2}')
57 |
58 | if [ $revert -eq 1 ]; then
59 | if [ ! -f $sentinel ]; then
60 | echo "ERROR: Unable to revert, can't find $sentinel! Try rebooting."
61 | exit 1
62 | else
63 | original_mac=$(cat $sentinel)
64 | echo "Original MAC address found: $original_mac"
65 | fi
66 |
67 | # change it back to default
68 | echo "Reverting it back..."
69 | ifconfig $default_iface ether $original_mac
70 |
71 | # verify that it was changed
72 | test_mac=$(ifconfig $default_iface | grep ether | awk '{print $2}')
73 | if [ "$test_mac" == "$original_mac" ]; then
74 | echo "Succeessfully changed MAC!"
75 | rm -f $sentinel
76 | else
77 | echo "MAC Address change unsuccessful."
78 | exit 1
79 | fi
80 | exit 1
81 | fi
82 |
83 | # set a sentinel file to keep track of real mac
84 | if [ -f $sentinel ]; then
85 | if [ $(cat $sentinel) != "$default_mac" ]; then
86 | # keep the mac in sentinel as default
87 | default_mac=$(cat $sentinel)
88 | fi
89 | else
90 | # first run, have to touch or it dies
91 | touch $sentinel
92 | echo $default_mac > $sentinel
93 | fi
94 |
95 | printf "%-20s %s\n" "Default Interface:" $default_iface
96 | printf "%-20s %s\n" "Default MAC Address:" $default_mac
97 |
98 | # generate part of a new mac address
99 | if [ "$i_can_haz_ssl" -eq "1" ]; then
100 | random_mac=$(openssl rand -hex 3 | sed 's/\(..\)/\1:/g; s/.$//')
101 | new_mac="${sel}${random_mac}"
102 | printf "%-20s %s\n" "Random MAC Address:" ${new_mac}
103 | else
104 | #random_mac=$()
105 | echo "Coming Soon!"
106 | fi
107 |
108 | # make the change
109 | ifconfig $default_iface ether $new_mac
110 |
111 | # verify that it was changed
112 | test_mac=$(ifconfig $default_iface | grep ether | awk '{print $2}')
113 | if [ "$test_mac" == "$new_mac" ]; then
114 | echo "Succeessfully changed MAC!"
115 | else
116 | echo "MAC Address change unsuccessful."
117 | fi
118 |
119 | elif [[ $OSTYPE =~ "linux" ]]; then
120 | sel=${prefix[$RANDOM % ${#prefix[@]} ]}
121 |
122 | default_iface=$(route | grep '^default' | grep -o '[^ ]*$')
123 | default_mac=$(ifconfig $default_iface | awk '/HWaddr/ {print $5}')
124 |
125 | if [ $revert -eq 1 ]; then
126 | if [ ! -f $sentinel ]; then
127 | echo "ERROR: Unable to revert, can't find $sentinel! Try rebooting."
128 | exit 1
129 | else
130 | original_mac=$(cat $sentinel)
131 | echo "Original MAC address found: $original_mac"
132 | fi
133 |
134 | # change it back to default
135 | echo "Reverting it back..."
136 | # bring the interface down (alt/old: ifconfig $default_iface down)
137 | echo "ip link set dev $default_iface down"
138 |
139 | # change the mac address (alt/old: ifconfig eth0 hw ether $new_mac)
140 | echo "ip link set dev $default_iface address $new_mac"
141 |
142 | # bring the interface up (alt/old: ifconfig eth0 up)
143 | echo "ip link set dev $default_iface up"
144 |
145 | # verify that it was changed
146 | test_mac=$(ip link show $default_iface | awk '/ether/ {print $2}')
147 | #test_mac=$(ifconfig $default_iface | awk '/HWaddr/ {print $5}')
148 | if [ "$test_mac" == "$original_mac" ]; then
149 | echo "Succeessfully changed MAC!"
150 | rm -f $sentinel
151 | else
152 | echo "MAC Address change unsuccessful."
153 | exit 1
154 | fi
155 | exit 1
156 | fi
157 |
158 | # set a sentinel file to keep track of real mac
159 | if [ -f $sentinel ]; then
160 | if [ $(cat $sentinel) != "$default_mac" ]; then
161 | # keep the mac in sentinel as default
162 | default_mac=$(cat $sentinel)
163 | fi
164 | else
165 | # first run
166 | touch $sentinel
167 | echo $default_mac > $sentinel
168 | fi
169 |
170 | printf "%-20s %s\n" "Default Interface:" $default_iface
171 | printf "%-20s %s\n" "Default MAC Address:" $default_mac
172 |
173 | # generate part of a new mac address
174 | if [ "$i_can_haz_ssl" -eq "1" ]; then
175 | random_mac=$(openssl rand -hex 3 | sed 's/\(..\)/\1:/g; s/.$//')
176 | new_mac="${sel}${random_mac}"
177 | printf "%-20s %s\n" "Random MAC Address:" ${new_mac}
178 | else
179 | random_mac=$(head -n 10 /dev/urandom | tr -dc 'a-fA-F0-9' | \
180 | fold -w 12 | head -n 1 | fold -w2 | paste -sd: - | \
181 | tr '[:upper:]' '[:lower:]')
182 | new_mac="${sel}${random_mac}"
183 | printf "%-20s %s\n" "Random MAC Address:" ${new_mac}
184 | fi
185 |
186 | # make the change
187 |
188 | # bring the interface down (alt/old: ifconfig $default_iface down)
189 | ip link set dev $default_iface down || \
190 | echo "ERROR: Unable to bring $default_iface down!"; exit 1
191 |
192 | # change the mac address (alt/old: ifconfig eth0 hw ether $new_mac)
193 | ip link set dev $default_iface address $new_mac || \
194 | echo "ERROR: Unable to chnage $default_iface MAC!"; exit 1
195 |
196 | # bring the interface up (alt/old: ifconfig eth0 up)
197 | ip link set dev $default_iface up || \
198 | echo "ERROR: Unable to bring $default_iface up!"; exit 1
199 |
200 | # verify that it was changed (alt: ifconfig $default_iface | grep HWaddr)
201 | test_mac=$(ip link show $default_iface | awk '/ether/ {print $2}')
202 | if [ "$test_mac" == "$new_mac" ]; then
203 | echo "Succeessfully changed MAC!"
204 | else
205 | echo "MAC Address change unsuccessful."
206 | fi
207 |
208 | else
209 | echo "ERROR: Unknown OSTYPE!"
210 | fi
211 |
212 | #EOF
213 |
--------------------------------------------------------------------------------
/remove_spaces.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # remove_space.sh - remove spaces of all files of a type in a path
4 |
5 | unset a i
6 |
7 | if [ $# -ne 1 ]; then
8 | echo "ERROR: You must supply both a path!"
9 | echo " e.g. $0 /home/user/Downloads/"
10 | exit 1
11 | fi
12 |
13 | path=$1
14 |
15 | # OLD METHOD
16 | #find $path -type f -print0 | while read -d $'\0' f; do mv -v "$f" "${f// /.}"; done
17 |
18 | # iterate through all regular files in $path and rename them
19 | while IFS= read -r -d $'\0' file; do
20 | mv -v "$file" "${file// /.}"
21 | done < <(find "$path" -type f -print0)
22 |
23 | #EOF
24 |
--------------------------------------------------------------------------------
/reverse_ssh_tunnel.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # reverse_ssh_tunnel.sh - create reverse ssh tunnel from current host to jumpbox
4 |
5 | # author : Chad Mayfield (chad@chd.my)
6 | # license : gplv3
7 |
8 | # NOTE: If keys are not setup you'll need to supply your password twice, once
9 | # for the test and once for the connection. I created this becuase on
10 | # certain servers I create reverse shells to bypass firewalls and leave
11 | # them running for a long time, so I always have to look up on a cheat
12 | # sheet the syntax, with this script I don't have to remember syntax!
13 |
14 | if [ $# -ne 3 ]; then
15 | echo "ERROR: You must supply the args; username hostname and port"
16 | echo " e.g. $0 "
17 | exit 1
18 | fi
19 |
20 | user=$1
21 | host=$2
22 | port=$3
23 |
24 | # must be a valid port
25 | if [[ $port -lt 0 ]] || [[ $port -gt 65536 ]]; then
26 | echo "ERROR: Invalid port, ($port), please use a port between 1-65536!"
27 | exit 1
28 | fi
29 |
30 | # crude check to see if we have connectivity
31 | if [ $(ping -c 1 -p $port $host | awk '/transmitted/ {print $(NF-2)}' | awk -F. '{print $1}') -eq 100 ]; then
32 | echo "ERROR: Unable to ping $host:$port! Are you sure it's up?"
33 | exit 1
34 | fi
35 |
36 | ck_if_tunnel_exists() {
37 | # check to see if the tunnel exists on the remote host
38 | netstat_test="netstat -tunla | grep -c 127.0.0.1:7000"
39 | tunnel_test=$(ssh -l $user -p $port $host "$netstat_test")
40 |
41 | if [ $tunnel_test -ne 0 ]; then
42 | echo "ERROR: There's already a tunnel running on $host! Please use it."
43 | exit 2
44 | fi
45 | }
46 |
47 | create_reverse_tunnel() {
48 | echo "No port is running, proceeding to create one..."
49 |
50 | # create tunnel in background without executing commands
51 | ssh -fN -p $port -R 7000:localhost:22 ${user}@${host}
52 |
53 | tunnel_rv=$?
54 |
55 | if [ $tunnel_rv -eq 0 ]; then
56 | echo "Tunnel created successfully! You can now connect to $host and run"
57 | echo "ssh -p 7000 user@localhost to connect back to this machine."
58 | else
59 | echo "ERROR: There was a problem creating the reverse tunnel!"
60 | exit 3
61 | fi
62 | }
63 |
64 | ck_if_tunnel_exists
65 | create_reverse_tunnel
66 |
67 | #EOF
68 |
--------------------------------------------------------------------------------
/rkhunter.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # rkhunter.sh - run rkhunter then log & email results
4 |
5 | # author : Chad Mayfield (code@chadmayfield.com)
6 | # license : gplv3
7 |
8 | command -v logrotate >/dev/null 2>&1; logrotate=1 || { logrotate=0; }
9 | command -v rkhunter >/dev/null 2>&1 || \
10 | { echo >&2 "ERROR: rkhunter isn't installed!"; exit 1; }
11 |
12 | if [ $UID -ne 0 ]; then
13 | echo "ERROR: You must be root to run this utility!"
14 | exit 1
15 | fi
16 |
17 | # set which package manager we should use
18 | if [ -f /etc/os-release ]; then
19 | pkgmgr=RPM
20 | elif [[ $(lsb_release -a 2> /dev/null | grep Desc) =~ (Ubuntu|Debian) ]]; then
21 | pkgmgr=DPKG
22 | elif [[ $OSTYPE =~ "darwin" ]]; then
23 | pkgmgr=BSD
24 | else
25 | pkgmgr=NONE
26 | fi
27 |
28 | # we want logrotate to rotate the logs weekly
29 | if [ $logrotate -eq 1 ]; then
30 | echo "checking if logrotate has been configured..."
31 |
32 | if [ $(grep -c rkhunter /etc/logrotate.d/*) -ne 1 ]; then
33 | #/var/log/rkhunter/rkhunter.log {
34 | # weekly
35 | # notifempty
36 | # create 640 root root
37 | #}
38 | echo "skipping logrotate autoconf, not implemented yet"
39 | else
40 | echo "rkhunter is already configured in logrotate"
41 | fi
42 | fi
43 |
44 | # where's our logs?
45 | logfile="/var/log/rkhunter/rkhunter.log"
46 |
47 | # runtime options
48 | rkhunter="command rkhunter"
49 | ver_opts="--rwo --nocolors --versioncheck"
50 | upt_opts="--rwo --nocolors --update"
51 | run_opts="-c --nomow --nocolors --syslog --pkgmgr $pkgmgr --cronjob --summary"
52 |
53 | # mail config
54 | mail_to='user@domain.tld'
55 | mail_from="root@$(hostname)"
56 | subject="RKHUNTER: Scan results for $(hostname)."
57 |
58 | # version check
59 | $rkhunter $ver_opts
60 |
61 | # run an update
62 | $rkhunter $upt_opts
63 |
64 | # finally run
65 | $rkhunter $run_opts
66 |
67 | # send an email
68 | mail -s "$subject" $mail_to < $logfile
69 |
70 | #EOF
71 |
--------------------------------------------------------------------------------
/screenshots/alert_login.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chadmayfield/scriptlets/eccdacee6bf4063472de1a6b9b90c3bb6e08cf71/screenshots/alert_login.png
--------------------------------------------------------------------------------
/update_other_repos.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # update_other_repos.sh - update the 'other' repos cloned under the current tree
4 |
5 | # git pull all repositories in tree
6 | for i in $(find . -name .git | sed 's/.git//g')
7 | do
8 | cd "$i"
9 |
10 | if [ -e .git ]; then
11 | repo=$(git config --local -l | grep "remote.origin.url" | awk -F "=" '{print $2}')
12 | echo "Found repo: $repo"
13 | echo "Pulling latest changes..."
14 | git pull
15 | else
16 | :
17 | fi
18 |
19 | cd ..
20 | done
21 |
22 | #EOF
23 |
--------------------------------------------------------------------------------
/vagrant_update_boxes.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # vagrant_update_boxes.sh - update all vagrant boxes under current directory
4 |
5 | for i in $(find . -name Vagrantfile)
6 | do
7 | if ! [[ $i =~ (do|vultr)- ]]; then
8 | echo "Found Vagrantfile at: $i"
9 | cd $(echo $i | sed 's/Vagrantfile//')
10 |
11 | boxname=$(grep "^ config.vm.box " Vagrantfile | awk -F "= " '{print $2}')
12 | echo "Updating box: $boxname"
13 |
14 | vagrant box update
15 |
16 | cd - &> /dev/null
17 | echo "============================================================"
18 | fi
19 | done
20 |
21 | #EOF
22 |
--------------------------------------------------------------------------------