利用Expect自动生成公钥

2009年09月15日

Shell脚本是系统日常管理中重要的工具,可以实现循环、判断等控制,但无法实现交互式操作。Expect弥补了这个缺陷,成为高效的系统和网络管理中的重要工具。下面的一个小脚本实现了,自动生成公钥、私钥对,并将产生的公钥上传到目标服务器,实现身份认证。

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/expect
# Createkeypair.exp
# Usage: ./createkeypair host-ip username user-password
# Exist & print error message if there are not 3 parameters
if { $argc != 3 } {
  puts stderr "usage: createkeypair host-ip username user-password\n"
  exit 1
}
 
# Configure the parameter
# set timeout=60
set timeout 60
# Set 1st parameter as host ip
set host [lindex $argv 0]
# Set login name
set name [lindex $argv 1]
# Set login user's password
set password [lindex $argv 2]
 
# Create .ssh directory and generate the key pair
if { $name == "root" } {
  if { ![file isdirectory "/$name/.ssh"] } {
    send [ exec mkdir /$name/.ssh ]
  }
  spawn ssh-keygen -t rsa -f /$name/.ssh/id_rsa
} else {
  if { ![file isdirectory "/home/$name/.ssh"] } {
    send [ exec mkdir /home/$name/.ssh ]
  }
  spawn ssh-keygen -t rsa -f /home/$name/.ssh/id_rsa
}
expect {
  "(y/n)?" {
    send "yes\n"
    expect "passphrase"
    send "\n"
    expect "passphrase again:"
    send "\n"
   }
  "passphrase" {
     send "\n"
     expect "passphrase again:"
     send "\n"
   }
}
expect "#"
# send the generated public key to the destination host
if { $name == "root"} { 
    spawn scp /$name/.ssh/id_rsa.pub $name@$host:/tmp 
 } else { 
    spawn scp /home/$name/.ssh/id_rsa.pub $name@$host:/tmp 
 } 
expect { 
   "(yes/no)?" { 
       send "yes\n"
       expect "assword:"
       send "$password\n"
   } 
"assword:" { 
       send "$password\n"
   } 
 
} 
expect "100%"
 
 
# Add public key to the authenrized file
spawn ssh $host -l $name
expect "assword:"
send "$password\n"
expect "#"
 
if { $name == "root"} { 
   send "cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys\n"
} else { 
   send "cat /tmp/id_rsa.pub >> /home/$name/.ssh/authorized_keys\n"
} 
expect "#"
 
send "exit\n"
expect "#"
 
 
# Test auto login without password
 
spawn  ssh $host -l $name 
expect  { 
           "Last login" { 
               send_user "Successfully to login the server!"
           } 
           "assword:" { 
               send_user "failed to login the server!"
           } 
} 
 
send "ls\n"
expect "#"
send "exit\n"
 
expect eof

特保与反倾销

2009年09月14日

2009年4月20日钢铁工人联合会向美国国际贸易委员会提出申请,对中国产乘用车轮胎展开特保调查,这就是最近搞得沸沸扬扬的“特保案”。9月11日,奥巴马政府决定对从中国进口的所有小轿车和轻型卡车轮胎实施为期三年的惩罚性关税。

9月13日,我国商务部依照我国法律和世贸组织规则,对原产于美国的部分进口汽车和肉鸡产品其他反倾销和反补贴立案审查,称为“双反审查”。那么什么是特保和反倾销呢?

特保是特别保障措施(Product-specific Safeguard Messures)的简称,特别保障措施是世界贸易组织(WTO)成员利用特定产品过渡性保障机制(Transitional Product-specific Safeguard Mechanism)针对来自特定成员的进口产品采取的措施,即在WTO机制之下,在特定的过渡期内,进口政府为防止来自特定成员国的产品对本国相关产业造成损害而实施的限制性保障措施。

反倾销(Anti-dumping)是指对外国商品在本土市场上的倾销行为所采取的抵制措施。一般认为外国商品到岸价低于出厂价格视为倾销。