经常需要把服务器的某些文件传到 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. 百度网盘生成二维码api

    分享出自精神,灵感来自大脑,在百度云网盘分享每一个文件,都会在页面生成一个二维码扫描的图片: 我就进一步看了该图片的地址: 发现没有,圈圈内是不是有点眼熟,就跟其他二维码api接口一样,只要盗用这段东 ...

  2. 关于Apache/Tomcat/JBOSS/Neginx/lighttpd/Jetty等一些常见服务器的区别比较和理解

    先说Apache和Tomcat的区别: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. ...

  3. Thinking in Java——笔记(15)

    Generics The term "generic" means "pertaining or appropriate to large groups of class ...

  4. 一起来做webgame,《卡片魔兽》(一)基础战斗

    写在前面的话 这不是教程,只是博主在娱乐过程中的一些小结记录.博主水平有限,没有什么高级的东西,只是将一些小的知识点结合一下,做这么一个养成类型的卡片页面游戏(=.=!有点绕).做一个完整的游戏,涉及 ...

  5. Unity5和WebGL移植指南的一些总结

    对于手游开发者来说,更新版本往往意味着非常复杂的过程,你需要根据反馈做更新.测试.提交然后等待审核,而由于不需要客户端依赖,页游往往是快速测试游戏版本的最佳途径,很多人可能都知道Unity 5可以再不 ...

  6. 关于AFNetworking中header的bug问题

    关于AFNetworking中header的bug问题 [摘要:AFNetworking那个正在ios开辟中便未几道了,网上一搜一大推,然则详细用法我便没有道了,偶然间我会整顿一下详细的一些用法.本日 ...

  7. slick for play 使用原生sql查询以及拼接sql

    在play中用函数式框架slick来操作数据库是一件很爽的事情.但有时因为某些特殊场景又不得不用原生的sql了. 还好slick支持这种写法,可以看看slick官方文档,Slick Plain SQL ...

  8. 利用nodeJS实现的网络小爬虫

    var http=require("http");var cheerio=require('cheerio');var url="http://www.imooc.com ...

  9. golang调用c++文件

    简要步骤: 1,将c++ 的方法提取到头文件.h中( ) 2,编译cc(c++)文件为动态链接库so文件 3,将头文件放入include目录 .so放入lib目录 4,go程序中指定 CFLAGS 和 ...

  10. 简单理解js的prototype属性

    在进入正文之前,我得先说说我认识js的prototype这个东西的曲折过程. 百度js的prototype的文章,先看看,W3School关于prototype的介绍: 你觉得这概念适合定义js的pr ...