期望通过ssh写入远程文件

时间:2019-05-27 14:50:42

标签: tcl expect

我有一个脚本,该脚本正在解析本地文件,并远程执行使用前一个内容创建的新文件。

 Just an example: machine1 with the following command file content:
#cmd1
<blank line here>
#cmd5
hostname -f
reboot`

现在,脚本将解析该文件,删除空格和注释行,并使用新内容创建REMOTELY新文件:

proc _command {fh} {
    set fd [open "$fh" r]
    #set fp [open "/tmp/script_${fh}" w+]
    while { [gets $fd data] >= 0 } {
            if { [string length $data] > 0 } {
                    #skip commented & blank lines
                    if {[string match "#*" $data] || [string match "" $data]} {
                            continue
                    }
                    #puts $fp "$data"
                    send  "$data\r"
                    #send [exec echo $data >>/tmp/1.txt]
            }
    }
                    #close $fp
}
...
   spawn -noecho ssh -i $home/.ssh/.id_rsa -q -o StrictHostKeyChecking=no $user@$server
                    expect {
                            -re "($prompt)" {
                                            send "sudo su -\r"
                                            expect {
                                                    -re "# " {
                                                            _command $cfile
                                                            send "exit\r"

好吧,目前来说,每次执行脚本时,都会在本地而不是在远程计算机上创建文件,从而注释了写入文件的过程的一部分。

这是我想念的东西,但不知道是什么...

1 个答案:

答案 0 :(得分:1)

您真的需要这个吗?您已经在使用私钥身份验证,所以我认为您真正需要的是:

sed -e '/^$/d' -e '/^#/d' local_file | ssh user@host sudo sh -c 'cat > remote_file'
相关问题