我正在尝试读取本地计算机上的xml文件并将其写入生成的进程。当我使用puts和gets执行代码时,xml文件将在TCl打开提示符上打印,但不会在生成的进程提示符上打印。因此,在谷歌搜索后,命令'send'用于在生成的进程上发送数据。但阅读它然后使用send写入并没有帮助。我在下面附上我的代码。请帮忙。提前致谢。
package require Expect
package require tdom
log_file -noappend myscript.log
set spawned_machine (some IP here)
set username un
set password pw
set timeout 10
spawn plink.exe -ssh $username@$spawned_machine -pw $password
expect "*myspawnedmachine>"
exp_send "loadlicense\r"
expect "*Press CTRL-D on a blank line when done."
set filename "C:/newfolder/pqr.xml"
set size [file size $filename]
set fd[open $filename]
fconfigure $fd -buffering line
gets $fd data
while{$data != ""} {
set data1[puts $data]
gets $fd data
send $data1
}
close $fd
exp_send "\004"
expect "*myspawnedmachine>"
如果我改变上述逻辑,一次性读取整个pqr.xml文件,即
set filename "C:/newfolder/pqr.xml"
set size[file size $filename]
set fd[open $filename r]
set xml[read $fd]
set data[split $xml "\n"]
send $xml
close $fd
exp_send "\r"
exp_send "\004"
expect "*myspawnedmachine>"
它使用send将其发送到衍生的计算机,但xml文件未按预期发送。问题是,它不是一行一行地发送,而是所有行都在生成的进程上打印在同一行上。让我们在这里考虑一个xml文件 -
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
所以,在我的衍生机器中,这个xml文件被打印为 -
<?xml version="1.0"?><catalog> <book id="bk101"><author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description> </book>
也就是说,都在同一条线上。
虽然预计将发送为,
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
我知道这是一个冗长的问题,但任何帮助都会非常感激。提前致谢。