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

 更新时间:2013年02月09日 17:53:59   作者:  
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
}

相关文章

  • 如何使用 Shell 脚本执行 .NET Core 应用

    如何使用 Shell 脚本执行 .NET Core 应用

    这篇文章主要介绍了如何使用 Shell 脚本执行 .NET Core 应用,帮助大家更好的理解和使用shell 脚本,感兴趣的朋友可以了解下
    2020-09-09
  • linux命令行下使用curl命令查看自己机器的外网ip

    linux命令行下使用curl命令查看自己机器的外网ip

    Linux命令行下使用curl命令查看自己机器的外网ip,大家参考使用吧
    2013-12-12
  • Shell脚本获取进程的运行时间

    Shell脚本获取进程的运行时间

    这篇文章主要介绍了Shell脚本获取进程的运行时间,需要的朋友可以参考下
    2014-06-06
  • Linux Shell 如何获取参数的方法

    Linux Shell 如何获取参数的方法

    这篇文章主要介绍了Linux Shell 如何获取参数的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 一个简单的linux命令 tail

    一个简单的linux命令 tail

    这篇文章主要介绍了一个简单的linux命令tail,tail命令用来从指定点开始将文件写到标准输出,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • shell两个文件去重的多种姿势

    shell两个文件去重的多种姿势

    最近在利用shell帮公司优化挖掘关键词的流程,用shell替代了多个环节的操作,极大提高了工作效率。下面这篇文章就给大家分享了利用shell实现两个文件去重的多种姿势,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-12-12
  • shell脚本监控系统负载、CPU和内存使用情况

    shell脚本监控系统负载、CPU和内存使用情况

    这篇文章主要介绍了shell脚本监控系统负载、CPU和内存使用情况,本文分别给出监控服务器系统负载情况、监控系统cpu使用情况、、监控系统内存情况、监控系统交换分区swap使用情况的脚本,需要的朋友可以参考下
    2014-12-12
  • linux下的tar命令详细解释

    linux下的tar命令详细解释

    在Linux中,压缩与解压用得最多的tar.tar命令确实很厉害。下面通过本文给大家分享linux下的tar命令详细解释,感兴趣的朋友一起看看吧
    2017-08-08
  • linux 获取某个日期对应的月末日期方法

    linux 获取某个日期对应的月末日期方法

    今天小编就为大家分享一篇linux 获取某个日期对应的月末日期方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • Linux 命令之rsync命令详解

    Linux 命令之rsync命令详解

    rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。下面通过本文给大家分享Linux 命令之rsync命令详解,需要的朋友参考下吧
    2017-08-08

最新评论