• ssh采用expect实现自动输入密码登录、拷贝


    1. 引言

    最近做了一个项目,需要频繁与另一台主机进行文件的传输;中间想到了很多方式:FTP、samba、curl等,但是还是感觉scp最好用。

    但scp也存在着一些问题,每次都需要输入目标机的密码,需人为手动干预,这个就比较烦了,那么有没有可以自动进行界面交互的命令呢?

    使用shell嵌套使用expect命令工作,很好的达到了文件的批量传输,无需人工干预的效果。

    2. 内容详解

    2.1 目标

    本博的目的是为了解决项目中出现的问题:scp文件时,需要人为干预(输入密码等),且scp需要输入目的主机+绝对路径

    而采用本博的脚本,可以实现快速简单的文件拷贝,而不需要输入目标主机的ip等,且无需人为干预输入目的主机的密码,实现快速、高效的文件传输

    2.2 实现框架

    • 1 目标主机的ip地址以变量的形式写入到脚本中,因此省略了每次输入目标ip的繁琐
    • 2 将目标主机的登录密码写入到脚本中,采用expect工具自动抓取密码,省略了人为的交互界面
    • 3 scp分为pull和push两种情况,本脚本的处理是:
    • push状态:即推送文件到目的主机,推送到的位置固定在目标主机的一个目录下,因此,便可以省略了每次都要写目标主机和存放的文件位置;命令如下:
      scp <local-file/dir>
    • pull状态:即从远程目标主机拉取文件到本机,无需写目标主机的ip,只要写上需要拉取的目标主机的文件和本机存放的位置即可,命令如下:
      scp <remote-file/dir> <local-file/dir>

    2.3 实现代码

    #==============================================================
    #CopyRight: 2018-06-03@jimmy_nie
    #E-mail: JimmyNie2017@163.com
    #Autor  Date        Modify
    #Jimmy  2018-06-01  create
    #==============================================================
    #!/bin/bash
    
    TAR_MAC="root@192.168.1.94"     #目标主机的IP地址
    TAR_DIR="/mnt/UDISK"            #目标主机存放文件的位置
    
    # 1. Determine the scp is push or pull, if there only 1 argument(push); 2(pull)
    if [ $# -eq 1 ];then
        FLAG="PUSH"
    elif [ $# -eq 2 ];then
        FLAG="PULL"
    else
        echo -e "scpd useage:
    	scpd local_dir[file] 			 --- push local dir/file to remote $TAR_MAC:$TAR_DIR 
                
    	scpd remote_dir[file] local_dir[file] 	 --- pull $TAR_MAC file/dir to local [without $TAR_MAC, only remote dir/file]
    "
        exit 1 
    fi 
    
    # 2. If push, Determine whether the destinated file existed or not
    if [ "$FLAG" == "PUSH" ];then
        if [ -f $1 -o -d $1 ];then
            expect -c "                                     #采用expect工具的命令(注意与shell命令的差异)
                spawn scp -r "$1" "$TAR_MAC:$TAR_DIR"   #-r为递归,将本机的文件/目录发送到目标机的固定位置
                expect "*password:"                       #当遇到以“*password:”结尾的字符串时
                set timeout 100                             #设置超时时间,超过这个时间,scp执行失败
                send "123123
    "                           #将密码填入到password中,相当于人为输入密码
                expect eof                                  #执行结束
            "
        else
            echo -e "$1 does not normal file or directory"
            exit 1
        fi
    
    # 3. If pull, determine whether local dir existed or not
    elif [ "$FLAG" == "PULL" ];then
        if [ ! -e $2 -a "$2" == "*/" ];then     #如果本机的目录不存在,则创建该目录,再执行拷贝
            mkdir -p "$2"
            expect -c "
                spawn scp -r "$TAR_MAC:$1" "$2"
                expect "*password:"
                set timeout 600
                send "123123
    "   
                expect eof
            "
        else
            expect -c "
                spawn scp -r "$TAR_MAC:$1" "$2"
                expect "*password:"
                set timeout 600
                send "123123
    "   
                expect eof
            "
        fi
    
    fi
    
    #4. Output error info
    if [ ! $? -eq 0 ]; then 
        if [ "$FLAG" == "PUSH" ];then
            echo -e "PUSH $1 to $TAR_MAC:$TAR_DIR failed"
        elif [ "$FLAG" == "PULL" ];then
            echo -e "PULL $2 from $TAR_MAC:$1 failed"
        fi
    fi
    
    
  • 相关阅读:
    [Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy
    [Redux] Reducer Composition with Arrays
    [Redux] Writing a Todo List Reducer (Toggling a Todo)
    [Redux] Writing a Todo List Reducer (Adding a Todo)
    [Javascript] Advanced Reduce: Common Mistakes
    [Javascript] Advanced Reduce: Additional Reducer Arguments
    [Redux] Avoiding Object Mutations with Object.assign() and ...spread
    [Javascript] Introducing Reduce: Common Patterns
    AI-Info-Micron-Insight:工业 5.0,伟大的思想将殊途同归
    AI-Info-Micron-Insight:案例分析:美光使用数据和人工智能来发现、倾听和感觉
  • 原文地址:https://www.cnblogs.com/Jimmy1988/p/9129356.html
Copyright © 2020-2023  润新知