-
Notifications
You must be signed in to change notification settings - Fork 0
/
openbox_smbpipe
executable file
·175 lines (160 loc) · 6.08 KB
/
openbox_smbpipe
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
#!/bin/sh
IFS="`printf '\n\t'`" # to fix problem with sharenames with spaces
mountpath=/mnt/samba
credentialpath=$XDG_DATA_HOME/smbpipe
tmppath=/tmp
tmpfile=openbox_smbpipe_bash_tmp # different to the python version for testing purpose only ; filename with '1' appended is also used
age=30 # age of file before refreshing (in minutes)
#server, serverip, user #, filemanager
function getshares () {
server=$1
serverip=$2
user=$3
filemanager=$4
if [ "$user" == "guest" ] ; then
listoption="-N -U guest"
mountoption="guest"
edituser="" #guest is unchangeable
else
listoption="--authentication-file=$credentialpath/$server/$user"
mountoption="credentials=$credentialpath/$server/$user"
edituser="<separator label=\"Edit User\" />"\
"<item label=\"Delete $user\">"\
"<action name=\"Execute\">"\
"<command>$0 --credential-file $server --user $user --remove</command>"\
"</action>"\
"</item>"\
"<item label=\"Change password\">"\
"<action name=\"Execute\">"\
"<command>urxvt -e sh -c \"$0 --credential-file $server --user $user\"</command>"\
"</action>"\
"</item>"
fi
# get shares
#write all to new temporary file
#format: Type+=("sharename") (type=Disk,Printer,...)
smbclient -L \\$server -g $listoption | grep '|' | sed -e 's/\(.*\)|\(.*\)|.*$/\1+=("\2")/' > $tmppath/$tmpfile"1"
# sort values
sort $tmppath/$tmpfile"1"
# source values
. $tmppath/$tmpfile"1"
rm -f $tmppath/$tmpfile"1"
# get disks
echo "<separator label=\"Mount\" />"
for disk in ${Disk[@]} ; do
localpath=$mountpath/$server/"$disk"
openpath=""
if [ "$filemanager" != "" ] ; then
openpath=$filemanager" "$localpath
fi
echo "<item label=\"$disk\">"
echo "<action name=\"Execute\">"
echo "<command>urxvt -e sh -c \"sudo mkdir \\\"$localpath\\\"; sudo mount -t cifs \\\"//$server/$disk\\\" \\\"$localpath\\\" -o ip=$serverip,$mountoption,file_mode=0777,dir_mode=0777,noacl,noperm ; $openpath\"</command>"
echo "</action>"
echo "</item>"
done
#get printers
echo "<separator label=\"Printer\" />"
for printer in ${Printer[@]} ; do
echo "<item label=\"$printer\">"
echo "<action name=\"Execute\">"
#smb://username.password@servername/printer
echo "<command>sh -c \"echo \\\"smb://$server/$printer\\\" | xclip\"</command>"
echo "</action>"
echo "</item>"
done
#split $shares ... -> disks, printers
echo $edituser
}
filemanager=""
usefilemanager=""
if [ "$1" == "--filemanager" ] ; then
filemanager=$2
usefilemanager="--filemanager "$filemanager
shift 2
fi
if [ $# == 0 ] ; then
echo "<openbox_pipe_menu>"
echo "<menu id=\"smbpipebash\" label=\"Servers\" execute=\"$0 $usefilemanager --serverlist\" />"
echo "<item label=\"Refresh list\">"
echo "<action name=\"Execute\">"
echo "<command>$0 --refresh</command>"
echo "</action>"
echo "</item>"
echo "</openbox_pipe_menu>"
else
if [ "$1" == "--help" ] || [ "$1" == "-h" ] ; then
echo "\
Usage: $0 [--help|-h] [--filemanager fm] [--serverlist | --refresh | --server server | --credential-file server [--user user [--remove]]
Args:
--help or -h Display this help
--filemanager fm Open the share (mounted location) when mounting a share
--serverlist Returns the serverlist
--refresh Deletes temporary file. Next --serverlist returns a new serverlist
--server server Returns the shares for server
--credential-file server Prompts for a username and a password for server and writes a credential file
--user user Prompts for a password for user and writes/updates the credential file
--remove Removes the credential fiel for the user (only with --user)
"
else
if [ "$1" == "--refresh" ] ; then
rm -f $tmppath/$tmpfile
else
# getting the serverlist
if [ "$1" == "--serverlist" ] ; then
# use old serverlist (if not too old)
if [ "`find $tmppath -mmin -$age -name $tmpfile`" == "$tmppath/$tmpfile" ] ; then
cat $tmppath/$tmpfile
# get new serverlist
else
echo '<openbox_pipe_menu>' | tee $tmppath/$tmpfile
#get avaliable servers and convert to lower case ( | tr ... )
servers=`smbtree -S -N -g | grep '\\\\.' | sed -e 's/\t[\\]*//g' | tr "[:upper:]" "[:lower:]"`
for server in $servers ; do
echo '<menu id="'$server'" label="'$server'" execute="'$0' '$usefilemanager' --server '$server'" />' | tee -a $tmppath/$tmpfile
done
echo '</openbox_pipe_menu>' | tee -a $tmppath/$tmpfile
fi
else
if [ "$1" == "--credential-file" ] ; then
server=$2
username=""
if [ $? > 3 ] && [ "$3" == "--user" ] ; then
username=$4
fi
if [ $? > 5 ] && [ "$5" == "--remove" ] ; then
rm -f $credentialpath/server/username
else
mkdir -p $credentialpath/$server
chmod 700 $credentialpath/$server
if [ "$username" == "" ] ; then
read -p "Username: " username
fi
read -s -p "Password: " password
echo "username="$username > $credentialpath/$server/$username
echo "password="$password >> $credentialpath/$server/$username
chmod 600 $credentialpath/$server/$username
fi
else
if [ "$1" == "--server" ] ; then
echo '<openbox_pipe_menu>'
server=$2
serverip=`nmblookup $server | grep $server'<' | sed -e 's/ [^ ]*$//g'`
users=("guest" `ls $credentialpath/$server`)
for user in ${users[@]} ; do
echo "<menu id=\"$server-$user\" label=\"$user\">"
getshares "$server" "$serverip" "$user" "$filemanager"
echo "</menu>"
done
echo "<item label=\"Create credential file\">"
echo "<action name=\"Execute\">"
echo "<command>urxvt -e sh -c \"$0 --credential-file $server\"</command>"
echo "</action>"
echo "</item>"
echo "</openbox_pipe_menu>"
fi
fi
fi
fi
fi
fi