cygwin下使用expect执行ssh登录示例

示例

示例1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #!/usr/bin/expect

  set timeout 5

  spawn ssh thc_2

  expect {
	  "yes/no" {
		  send "yes\r"
		  exp_continue
	  }
  }

  while {1} {
			expect {
				"Press" {
					send "\n"
					exp_continue
				}
				"y/N" {
					send "\n"
					exp_continue
				}
				"root" {
					break;
				}
			}		  
		}

		interact

使用示例

1
  expect expect-ssh.sh thc_2

示例2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #!/usr/bin/expect

  # 检查参数数量
  if {$argc < 1} {
		 puts "Usage: $argv0 <target_host>"
		 exit 1
	 }

	 set target_host [lindex $argv 0]

	 spawn ssh "$target_host"

	 expect {
		 "yes/no" {
			 send "yes\r"
			 exp_continue
		 }
	 }

	 while {1} {
			   expect {
				   "Press" {
					   send "\n"
					   exp_continue
				   }
				   "y/N" {
					   send "\n"
					   exp_continue
				   }
				   "root" {
					   break;
				   }
			   }		  
		   }

		   interact

使用方法

1
  expect expect-ssh.sh thc_2
Licensed under CC BY-NC-SA 4.0
最后更新于 Dec 27, 2024 15:49 +0800
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计