expect无密码登陆

本人所有系统为ubuntu8.04今天写了个小程序,实现自动登陆
默认系统不带expect,要自己安装
1。安装expect
root@sunsyk-desktop:~/桌面# apt-get install expect
         正在读取软件包列表... 完成
         正在分析软件包的依赖关系树      
         读取状态信息... 完成
2。验证
root@sunsyk-desktop:~/桌面# exp
         expand  expect  expiry  export  expr   
root@sunsyk-desktop:~/桌面# expect
         expect1.1>
3。说明安装好了,我们进入到/home下
root@sunsyk-desktop:/home# vim ssh.sh
4。写个shell
#!/usr/bin/expect -f
set timeout 30
set host [ lindex $argv 0 ]
spawn ssh $host
expect "password:"
send "sun163\r"
interact
5。执行一下
root@sunsyk-desktop:/home# chmod 755 ssh.sh
root@sunsyk-desktop:/home# ./ssh.sh 192.168.1.6
spawn ssh 192.168.1.6
[email protected]'s password:
Last login: Tue Dec 30 10:18:41 2008 from 192.168.1.163
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:1C:C0:4E:B1:99 
          inet addr:192.168.1.6  Bcast:172.20.55.255  Mask:255.255.248.0
          inet6 addr: fe80::21c:c0ff:fe4e:b199/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:567123 errors:0 dropped:0 overruns:0 frame:0
          TX packets:416969 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
这已经登陆成功了,现在就在192.168.1.6机器上了,你可以做任何事了
 

两个小例子

例子1,批量拷贝

[root@moedb210 src]# cat ip.txt
192.168.1.170
192.168.1.210

[root@moedb210 src]# cat expect.sh
#!/usr/bin/expect -f
set ip [exec cat ip.txt]
foreach i $ip {
spawn scp "ip.txt" "root@$i:/mnt/"
set timeout 20
expect "password:"
exec sleep 10
send "password\r"
interact
}

例子2,批量检查

[root@moedb210 src]# cat checkhostname.sh
#!/usr/bin/expect -f
set ip [exec cat ip.txt]
foreach i $ip {
spawn ssh "root@$i"
set timeout 20
expect "password:"
exec sleep 10
send "password\r"
#interact
expect "#"
send "hostname\r"
#send "exit\r"
expect eof
}

你可能感兴趣的:(hostname,职场,scp,expect,休闲)