生成-找不到命令!


15

我正在使用Mac OS X 10.9.4,以下是我的脚本,用于将文件从本地计算机复制到其他主机

#!/bin/bash
#!/usr/bin/expect

echo "I will fail if you give junk values!!"
echo " "
echo "Enter file name: "
read filePath
echo " "
echo "Where you want to copy?"
echo "Enter"
echo "1. if Host1"
echo "2. if Host2"
echo "3. if Host3"
read choice
echo " "
if [ $choice -eq "1" ]
then
  spawn scp filePath uname@host1:/usr/tmp   
  expect "password"   
  send "MyPassword\r"
  interact
elif [ $choice -eq "2" ]
then
  spawn scp filePath uname@host2:/usr/tmp   
  expect "password"   
  send "MyPassword\r"
  interact
elif [ $choice -eq "3" ]
then
  spawn scp filePath uname@host3:/usr/tmp   
  expect "password"   
  send "MyPassword\r"
  interact
else
  echo "Wrong input"
fi

运行此脚本时,我正在关注

./rcopy.sh: line 21: spawn: command not found
couldn't read file "password": no such file or directory
./rcopy.sh: line 23: send: command not found
./rcopy.sh: line 24: interact: command not found

就我而言,我需要在期望代码周围放置EOD标记。stackoverflow.com/questions/26125036/…–
AnneTheAgile

Answers:


20

您的脚本正在尝试合并两个解释器。您同时拥有#!/bin/bash#!/usr/bin/expect。那行不通。您只能使用两者之一。从bash一开始,您的脚本就作为bash脚本运行。

但是,在脚本中,您有expect诸如spawn和的命令send。由于脚本是由bash而不是读取的expect,因此失败。您可以通过编写不同的expect脚本并从脚本中调用它们,bash或者将整个过程翻译成来解决此问题expect

最好的方法是避免设置简单文本文件中的纯文本密码的可怕做法,而是设置无密码ssh。这样,scp将不需要密码,并且您不需要expect

  1. 首先,在计算机上创建一个公共ssh密钥:

    ssh-keygen -t rsa

    您将被要求输入一个密码,每次登录后第一次运行任何ssh命令时,都会要求您输入该密码。这意味着对于多个ssh或scp命令,您只需输入一次即可。将密码短语留空以完全进行无密码访问。

  2. 生成公钥后,将其复制到网络中的每台计算机上:

    while read ip; do 
     ssh-copy-id -i ~/.ssh/id_rsa.pub user1@$ip 
    done < IPlistfile.txt

    IPlistfile.txt文件应该是每行包含服务器名称或IP的文件。例如:

    host1
    host2
    host3

    由于这是您第一次这样做,因此您将必须手动输入每个IP的密码,但是一旦完成,就可以使用以下简单的方法将文件复制到这些计算机中的任何一个:

    scp file user@host1:/path/to/file
  3. 从脚本中删除期望。现在,您可以无密码访问了,您可以将脚本用作:

    #!/bin/bash
    echo "I will fail if you give junk values!!"
    echo " "
    echo "Enter file name: "
    read filePath
    echo " "
    echo "Where you want to copy?"
    echo "Enter"
    echo "1. if Host1"
    echo "2. if Host2"
    echo "3. if Host3"
    read choice
    echo " "
    if [ $choice -eq "1" ]
    then
      scp filePath uname@host1:/usr/tmp   
    elif [ $choice -eq "2" ]
    then
      scp filePath uname@host2:/usr/tmp   
    elif [ $choice -eq "3" ]
    then
      scp filePath uname@host3:/usr/tmp   
    else
      echo "Wrong input"
    fi

1
如果启动ssh代理,则只需输入一次密码。否则,您需要每次输入。
glenn jackman

5

您的代码可以更加简洁:

#!/bin/bash

read -p "Enter file name: " filePath
if ! [[ -r $filePath ]]; then
    echo "cannot read $filePath"
    exit 1
fi

PS3="Where you want to copy? "
select host in host1 host2 host3; do
    if [[ -n $host ]]; then
        expect <<END
            spawn scp "$filePath" uname@$host:/usr/tmp   
            expect "password"   
            send "MyPassword\r"
            expect eof
END
        break
    fi
done

如果按照建议设置ssh密钥,那就更好了:

    if [[ -n $host ]]; then
        scp "$filePath" uname@$host:/usr/tmp   
        break
    fi

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.