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

所属分类: 脚本专栏 / linux shell 阅读数: 1326
收藏 0 赞 0 分享

复制代码 代码如下:

#!/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
}

更多精彩内容其他人还在看

linux whatis与whatis database 使用及查询方法(man使用实例)

在学习man命令时候,估计很多朋友都发现有man –f ,man –k 参数,可以查出很多数据。这些有的与man手册页相同,有些不是属于手册页的。它们数据怎么收集来的,并且whatis是怎么样工作的
收藏 0 赞 0 分享

linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random)

这篇文章主要介绍了linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell命令快捷获得系统帮助(一)[man-pages定义规范]

linux命令帮助,一般有2种,命令自身代码里面带有使用帮助说明,这种一般很精简,太长了,程序自身的大小以及日常维护不方便。还有一种,就是带有帮助文件,类似windows的chm格式文件。下面我说下这2种怎么样查阅
收藏 0 赞 0 分享

linux shell实现随机数几种方法分享(date,random,uuid)

这篇文章主要介绍了linux shell实现随机数多种方法(date,random,uuid),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 脚本实现tcp/upd协议通讯(重定向应用)

这篇文章主要介绍了linux shell 脚本实现tcp/upd协议通讯(重定向应用),需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell数据重定向(输入重定向与输出重定向)详细分析

这篇文章主要介绍了linux shell数据重定向(输入重定向与输出重定向)详细分析,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell 管道命令(pipe)使用及与shell重定向区别

这篇文章主要介绍了linux shell 管道命令(pipe)使用及与shell重定向区别,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux下使用tcpdump抓包的实现方法

tcpdump是Linux下面的一个开源的抓包工具,和Windows下面的wireshark抓包工具一样, 支持抓取指定网口、指定目的地址、指定源地址、指定端口、指定协议的数据。这篇文章主要介绍了Linux下使用tcpdump抓包的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

零基础入门篇之Linux及Arm-Linux程序开发笔记

这篇文章主要介绍了零基础入门篇之Linux及Arm-Linux程序开发笔记,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux 中 CURL常用命令详解

这篇文章主要介绍了Linux 中 CURL常用命令详解,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多