经常需要把服务器的某些文件传到 Mac,或者获取 Mac 的一些文件到服务器。尽管有很多命令scp, ftp, rsync都可以,霸特每次都有敲好长的命令,好烦,而且还要输入密码。所以想着 wrap 到 expect 脚本这样密码什么的都不用输入了。

下面这个是放在服务器端的,用于把file/folder 传到 Mac

#!/usr/bin/expect
#file: _getF.expect
# 和 Bash 的变量一样
# 定义时不加$,用的时候加
set host xxxx@00.000.00.00
set fromWhere [lindex $argv 0]
set toWhere [lindex $argv 1]
#服务器端目录变量
if { $toWhere == "desktop" } {
set toWhere "/home/rayleo/Desktop/"
} elseif { $toWhere == "downloads" } {
set toWhere "/home/rayleo/Downloads/"
} elseif { $toWhere == "home" } {
set toWhere "/home/rayleo/"
} else {
set toWhere $toWhere
}
spawn scp -r -p "$host:$fromWhere" "$toWhere"
expect "assword:"
send "wwlcr\r"
interact

本来想服务器端和本地的一些常用变量可以用变量代替,

为了不被 Bash 在命令行解析,需要把$放在单引号或者\转意

但是传进来的变量无法被再次解析,I haven't figure out why yet

例如

putF $desktop/lensid/conf.mout desktop

把服务器端桌面的 lensid/conf.mout 文件 传到自己的 Mac桌面

传进来的$desktop 就无法解析了

单引号是同样的情况

can you tell me why? mail

不能解析就绕呗,所以就想到用一个 bash wrapper来处理,再传给 expect

用 Bash字符串替换

#!/bin/bash
#file: getF.bash
#获取参数
fromWhere=$1
toWhere=$2
#解析Mac变量
home=/Users/ruili
desktop=/Users/ruili/Desktop
downloads=/Users/ruili/Downloads
desk=/Users/ruili/YunPan/DeskFolder
fromWhere=${fromWhere//\$desktop/$desktop}
fromWhere=${fromWhere//\$desk/$desk}
fromWhere=${fromWhere//\$downloads/$downloads}
fromWhere=${fromWhere//\$home/$home}
# 服务器本地变量
lhome=/home/rayleo
ldesktop=/home/rayleo/Desktop
ldownloads=/home/rayleo/Downloads
toWhere=${toWhere//\$desktop/$ldesktop}
toWhere=${toWhere//\$downloads/$ldownloads}
toWhere=${toWhere//\$home/$lhome}
#传给 expect
/usr/bin/expect $HOME/Bin/bin/_getF "$fromWhere" $toWhere

下面的是 putF (pushF) ;把文件从服务器推到 Mac 端

#!/usr/bin/expect
#_putF.expect
#定义变量
set host xxxxxxx@00.000.00.00
set fromWhere [lindex $argv 0]
set toWhere [lindex $argv 1]
set desktop "/Users/ruili/Desktop/"
set downloads "/Users/ruili/Downloads/"
set home "/Users/ruili/"
#为单特殊目录替换
if { $toWhere == "desktop" } {
set toWhere "/Users/ruili/Desktop/"
} elseif { $toWhere == "downloads" } {
set toWhere "/Users/ruili/Downloads/"
} elseif { $toWhere == "desk" } {
set toWhere "/Users/ruili/YunPan/DeskFolder/"
} else {
set toWhere $toWhere
}
#执行
spawn scp -r -p "$fromWhere" "$host:$toWhere"
expect "assword:"
send "wwlcr\r"
interact

Bash wrapper

#!/bin/bash
#file: putF.bash
fromWhere=$1
toWhere=$2
home=/Users/ruili
desktop=/Users/ruili/Desktop
downloads=/Users/ruili/Downloads
desk=/Users/ruili/YunPan/DeskFolder
toWhere=${toWhere//\$desktop/$desktop}
toWhere=${toWhere//\$desk/$desk}
toWhere=${toWhere//\$downloads/$downloads}
toWhere=${toWhere//\$home/$home} lhome=/home/rayleo
ldesktop=/home/rayleo/Desktop
ldownloads=/home/rayleo/Downloads
fromWhere=${fromWhere//\$desktop/$ldesktop}
fromWhere=${fromWhere//\$downloads/$ldownloads}
fromWhere=${fromWhere//\$home/$lhome} /usr/bin/expect $HOME/Bin/bin/_putF $fromWhere $toWhere

现在可以

putF \$downloads/google.dmg desktop

把/home/$USER/Downloads 底下的 google.dmg 文件传到 Mac 的桌面

getF \$desktop/bt2_bf_PE.fq \$desktop/../QTL

把 Mac 桌面的bt2_bf_PE.fq文件传到服务的和桌面同级的 QTL 文件

push or get File or Folder using scp wrapped with expect and bash的更多相关文章

  1. The specified file or folder name is too long

    You receive a "The specified file or folder name is too long" error message when you creat ...

  2. The URL "filename" is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web

    Sharepoint Error : The URL "filename" is invalid. It may refer to a nonexistent file or fo ...

  3. [转]ADT中通过DDMS导入文件出错ddms transfer error: Read-only file system,Failed to push selection: Read-only file system

    [已解决] 原文  http://www.crifan.com/ddms_import_file_error_transfer_error_read_only_file_system/ 想要通过adt ...

  4. File and Folder Permissions

    https://msdn.microsoft.com/en-us/library/bb727008.aspx On NTFS volumes, you can set security permiss ...

  5. c# zip file and folder programmatically

    In .net 4.5 Framework, we can zip a file by this way: private static string CompressFile(string sour ...

  6. Failed to push selection: Read-only file system的解决方法

    1.获得root权限:adb root 2.设置/system为可读写:adb remount 3.将hosts文件复制到PC:adb pull /system/usr/keylayout/mtk-k ...

  7. 编译Linux-4.15.1内核时遇到:“error : openssl/bio.h :No such file or folder”

    如题: scripts/extract-cert.c::: fatal error: openssl/bio.h: No such file or directory compilation term ...

  8. 王立平--Failed to push selection: Read-only file system

    往android模拟器导入资源,失败. 提示:仅仅读文件. mnt是仅仅读文件.应点击sdcard.,在导入

  9. shell 中scp密码输入 --expect

    这里必须先安装: yum install expect -y expect是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录. 下面给出scp和ssh的使用示例: 1. ...

随机推荐

  1. Ubuntu ./configure 半途终止 导致没有生成makefile文件 解决方法

    在安装thrift的时候,解压包进入目录,执行命令: ./configure 之后,发现某些包没有安装,导致configure到一半的时候退出,接着make发现没有makefile文件.估计是我系统安 ...

  2. memcached服务器

    memcached是高性能的分布式内存缓存服务器,通过缓存数据库查询结果,减少数据库访问次数,提高动态web应用的速度和可扩展性.为了提高性能,memcached把数据存储在内存中,重启memcach ...

  3. Tp验证码:$Verify = new \Think\Verify(); $Verify->entry(n);【参数n,页面有多个验证码时用】

    一.验证码参数:(中文字符集和英文字符集在父类里面都可以取到,可修改) //1.生成验证码 $Verify = new \Think\Verify(); $Verify->entry(n);[参 ...

  4. TypeScript 自动编译

    安装Typescript npm install -g typescript 手动编译 tsc greeter.ts 自动编译 tsc -w greeter.ts

  5. Java 获取汉字拼音的方法

    package lius.util;   import java.io.Serializable; import java.util.ArrayList;   public class JString ...

  6. servlet中service() 和doGet() 、doPost() 学习笔记

    Sevlet接口定义如下: 与Sevlet接口相关的结构图: service() 方法是 Servlet 的核心.每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要 ...

  7. JAVA中去掉空格经典整理

    JAVA中去掉空格经典整理 JAVA中去掉空格          1. String.trim() --------------trim()是去掉首尾空格           2.str.replac ...

  8. Libgdx 循环绘制图片时间隔的问题

    在libgdx中使用循环绘制一张图片铺满某个区域时,有可能会遇到像素计算没有问题时,图块中间还是有约1像素的间隔,或者是本来没有间隔,做了缩放处理之后发现中间有间隔. 解法 当使用Texture加载图 ...

  9. mysql去重

    select a.id,a.ssmz,(select count(ssmz) from shop_tourist_key b where b.ssmz=a.ssmz) as count        ...

  10. 用Pyinstaller打包发布exe应用

    有时候编写的Python程序依赖很多,如果要在不同服务器上安装python环境等东西有点得不偿失了.这时候可以使用pyinstaller和py2exe,能够将python程序打包成可执行的exe文件, ...