如何使用 tcl-expect 将文件从远程主机复制到本地主机

发布于 2024-12-01 02:19:48 字数 485 浏览 1 评论 0原文

在这里,我使用 telnet 在两台机器之间建立连接,并在远程主机中创建一个名为“file.txt”的文件,并将“ls”命令执行的结果存储在 file.txt 中。现在我需要将文件发送到本地主机。我不知道该怎么做。任何知道的人都请帮助我。

#!/usr/local/bin/expect 
spawn telnet <machine ip> 
expect "login:" 
send "<username>\n" 
expect "Password:" 
send "<password>\n" 
expect "bash"
send " ls > file.txt\r "
expect "bash"
send " cat file.txt \r "
expect "bash"
send " command for copying file to the localhost computer\r"
expect "bash"
send "exit\r"

Here i am making a connection b/w two machines using telnet and in the remote host i am creating a file named "file.txt" and storing the result of the "ls" command execution in the file.txt. now i need to send the file to the local host. i dont know how to do it. anybody who knows it kindly help me out here.

#!/usr/local/bin/expect 
spawn telnet <machine ip> 
expect "login:" 
send "<username>\n" 
expect "Password:" 
send "<password>\n" 
expect "bash"
send " ls > file.txt\r "
expect "bash"
send " cat file.txt \r "
expect "bash"
send " command for copying file to the localhost computer\r"
expect "bash"
send "exit\r"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2024-12-08 02:19:48

强烈建议您使用 sshscp 来执行此任务。

#!/usr/local/bin/expect

### Factor these out
set user <username>
set pass <password>
set host <machineIP>

### Get the file list into a file remotely
spawn ssh $user@$host sh -c {ls -1 >file.txt}
expect {
    "Password:" {
        send "$pass\r"
        exp_continue
    }
    eof {
        close
    }
}
### Copy the file to the local machine
spawn scp $user@${host}:file.txt .
expect {
    "Password:" {
        send "$pass\r"
        exp_continue
    }
    eof {
        close
    }
}

You're strongly recommended to use ssh and scp for this task.

#!/usr/local/bin/expect

### Factor these out
set user <username>
set pass <password>
set host <machineIP>

### Get the file list into a file remotely
spawn ssh $user@$host sh -c {ls -1 >file.txt}
expect {
    "Password:" {
        send "$pass\r"
        exp_continue
    }
    eof {
        close
    }
}
### Copy the file to the local machine
spawn scp $user@${host}:file.txt .
expect {
    "Password:" {
        send "$pass\r"
        exp_continue
    }
    eof {
        close
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文