Make it easy to scp/ssh
由于经常需要登录服务器、在服务器与本地之间传送文件,所以花了点时间写了个脚本,以提高效率
脚本内容如下(vi to_my_server.sh; chmod +x to_my_server.sh; cat to_my_server.sh):
#!/bin/bash #IP_OR_DOMAIN="www.waketzheng.top" IP_OR_DOMAIN=43.139.125.122 USER="root" #PORT=22 #PRI=~/.ssh/id_rsa #passwd='my password is xxx' if [ "$passwd" ]; then echo "$passwd" fi HOST=$USER@$IP_OR_DOMAIN if [ $1 ]; then cmd="scp" if [ $PRI ]; then cmd="$cmd -i $PRI" fi if [ $PORT ]; then cmd="$cmd -P $PORT" fi cmd="$cmd -r " if [ $2 ]; then echo $1 |grep -q '/' if [ $? -eq 0 ]; then cmd="$cmd$HOST:$1 $2" else cmd="$cmd$HOST:~/$1 $2" fi else cmd="$cmd$1 $HOST:~" fi else cmd="ssh" if [ $PRI ]; then cmd="$cmd -i $PRI" fi if [ $PORT ]; then cmd="$cmd -p $PORT" fi cmd="$cmd $HOST" fi echo "--> $cmd" sh -c "$cmd"
使用:
1. 登录服务器
./to_my_server.sh
2. 从本地传文件到服务器(会传到服务器的~目录下)
./to_my_server.sh /path/to/local/file
3. 从服务器拷贝文件到本地
./to_my_server.sh /abspath/of/file/in/server /local/folder/or/file # 例如: ./to_my_server.sh "~/.ssh/id_rsa.pub" .