Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1074171
  • 博文数量: 120
  • 博客积分: 887
  • 博客等级: 准尉
  • 技术积分: 1883
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-05 21:10
个人简介

RHCE,CCNA,熟悉shell脚本

文章分类

全部博文(120)

文章存档

2015年(16)

2014年(20)

2013年(48)

2012年(20)

2011年(16)

分类: LINUX

2012-12-07 12:47:25

[root@localhost ~]# cat bash.sh
#!/bin/bash
for i in $(awk '{print $1}' passwd.txt)
do
j=$(awk -v I="$i" '{if(I==$1)print $2}' passwd.txt) //将AWK取出的值,赋值给j
expect /root/login.exp $i $j   //expect调用 login.exp 脚本,并带上参数i,j
done


#[root@localhost ~]# cat login.exp
#! /usr/bin/expect
##by Lightman
set ipaddress [lindex $argv 0]   //设置ipaddress变量,采用参数0
set password [lindex $argv 1]    //设置ipaddress变量,采用参数0
spawn ssh ii@$ipaddress          //spwan  激活ssh程序进行交互式运行
expect {
    "yes/no" {send "yes\r"}      //捕捉yes/no的字符串,并发送send命令
    "*assword:" {send "$password\r"}  //捕捉*assword的字符串,并发送send命令
}
expect "]*"                       //捕捉shell提示符($、#)
send "date\r"
send "sleep 5\r"
#send "su - ii\r"
send "su - root\r"
expect "*assword:"
send "echo\“hello \” >> /root/temp.txt\r"              //send 传送命令时,如遇到有双引号之类的 ,要用 "\" 进行转义
send "sleep 5\r"
send "exit\r"
send "sleep 5\r"
send "exit\r"
interact                           //terminal保持停留在当前服务器

[root@localhost ~]# cat passwd.txt
192.168.56.1    12345623
192.168.195.128 123456
192.168.177.128    1234563
阅读(8611) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~