Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 除了脚本和ssh之外_Linux_Shell_Expect - Fatal编程技术网

Linux 除了脚本和ssh之外

Linux 除了脚本和ssh之外,linux,shell,expect,Linux,Shell,Expect,我已经实现了expect脚本,以在一些服务器上自动化ssh连接 当ssh第一次尝试打开与服务器的连接时出现问题,我得到一个是/否问题 我必须先手动执行连接,然后它将不会再次出现,脚本工作良好 第一:我需要你的帮助,在expect脚本中找到跳过这个问题的步骤,这可能吗 以下是一个例子: #!/usr/bin/env expect spawn ssh root@127.0.0.1 while {1} { expect "(yes/no)?" { send "yes\r"

我已经实现了expect脚本,以在一些服务器上自动化ssh连接

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

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

第一:我需要你的帮助,在expect脚本中找到跳过这个问题的步骤,这可能吗

  • 以下是一个例子:

    #!/usr/bin/env expect
    
    spawn ssh root@127.0.0.1
    
    while {1} {
        expect "(yes/no)?" {
           send "yes\r"
        } "password: " {
            send "xxxxxxxx\r"
            interact 
            exit 0
        }
    }
    
  • 如果您的根目录目的只是自动ssh,请尝试sshpass,但sshpass通常不是默认安装:

    `sshpass -p xxxxxxxx ssh root@127.0.0.1`
    
  • 这里还提供了一个设置公钥/私钥对的过程,这使您无需密码即可访问目标服务器:

    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)
    
  • 设置超时30
    生成ssh-o“StrictHostKeyChecking no”“$usr@$ip”
    期待{
    “RSA密钥指纹”{send“yes\r”;exp\u continue}
    -重新“{send”yes\r;exp\u continue}
    “assword:{发送“$password\r”}
    }
    睡眠5
    互动
    

    将在超时时间(30秒)之前保持所有内容处于循环中。然后它将移动到脚本中的下一行。我还添加了一个expect for RSA,以防您需要它

    -oStrictHostKeyChecking=no-oCheckHostIP=no仅将此添加到ssh即可解决此问题,谢谢:)
    set timeout 30
    spawn ssh -o "StrictHostKeyChecking no" "$usr@$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