slitaz-tools view boxes/scp-box @ rev 936

autoselect_driver(): no xorg_conf_d
author Xander Ziiryanoff <psychomaniak@xakep.ru>
date Mon Jan 12 01:19:06 2015 +0200 (2015-01-12)
parents e232f0e1413a
children 5d80f6fdbdb7
line source
1 #!/bin/sh
2 #
3 # SCP Box - Small front end to the secure file copy utility.
4 #
5 # Copyright (C) 2008-2014 SliTaz GNU/Linux - BSD License
6 #
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
9 . /lib/libtaz.sh
10 export TEXTDOMAIN='slitaz-boxes' # i18n
12 [ "$file" ] || file="$HOME"
13 [ "$dir" ] || dir="$HOME"
15 # Internal variables (we need a space before options).
16 config=$HOME/.config/scpbox
17 term="terminal -geometry 80x16"
18 scpopts=" -r -P 22"
20 # Make sure we have config files.
21 if [ ! -d "$config" ] || [ -f "$config/hosts" ]; then
22 mkdir -p $config
23 touch $config/hosts && chmod 0600 $config/hosts
24 fi
26 #
27 # Functions
28 #
30 # Help and usage
31 usage() {
32 cat << EOT
34 $(_ 'SCP Box - Small front end to the secure file copy utility.')
36 $(boldify "$(_ 'Usage:')")
37 $(basename $0) [$(_n 'command')|$(_n 'option')]
39 $(boldify "$(_ 'Commands:')")
40 list-hosts $(_n 'List all known hosts')
42 $(boldify "$(_ 'Options:')")
43 --file=$(_n '/path/to/file')
44 --dir=$(_n '/path/to/directory')
46 $(boldify "$(_ 'Examples:')")
47 $(basename $0) --file=$(_n '/path/to/file')
49 EOT
50 }
52 # List last used hosts.
53 list_hosts() {
54 for h in $(cat $config/hosts); do
55 echo -n "!$h"
56 done
57 }
59 # Main GUI box function with pure Yad spec
60 scpbox_main() {
61 icon=folder-remote
62 yad --title="$(_n 'SCP Box')" --window-icon=$icon \
63 --width=400 \
64 --image=$icon --image-on-top \
65 --text="$(_n '<b>Secure copy</b> - Copy files remotely with scp')" \
66 --form \
67 --field="$(_n 'User name:')" "$USER" \
68 --field="$(_n 'Hostname:')" "" \
69 --field="$(_n 'Known hosts:'):CB" "$(list_hosts)" \
70 --field="$(_n 'Options:')" "$scpopts" \
71 --field="$(_n 'Local file:'):FL" "$file" \
72 --field="$(_n 'Local directory:'):DIR" "$dir" \
73 --field="$(_n 'Remote path:')" "" \
74 --button="$(_n 'Download'):2" \
75 --button="$(_n 'Upload'):0" \
76 --button="gtk-close:1"
77 }
79 # Main function
80 scpbox() {
81 # Store box results
82 main=$(scpbox_main)
83 ret=$?
84 [ "$debug" ] && echo "DEBUG: main=$main"
86 user=$(echo $main | cut -d "|" -f 1)
87 hostname=$(echo $main | cut -d "|" -f 2)
88 options=$(echo $main | cut -d "|" -f 4)
89 remote=$(echo $main | cut -d "|" -f 7)
91 # Use and store new hostname.
92 if [ "$hostname" ]; then
93 echo "$hostname" >> $config/hosts
94 host="$hostname"
95 else
96 host=$(echo $main | cut -d "|" -f 3)
97 fi
98 if [ "$host" == "(null)" ] || [ ! "$host" ]; then
99 echo "No host, exit"; exit 0
100 fi
102 # Deal with --button values
103 case $ret in
104 0)
105 # Upload: do we have a single file or a directory (skip $HOME)
106 file=$(echo $main | cut -d "|" -f 5)
107 dir=$(echo $main | cut -d "|" -f 6)
108 if [ -f "$file" ]; then
109 local="$file"
110 elif [ "$dir" != "$HOME" ]; then
111 local="$dir"
112 else
113 echo "No file, exit"; exit 0
114 fi
115 cmd="scp $options $local $user@$host:$remote"
116 [ "$debug" ] && echo "DEBUG: $cmd"
117 $term -e "$cmd" ;;
118 2)
119 # Download: we need a remote file.
120 local=$(echo $main | cut -d "|" -f 6)
121 if [ ! "$remote" ]; then
122 echo "No remote file, exit"; exit 0
123 fi
124 cmd="scp $options $user@$host:$remote $local"
125 [ "$debug" ] && echo "DEBUG: $cmd"
126 $term -e "$cmd" ;;
127 *)
128 exit 0 ;;
129 esac
130 }
132 #
133 # Commands
134 #
136 case "$1" in
137 -h|--help) usage ;;
138 list-hosts) list_hosts ;;
139 ""|--*) scpbox ;;
140 *) usage ;;
141 esac
143 exit 0