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

所属分类: 脚本专栏 / linux shell 阅读数: 1291
收藏 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
}

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

shell脚本编程之循环语句学习笔记

这篇文章主要介绍了shell脚本编程之循环语句学习笔记,本文内容较简单,可以作为shell循环语句的备忘录,忘记怎么写时来看看吧~需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本编程之case语句学习笔记

这篇文章主要介绍了shell脚本编程之case语句学习笔记,本文代码中包含注释来说明case语句的使用,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本实现的阳历转农历代码分享

这篇文章主要介绍了Shell脚本实现的阳历转农历代码分享,本文是作者一个星期的工作成果,得来不易,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本实现复制文件到多台服务器的代码分享

这篇文章主要介绍了Shell脚本实现复制文件到多台服务器的代码分享,用在多机集群环境中非常方便,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本实现批量下载网络图片代码分享

这篇文章主要介绍了Shell脚本实现批量下载网络图片代码分享,本文基于下载Yahoo天气图标而写,图片地址需有一定的规则,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本实现检测文件是否被修改过代码分享

这篇文章主要介绍了Shell脚本实现的检测文件是否被修改代码分享,其实了解了原理就可以做很多事了,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本数组用法小结

这篇文章主要介绍了Shell脚本数组用法小结,本文讲解了数组声明、数组遍历、获取数组长度、删除数组元素、数组切片等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本批量重命名文件后缀的3种实现

这篇文章主要介绍了Shell脚本批量重命名文件后缀的3种实现,本文分别使用find + xargs +sed、for循环、rename等3种方法实现重命名文件后缀,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现的ls命令源码分享

这篇文章主要介绍了C语言实现的ls命令源码分享,本文是学习apue的练手之作,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux下查找后门程序 CentOS 查后门程序的shell脚本

这篇文章主要介绍了Linux下查找后门程序 CentOS 查后门程序的shell脚本,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多