forked from typhonius/profile.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionsrc
236 lines (216 loc) · 5.5 KB
/
functionsrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Contains all custom functions
# Downloads and phpizes composer
compose() {
curl -sS https://getcomposer.org/installer | php
echo "Use 'php composer.phar install' to install."
}
# Starts a simple http server at the current path
http() {
python -m SimpleHTTPServer
}
# Returns a random MAC address
newmac() {
MAC=`openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'`
echo "Setting MAC address to $MAC"
sudo ifconfig en0 ether $MAC
}
# Runs a du -h and orders the results by actual size rather than numeric size
dusort() {
TOTAL=`du -sh .`
perl -e'%h=map{/.\s/;99**(ord$&&7)-$`,$_}`du -sh *`;print@h{sort%h}';
echo "\nTotal size: $TOTAL"
}
# Executes commands against a number of servers
apdsh() {
if [ -z `which pdsh` ]; then
echo "pdsh must be installed before using apdsh."
exit 1
fi
OPTIND=1
sitename=
type=
command=
while getopts "s:t:c:" opt; do
case "$opt" in
s)
sitename=$OPTARG
;;
c)
command=$OPTARG
;;
t)
type=$OPTARG
;;
esac
done
if [[ -z $command ]] || [ -z $type ] || [ -z $sitename ]; then
echo "Enter a sitename a command and a server type."
echo "Usage should be:"
echo "* apdsh -s eeamalone -t web -c "ls" #To execute ls against all eeamalone webs"
echo "* apdsh -s eeconeill.dev -t bal -c "netstat -nlept" #To execute netstat against all eeconeill bals"
echo "* apdsh -s eescooper.prod -t web,ded -c "touch foo" #To touch the file foo on all scooper prod deds and bals"
else
echo "Running $command against all $type servers on $sitename :>"
filter=$type
if [ $type == 'web' ] || [ $type == 'staging' ] || [ $type == 'ded' ] || [ $type == 'managed' ]; then
filter='web,staging,ded,managed'
fi
if [ $type == 'db' ]; then
type='ded\|fsdb\|fsdbmesh\|dbmaster'
fi
pdsh -w `aht @$sitename --show=$filter | awk {'print $1'} | grep $type | sed -e 's/^[ \t]*//' | tr '\n' ','` $command
fi
shift $((OPTIND-1))
}
# Text to speech
t2s() {
wget -q -U Mozilla -O /tmp/$(tr ' ' _ <<< "$1"| cut -b 1-15).mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en-au&q=$(tr ' ' + <<< "$1")";
if hash afplay 2>/dev/null; then
afplay /tmp/$(tr ' ' _ <<< "$1"| cut -b 1-15).mp3
else
mplayer /tmp/$(tr ' ' _ <<< "$1"| cut -b 1-15).mp3
fi
}
# Returns your external IP
myip() {
curl ifconfig.me
}
when() {
if [ "$#" -eq 0 ]; then
TIME=`date +%s`
echo "Using current time: $TIME"
else
TIME=$1
fi
perl -e "print 'Local: '. scalar(localtime($TIME))"; echo
perl -e "print 'UTC: ' . scalar(gmtime($TIME))"; echo
}
# Puts an ls with your cd
cd ()
{
if [[ -z "$1" ]] ; then
builtin cd "$HOME";
else
builtin cd "$*";
fi
if [ $? -ne 0 ]; then
if [ ! -x "$1" ] && [ -d "$1" ]; then
echo -n "Cannot access dir, become root? ";
read foo;
if [[ $foo = "y" ]] || [[ $foo = "Y" ]]; then
sudo bash;
return;
else
builtin cd "$*";
return;
fi;
fi;
else
echo;
ls -lrtha;
fi
}
pskill() {
local pid
pid=$(ps -ax | grep $1 | grep -v grep | gawk '{ print $1 }')
echo -n "killing $1 (process $pid)..."
kill -9 $pid
echo "slaughtered."
}
ttl () {
echo "Finding TTL for $1"
NS=`dig $1 NS +noall +short`
arr=$(echo $NS | tr " " "\n")
for x in $arr
do
# if [ "$x" != "$1." ]; then
echo NameServer: $x
dig @$x $1 +noall +answer | grep $1 | awk {'print $2'}
# fi
done
}
sshcheck () {
if [ "$#" -eq 0 ]; then
echo "Enter some servers"
else
for arg; do
echo "Checking $arg" ;
ssh $arg "uptime" ;
done
fi
}
x () {
if [[ -z "$1" ]] ; then
print "No files given."
return
fi
for f in $* ; do
if [[ -f $f ]]; then
case $f in
*.tar.bz2) command tar xjf $f ;;
*.tar.gz) command tar xzf $f ;;
*.bz2) command bunzip2 $f ;;
*.rar) command unrar x $f ;;
*.gz) command gunzip $f ;;
*.tar) command tar xf $f ;;
*.tbz2) command tar xjf $f ;;
*.tgz) command tar xzf $f ;;
*.zip) command unzip $f ;;
*.Z) command uncompress $f ;;
*.7z) command 7z x $f ;;
*.xz) command unxz -vk $f ;;
*.lzma) command unlzma -vk $f ;;
*) print "'$f' cannot be extracted via x()" ;;
esac
else
print "'$f' is not a valid file"
fi
done
}
# create .tar.gz archive
com() {
if [[ -z "$1" ]] ; then
print "No files given."
return
fi
for f in $* ; do
if [[ -f $f ]]; then
command tar -cvzf $f.tar.gz $f
else
print "'$f' is not a valid file"
fi
done
}
#dirsize - finds directory sizes and lists them for the current directory
dirsize ()
{
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
egrep '^ *[0-9.]*M' /tmp/list
egrep '^ *[0-9.]*G' /tmp/list
rm -rf /tmp/list
}
psgrep() {
if [ ! -z $1 ] ; then
echo "Grepping for processes matching $1..."
ps aux | grep $1 | grep -v grep
else
echo "!! Need name to grep for"
fi
}
# Copy a file then follow it
cpg (){
if [ -d "$2" ];then
cp $1 $2 && cd $2
else
cp $1 $2
fi
}
# Move a file then follow it
mvg (){
if [ -d "$2" ];then
mv $1 $2 && cd $2
else
mv $1 $2
fi
}