gpt4 book ai didi

linux - 我如何从期望脚本中的文件中读取数据

转载 作者:太空狗 更新时间:2023-10-29 11:45:59 25 4
gpt4 key购买 nike

#! /bin/expect   
set timeout 20
set user [lindex $argv 0]
set password [lindex $argv 1]
set prompt "$ " ;# -- main activity
proc dostuff { currenthost} {
;# do something with currenthost
send -- "ls -lrt\r" return} ;# -- start of task

set fd [open ./hostlist r]
set hosts [read -nonewline $fd]
close $fd

foreach host [split $hosts "\n" ] {
spawn /usr/bin/ssh $user@$host
while (1) {
expect {
"no)? " {
send -- "yes\r"
}
"password: " {
send -- "$password\r"
}
"$prompt" {
dostuff { $host }
break
}
}
}

expect "$prompt"
send -- "exit\r"
}
expect eof

我浏览了 this在线代码,但如果它被排列成 2 列,它会读取数据吗?

HOST   TFTP_SERVER_ADDRESS
xx XXYY
aa AABB
... ...
... ....

等等

我想将每个 TFTP_SERVER_ADDRESS 分配给一个变量并在我的脚本中使用它。

此外,这段代码是否安全:

# grab the password
stty -echo
send_user -- "Password for $user@$host: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)

#... later
send -- "$pass\r"

最佳答案

从文件中读取数据然后调用 expect 脚本的示例脚本。

更改密码.sh

while read line
do
pin=`echo $line | awk -F'|' '{print $1}'`
ipaddress=`echo $line | awk -F'|' '{print $2}'`
password=`echo $line | awk -F'|' '{print $3}'`
#echo "$pin -- $password -- $ipaddress"
echo "./t.expect $ipaddress $pin \"$password\" "
done<pin_password_8_192series.csv

期待

#!/usr/bin/expect -f
set timeout 20
set IPaddress [lindex $argv 0]
set PIN [lindex $argv 1]
set PASSWORD [lindex $argv 2]

spawn telnet $IPaddress
expect "localhost login:"
send "root\r"
expect "Password:"
send "root\r"
send "cd /mnt/sip\r"
send "sed -i 's/Password=\"$PIN\"/Password=\"$PASSWORD\"/g' Config.xml\r"
send "reboot\r"
send "exit\r"
interact

pin_password_8_192series.csv

341|192.168.6.1|&<M11qqHVkDdBj2
342|192.168.6.3|gyo8rB-9C<Ok.;=
343|192.168.6.6|skFI)4S-O.&-y,i
344|192.168.6.8|+Fqp6z*qEtQN0s?

关于linux - 我如何从期望脚本中的文件中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15322273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com