• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    except自动登录的几段代码分享

    复制代码 代码如下:

    #!/usr/bin/expect -f
    set timeout 30
    set host "192.168.1.198"
    spawn ssh $host
    expect_before "no)?" {
    send "yes\r" }
    sleep 1
    expect "password:"
    send "123456\r"
    expect "*#"
    send "echo my name is fivetrees > /root/fivetrees.txt\r"
    interact

    ##----------------------------

    复制代码 代码如下:

    [root@fivetrees ~]# cat expect
    #!/usr/bin/expect
    for {set i 10} {$i = 12} {incr i} {
    set timeout 30
    set ssh_user [lindex $argv 0]
    spawn ssh -i .ssh/$ssh_user abc$i.com
    expect_before "no)?" {
    send "yes\r" }
    sleep 1
    expect "password*"
    send "hello\r"
    expect "*#"
    send "echo hello expect! > /tmp/expect.txt\r"
    expect "*#"
    send "echo\r"
    }
    exit

    ##-------------------------

    复制代码 代码如下:

    #!/usr/bin/expect
    if {$argc!=2} {
        send_user "usage: ./expect ssh_user password\n"
        exit
    }
    foreach i {11 12} {
    set timeout 30
    set ssh_user [lindex $argv 0]
    set password [lindex $argv 1]
    spawn ssh -i .ssh/$ssh_user root@xxx.yy.com
    expect_before "no)?" {
    send "yes\r" }
    sleep 1
    expect "Enter passphrase for key*"
    send "password\r"
    expect "*#"
    send "echo hello expect! > /tmp/expect.txt\r"
    expect "*#"
    send "echo\r"
    }
    exit

    ##---------------------------

    复制代码 代码如下:

    #!/usr/bin/expect
    set timeout 20
    if {$argc 1} {
      puts "Usage: script IP"
      exit 1
    }
    # 替换你自己的用户名
    set user "username"
    #替换你自己的登录密码
    set password "yourpassword"
    foreach IP  $argv {
    spawn  ssh $user@$IP
    expect \
      "(yes/no)?" {
        send "yes\r"
        expect "password:?" {
          send "$password\r"
        }
      } "password:?" {
        send "$password\r"
    }
    expect "\$?"
    # 替换你要执行的命令
    send "last\r"
    expect "\$?"
    sleep 10
    send "exit\r"
    expect eof
    }
    使用方法
    script_name   ip1  ip2  ip3 ...

    ##---------------------

    复制代码 代码如下:

    #!/bin/sh
    # -*- tcl -*- \
    exec tclsh $0 "$@"
    package require Expect
    set username [lindex $argv 0]
    set password [lindex $argv 1]
    set argv [lrange $argv 2 end]
    set prompt "(%|#|\\$) $"
    foreach ip $argv {
        spawn ssh -t $username@$ip sh
        lappend ids $spawn_id
    }
    expect_before -i ids eof {
        set index [lsearch $ids $expect_out(spawn_id)]
        set ids [lreplace $ids $index $index]
        if [llength $ids] exp_continue
    }
    expect -i ids "(yes/no)\\?" {
        send -i $expect_out(spawn_id) yes\r
        exp_continue
    } -i ids "Enter passphrase for key" {
        send -i $expect_out(spawn_id) \r
        exp_continue
    } -i ids "assword:" {
        send -i $expect_out(spawn_id) $password\r
        exp_continue
    } -i ids -re $prompt {
        set spawn_id $expect_out(spawn_id)
        send "echo hello; exit\r"
        exp_continue
    } timeout {
        exit 1
    }

    您可能感兴趣的文章:
    • 用expect实现ssh自动登录服务器并进行批量管理的实现方法
    • ssh expect自动登录的脚本代码
    • ssh自动登录的4种实现方法
    • putty实现自动登录的方法(ssh和ssh2)
    上一篇:bash批量修改文件名称的方法小结(增加,去除,修改后缀)
    下一篇:查看某时间段到现在的系统日志的sed命令
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    except自动登录的几段代码分享 except,自动,登录,的,几段,