最近有用到,利用expcet脚本自动登录到远程服务器并提权执行脚本。

搜集的知识如下:

tcl/tk参考——列表操作lindex

expect脚本解释

代码如下

#!/usr/bin/expect --
if { $argc != && $argc != } { exit }
set ip [lindex $argv ]
set port [lindex $argv ]
set proto [lindex $argv ] ;#:ssh;:telnet
set user [lindex $argv ]
set pwd [binary format H* [lindex $argv ]]
set cmds [lindex $argv ] if { $argc == } {
set root_pwd [binary format H* [lindex $argv ]]
puts "root_pwd:$root_pwd"
}

puts "ip:$ip";一些输出方便观察
puts "port:$port"
puts "proto:$proto"
puts "user:$user"
puts "pwd:$pwd"
puts "cmds:$cmds"
set timeout 30;设置超时 #set default client
set ssh "/usr/bin/ssh" #set default promptions
set login_pmt "ogin:"
set pwd_pmt "assword:"
set user_pmt "$ "
set root_pmt "# "
set login_fail_pmt "error"
set elevation_cmd "su -"
set elevation_pmt "assword:"
set elevation_ok_pmt "$root_pmt"
set elevation_failed_pmt "$user_pmt" ;把$符号转义一下
if { $user_pmt == "$" } {
set user_pmt "\$"
}
if { $root_pmt == "$" } {
set root_pmt "\$"
}
#puts "login_ont is $login_pmt"

;函数
proc handle_cmds { } {
global cmds user_pmt
set hex_cmds [split $cmds "|"]
puts "into handle_cmds"
foreach hex_cmd $hex_cmds {
set cmd [binary format H* $hex_cmd]
send -- "$cmd\r"
expect {
"$user_pmt" { }
"not found" { }
eof { exit }
timeout { exit }
}
}
} proc handle_cmds_root { } {
global cmds root_pmt
set hex_cmds [split $cmds "|"]
puts "into handle_cmds_root"
foreach hex_cmd $hex_cmds {
set cmd [binary format H* $hex_cmd]
send -- "$cmd\r"
puts "root:$cmd"
expect {
"$root_pmt" { } eof { exit }
timeout { exit }
}
}
} proc handle_pwd { } {
global pwd pwd_pmt user_pmt login_fail_pmt argc root_pwd root_pmt
puts "into handle_pwd"
puts "pwd:$pwd"
puts "pwd_pmt:$pwd_pmt"
send -- "$pwd\r" expect {
"$user_pmt" {
send -- "export LANG=en_US.UTF-8\r"
send -- "export LANGUAGE=en_US.UTF-8\r"
puts "argc $argc"
if { $argc == } {
send -- "su -\r" expect {
"$pwd_pmt" {
send -- "$root_pwd\r" expect {
"$root_pmt" handle_cmds_root eof { exit }
timeout { exit }
} }
eof { puts "-eof" ; exit }
timeout { puts "-timeout"; exit }
}
} elseif { $argc == } {
handle_cmds
}
}
timeout { puts "timeout" ; exit }
eof { puts "eof" ; exit }
}
exit
} proc handle_user { } {
global user pwd_pmt
send -- "$user\r"
expect {
"$pwd_pmt" handle_pwd
timeout { exit }
eof { exit }
}
} puts "result:$result" if { $proto == "" } {
if { $result == "CONTINUE" || $result == "ERROR" } {
spawn $ssh -p $port $user@$ip
} else {
send "$ssh -p $port $user@$ip\r"
}
} elseif { $proto == "" } {
if { $result == "CONTINUE" } {
spawn -noecho $telnet $ip $port
} else {
send -- "$telnet $ip $port\r"
}
} expect {
"$pwd_pmt" handle_pwd
"$login_pmt" handle_user
"(yes/no)?" {
puts "yes/no?"
send "yes\r"
expect {
"$pwd_pmt" { handle_pwd }
timeout { exit }
eof { exit }
}
}
eof { exit }
timeout { exit }
}

tck/tl 以及expect脚本的更多相关文章

  1. shell脚本嵌套expect脚本

    #!/bin/sh echo "helo" password='xxxx' ###不能在下面的expect脚本段设置成 set password xxxx否则获取不到变量,单独的e ...

  2. 使用expect脚本语言写一键发布服务(代码发布、所有服务重启)

    互联网服务有很多台服务,但是在上线的时候需要将这些服务版本都更新与个个都重启,下面的脚本语言,就是一键发布服务~ 1.在/home/weihu/deploy/ 目录下建下publish .publis ...

  3. Linux 下 expect 脚本语言中交互处理常用命令

    Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...

  4. Mac 让 iTerm2 记住用户名密码 expect 脚本

    刚刚用iTerm2的时候,总是要一遍遍的敲用户名.密码. 我在想, 能不能像Windows的软件一样,可以直接让软件记住.然后只要点击一下,就直接ssh到远程服务器上面去了. 之后经过搜索,可以用ex ...

  5. expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令

    自动同步文件 #!/usr/bin/expect set " spawn rsync -av root@.txt /tmp/ expect { "yes/no" { se ...

  6. 分发系统介绍 expect脚本远程登录 expect脚本远程执行命令 expect脚本传递参数

    expect脚本远程登录 yum install -y expect yum install -y tcl tclx tcl-devel 自动远程登录 #! /usr/bin/expect set h ...

  7. 与服务器同步工程(expect脚本)

    先看下我实际用的例子: #!/usr/bin/expect spawn rsync -vazu ssh-src/src wayne@192.168.5.2:~/projects/ expect &qu ...

  8. shell脚本通过expect脚本实现自动输入密码(使用expect)

    背景:在远程文件下载时,需要输入对方的服务器密码,shell不支持交互输入内容,可以用下面两种方式实现   一.在shell脚本中嵌入expect来实现密码输入 expect是一个自动交互功能的工具. ...

  9. shell脚本通过expect脚本实现自动输入密码

     背景:在远程文件下载时,需要输入对方的服务器密码,shell不支持交互输入内容,可以用下面两种方式实现   一.在shell脚本中嵌入expect来实现密码输入 expect是一个自动交互功能的工具 ...

随机推荐

  1. python3如何打印进度条

    Python3 中打印进度条(#)信息: 代码: import sys,time for i in range(50): sys.stdout.write("#") sys.std ...

  2. eclipse - 链接hadoop

    插件: 配置:Map/Reduce Location 问题:An internal error occurred during: "Map/Reduce location status up ...

  3. Android6的Logger日志系统

    版权声明:本文为博主原创文章,未经博主允许不得转载. Android2.x中Logger日志系统采用了字符设备驱动来支持,到了Android6.0已经找不到kernel/goldfish/driver ...

  4. UIColor

    UIColor.CIColor 和 CGColor 出现在不同的类库里面,其实就是颜色存储方式不同而已,比如 999 可以用 10 进制.2 进制.16 进制等存储.三者之间都是能够方便转换的,特别是 ...

  5. SingleClass单例类

    前言 对于一个单例类,无论初始化单例对象多少次,在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死,该实例对象就不会被释放,并且该对象是全局的,能够被整个系统访问到. 在应用这个模 ...

  6. New Distinct Substrings(后缀数组)

    New Distinct Substrings(后缀数组) 给定一个字符串,求不相同的子串的个数.\(n<=50005\). 显然,任何一个子串一定是后缀上的前缀.先(按套路)把后缀排好序,对于 ...

  7. 【BZOJ 3326】[Scoi2013]数数

    题目描述 Fish 是一条生活在海里的鱼,有一天他很无聊,就开始数数玩.他数数玩的具体规则是: 确定数数的进制B 确定一个数数的区间[L, R] 对于[L, R] 间的每一个数,把该数视为一个字符串, ...

  8. 洛谷P4137 Rmq Problem / mex(莫队)

    题目描述 有一个长度为n的数组{a1,a2,…,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. 输入输出格式 输入格式: 第一行n,m. 第二行为n个数. 从第三行开始,每行一个询问l, ...

  9. Cannot find module 'webpack/bin/config-yargs'

    1.版本不兼容 npm install webpack-dev-server@1.15.0 -g

  10. Hadoop源码分析之产生InputSplit文件过程

        用户提交 MapReduce 作业后,JobClient 会调用 InputFormat 的 getSplit方法 生成 InputSplit 的信息.     一个 MapReduce 任务 ...