使用SSH在远程服务器上无法运行的命令

时间:2017-02-16 08:28:57

标签: shell unix ssh expect

我正在尝试运行一个shell脚本,其中包含ssh login的预期代码。

我能够登录远程服务器,但无法在远程服务器上执行任何命令,而是在执行shell脚本的本地计算机上执行代码。

我使用以下方法运行shell的期望,因为我需要从同一个脚本中的三个文件(主机,用户名和密码)中获取值。 请找到以下代码:

**array function for creating an array and storing the file content in it. **
getArray "password.txt" //for getting data from file to array
for e in "${array[@]}" //password getting stored in 
do
    echo "$e"
/usr/bin/expect << EOD
spawn ssh abc@xxx.xx.xxx.xxx
sleep 5
expect "Password:"
sleep 5
send "$e\n"  //sending password
sleep 5
interact
expect "[abc@servername ~]"  //this is the text observed after logging into remote server
sleep 5
send "ls -lrt"  // trying to get file list from remote 
sleep 5
interact
sleep 5
EOD
sleep 5
ls -lrt
echo "you are out"
sleep 5
ls -lrt
done

********* *********输出***************************

我可以登录远程服务器,但输入“ls -lrt

的输出
$ invalid command name "abc@servername"
    while executing
"abc@servername ~"
    invoked from within
"expect "[abc@servername ~]""
total 24

为我的本地计算机显示的文件列表 你出去了 为我的本地机器显示的文件列表

请告诉我是否需要修改/上述代码的其他方法。

提前致谢

0 个答案:

没有答案
相关问题