2014-10-11 102 views
0

我已经实现expect脚本来自动化某些服务器上的ssh连接。Expect脚本和ssh

当ssh尝试第一次打开与服务器的连接时出现问题,我得到了一个是/否的问题。

我必须手动执行连接1,然后它不会再出现,脚本工作良好。

1st:我需要你的帮助才能在期望脚本中找到跳过这个问题的步骤,这有可能吗?

+0

-oStrictHostKeyChecking = no -oCheckHostIP =没有加入到ssh就可以解决问题,谢谢:) – user212051 2014-10-11 10:40:35

回答

0
  1. 这里有一个例子:

    #!/usr/bin/env expect 
    
    spawn ssh [email protected] 
    
    while {1} { 
        expect "(yes/no)?" { 
         send "yes\r" 
        } "password: " { 
         send "xxxxxxxx\r" 
         interact 
         exit 0 
        } 
    } 
    
  2. 而如果你的root目的就是自动的ssh,请有sshpass的尝试,但sshpass通常不是默认安装:

    `sshpass -p xxxxxxxx ssh [email protected]` 
    
  3. 这里还提供了设置公钥/私钥对的过程,这使您无需密码即可访问目标服务器:

    Generate a key pair, id_dsa.pub id_dsa with following command: 
        ssh-keygen -t dsa -b 1024 
    Move id_dsa to client:/home/xxxx/.ssh/ 
    Move id_dsa.pub to server:/home/xxxx/.ssh/, and "cat id_dsa.pub >> authorized_keys" 
    chmod 600 /home/xxxx/.ssh/* (both the id_dsa and remote authorized_keys) 
    
0
set timeout 30 
spawn ssh -o "StrictHostKeyChecking no" "[email protected]$ip" 
expect { 
    "RSA key fingerprint" {send "yes\r"; exp_continue} 
    -re "<regex for whatever the yes/no question is>" {send "yes\r"; exp_continue} 
    "assword:" {send "$password\r"} 
} 
sleep 5 
interact 

意志把一切都在一个循环,直到超时时间(30秒)。然后它会移动到脚本中的下一行。如果你需要的话,我还增加了对RSA的期望。