栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

自动执行远程主机命令expect脚本

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自动执行远程主机命令expect脚本

2014年第一个脚本,哈哈!!! expect实现远程主机自动执行命令脚本: 

#!/usr/bin/expect --

if { [llength $argv] < 4 } {
	puts "Usage: $argv0 ip user passwd port commands timeout"
	exit 1
}

match_max 600000

set ip [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set port [lindex $argv 3]
set commands [lindex $argv 4]
set timeoutflag [lindex $argv 5]

set yesnoflag 0
set timeout $timeoutflag


for {} {1} {} {
# for is only used to retry when "Interrupted system call" occured

spawn /usr/bin/ssh -q -l$user -p$port $ip

expect 	{

	"assword:" {
		send "$passwdr"
		break;
	}

	"yes/no)?" {
		set yesnoflag 1
		send "yesr"
		break;
	}

	"FATAL" {
		puts "nCONNECTERROR: $ip occur FATAL ERROR!!!n"
		exit 1
	}

	timeout {
		puts "nCONNECTERROR: $ip Logon timeout!!!n"
		exit 1
	}

	"No route to host" {
		puts "nCONNECTERROR: $ip No route to host!!!n"
		exit 1
	}

	"Connection Refused" {
		puts "nCONNECTERROR: $ip Connection Refused!!!n"
		exit 1
	}

	"Connection refused" {
		puts "nCONNECTERROR: $ip Connection Refused!!!n"
		exit 1
	}

	"Host key verification failed" {
		puts "nCONNECTERROR: $ip Host key verification failed!!!n"
		exit 1
	}

	"Illegal host key" {
		puts "nCONNECTERROR: $ip Illegal host key!!!n"
		exit 1
	}
		
	"Connection Timed Out" {
		puts "nCONNECTERROR: $ip Logon timeout!!!n"
		exit 1
	}

	"Interrupted system call" {
		puts "n$ip Interrupted system call!!!n"
	}
}
}

if { $yesnoflag == 1 } {
	expect {
		"assword:" {
			send "$passwdr"
		}

		"yes/no)?" {
			set yesnoflag 2
			send "yesr"
		}
	}
}

if { $yesnoflag == 2 } {
	expect {
		"assword:" {
			send "$passwdr"
		}
	}
}

expect {
	"]" {send "$commands r"}
	"assword:" {
		send "$passwdr"
		puts "nPASSWORDERROR: $ip Password error!!!n"
		exit 1
	}
}

expect {
	"]" {send "sleep 1 r"}
}

expect {
	"]" {send "exitr"}
}

expect eof {
	puts "OK_SSH: $ipn"
	exit 0;
}

用法:

./ssh.exp
Usage: ./ssh.exp ip user passwd port commands timeout
Wed Jan 1 00:17:28 CST 2014
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/10330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号