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

自动执行远程主机命令expect脚本,第1张

自动执行远程主机命令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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3012808.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-28
下一篇 2022-09-28

发表评论

登录后才能评论

评论列表(0条)

保存