linux shell except tcl login ssh Automatic interaction

/***************************************************************************************
 *               linux shell except tcl login ssh Automatic interaction
 * 声明:
 *     本程序是使用except自动登入远程目标机,并且执行commands文件中的命令给定的命令,
 * 可以对多个目标机进行测试,目标机的IP保存在shell的数组中,目前只支持相同的账户和密码。
 *
 *                                               2015-9-15 晴 深圳 南山平山村 曾剑锋
 **************************************************************************************/

                          \\\\\\\\-*- 目录 -*-/////////
                          |   一、cat autorun.sh      |
                          |   二、cat ssh.sh          |
                          |   三、cat commands        |
                          |   四、运行结果:          |
                          \\\\\\\\\\\\\\///////////////

一、cat autorun.sh
    #!/bin/bash
    
    # ssh登入用户名,请修改为目标用户名,等号两边不能空格
    user=zengjf        
    # ssh登入密码,请修改为目标用户密码,等号两边不能空格
    passwd=zengjf
    # 修改这个目标IP数组,每个IP占一行
    ips=(
        192.168.0.65        
        127.0.0.1 
    )
    
    count=0
    for ip in ${ips[@]} ; do 
    
        # 执行自动交互脚本,传入用户名、密码、IP作为参数
        # 后台执行,多进程并行执行
        ./ssh.sh $user $passwd $ip &
    
        # 显示部分放在后面是为了后运行结果,不用再shell窗口中去翻页查看
        echo 
        echo   "-----------------------------------------------------"
        printf "|        ip = %-15s     NO. = %03d         |\n" $ip $count
        echo   "-----------------------------------------------------"
        echo 
    
        count=$(($count+1))
    done
    
    # 提示运行结束
    echo -e  "\033[32m|||||||||||||||||||||||||||||||||||||||||||||||||||||"
    echo -e  "-----------------------------------------------------"
    echo -e  "|                      TASK OVER                    |" 
    echo -e  "-----------------------------------------------------"
    echo -e  "|||||||||||||||||||||||||||||||||||||||||||||||||||||\033[0m"

二、cat ssh.sh
    #!/usr/bin/expect -f
    
    # reference web pages:
    #
    # 1. linux expect auto login ssh and ftp: 
    #    http://blog.51yip.com/linux/1462.html
    # 2. Array Initialization in Expect script: 
    #    http://stackoverflow.com/questions/17544467/array-initialization-in-expect-script
    # 3. Expect Script Tutorial: Expressions, If Conditions, For Loop, and While Loop Examples: 
    #    http://www.thegeekstuff.com/2011/01/expect-expressions-loops-conditions/
    # 4. Read file into String and do a loop in Expect Script: 
    #    http://stackoverflow.com/questions/17662391/read-file-into-string-and-do-a-loop-in-expect-script
    # 5. tcl sleep command: 
    #    http://www.wellho.net/forum/The-Tcl-programming-language/tcl-sleep-command.html
    
    # 参数必须满足要求,不满足要求,给出提示信息,并退出
    if {$argc != 3} {
        puts ""
        puts "USAGE:"
        puts "\t ./ssh.sh <username> <passwd> <host_ip>"
        puts ""
        exit -1
    }
    
    # 设置等待延时时间
    set timeout 5  
    
    # 获取ssh连接的用户名
    set ssh_user [lindex $argv 0]
    # 获取ssh连接的密码
    set password [lindex $argv 1]
    # 获取ssh连接的IP
    set host_ip  [lindex $argv 2]
    
    # 测试IP是否可用,不可用,给出提示信息,并退出
    spawn ping -c1 -w1 $host_ip
    expect {  
        " 0%"   { puts "$host_ip was available" }
        "100%*" { puts "$host_ip was not available"; exit -1 }
    } 
    
    # 读取命令文件,每一行一条命令
    set infile    [open "./commands" r]
    set file_data [read $infile]
    set cmds      [split $file_data "\n"]
    
    # 显示读取到的指令
    puts "show commands:"
    foreach {cmd} $cmds {
        puts "\t$cmd"
    }
    
    # ssh远程连接到目标机器
    spawn ssh ${ssh_user}@${host_ip}
    expect {  
        "*yes/no"    { send "yes\r"; exp_continue }  
        "?assword:*" { send "${password}\r" }  
    } 
    
    # 延时500毫秒,再继续运行
    #after 500
    
    # 等待目标返回,并跳转到目标机的根目录 
    expect "\."
    send "cd / \r"  
    
    # for循环执行目标指令
    foreach {cmd} $cmds {
        expect "\."
        send "${cmd} \r"
    }
    
    # 退出远程连接
    expect "\."
    send "exit \r"
    
    # 等待远程结束
    expect eof
    
三、cat commands
    ls -al
    pwd

四、运行结果:
    zengjf@zengjf-virtual-machine:~/zengjf/software/shell/autoSSH$ ./auto.sh 
    spawn ping -c1 -w1 192.168.0.65
    PING 192.168.0.65 (192.168.0.65) 56(84) bytes of data.
    
    --- 192.168.0.65 ping statistics ---
    2 packets transmitted, 0 received, 100% packet loss, time 999ms
    
    192.168.0.65 was not available
    
    -----------------------------------------------------
    |        ip = 192.168.0.65        NO. = 000         |
    -----------------------------------------------------
    
    spawn ping -c1 -w1 127.0.0.1
    PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
    64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.020 ms
    
    --- 127.0.0.1 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.020/0.020/0.020/0.000 ms
    127.0.0.1 was available
    show commands:
        ls -al
        pwd
        
    spawn ssh zengjf@127.0.0.1
    zengjf@127.0.0.1's password: 
    Welcome to Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic i686)
    
     * Documentation:  https://help.ubuntu.com/
    
    New release '14.04.1 LTS' available.
    Run 'do-release-upgrade' to upgrade to it.
    
    Last login: Tue Sep 15 15:14:34 2015 from localhost
    cd / 
    ls -al 
    pwd 
     
    exit 
    zengjf@zengjf-virtual-machine:~$ cd / 
    zengjf@zengjf-virtual-machine:/$ ls -al 
    total 104
    drwxr-xr-x  25 root root  4096 Nov 13  2014 .
    drwxr-xr-x  25 root root  4096 Nov 13  2014 ..
    drwxr-xr-x   2 root root  4096 Aug 12  2013 bin
    drwxr-xr-x   3 root root  4096 Aug 12  2013 boot
    drwxr-xr-x   2 root root  4096 Aug 12  2013 cdrom
    drwxr-xr-x  15 root root  4320 Sep 15 13:03 dev
    drwxr-xr-x 134 root root 12288 Sep 15 13:03 etc
    drwxr-xr-x   4 root root  4096 Sep  4  2013 home
    lrwxrwxrwx   1 root root    32 Aug 12  2013 initrd.img -> boot/initrd.img-3.5.0-23-generic
    drwxr-xr-x  24 root root  4096 Nov 18  2014 lib
    drwxr-xr-x   2 root root  4096 Nov 13  2014 lib64
    drwx------   2 root root 16384 Aug 12  2013 lost+found
    drwxr-xr-x   6 root root  4096 Sep 14 15:01 media
    drwxr-xr-x   4 root root  4096 May 16  2014 mnt
    drwxr-xr-x   3 root root  4096 Aug 24 14:56 nfsroot
    drwxr-xr-x   2 root root  4096 Feb 14  2013 opt
    dr-xr-xr-x 190 root root     0 Sep 15 13:03 proc
    drwx------  11 root root  4096 Aug 20 10:23 root
    drwxr-xr-x  24 root root   940 Sep 15 15:14 run
    drwxr-xr-x   2 root root  4096 Nov 13  2014 sbin
    drwxr-xr-x   2 root root  4096 Mar  5  2012 selinux
    drwxr-xr-x   2 root root  4096 Feb 14  2013 srv
    dr-xr-xr-x  13 root root     0 Sep 15 13:03 sys
    drwxrwxrwt  14 root root  4096 Sep 15 14:17 tmp
    drwxr-xr-x  11 root root  4096 Nov 13  2014 usr
    drwxr-xr-x  13 root root  4096 Sep 13 21:45 var
    lrwxrwxrwx   1 root root    29 Aug 12  2013 vmlinuz -> boot/vmlinuz-3.5.0-23-generic
    zengjf@zengjf-virtual-machine:/$ pwd 
    /
    zengjf@zengjf-virtual-machine:/$  
    zengjf@zengjf-virtual-machine:/$ exit 
    logout
    Connection to 127.0.0.1 closed.
    
    -----------------------------------------------------
    |        ip = 127.0.0.1           NO. = 001         |
    -----------------------------------------------------
    
    |||||||||||||||||||||||||||||||||||||||||||||||||||||
    -----------------------------------------------------
    |                      TASK OVER                    |
    -----------------------------------------------------
    |||||||||||||||||||||||||||||||||||||||||||||||||||||
    zengjf@zengjf-virtual-machine:~/zengjf/software/shell/autoSSH$ 
    

 

posted on 2015-09-15 15:22  zengjf  阅读(807)  评论(0编辑  收藏  举报

导航