我需要通过telnet向系统发送一些字符串,并获得以下示例字符串:
$ cat SERVERTEMPPASSWORDLIST.txt
SID=21|21|asldf8j2sRa2255||2840400|
SID=21|27|ala"sd8fjs2A"$||2840400|alsd8fj2s%"aa
SID=21|27|alsd"8fujs!"sl9a2e4|asdf2$sa5$23|2840400|asdfs2asl92
SID=21|21|holysshit||2840400|
脚本必须这样做:
$ cat test.sh
#!/usr/bin/env bash
(
grep -E "SID=${sid}" SERVERTEMPPASSWORDLIST.txt | while read line; do
TCID=$(echo ${line} | cut -d '|' -f 2)
TSPW="$(echo ${line} | cut -d '|' -f 3)"
TCPW="$(echo ${line} | cut -d '|' -f 4)"
DURATION="$(echo ${line} | cut -d '|' -f 5)"
DESC="$(echo ${line} | cut -d '|' -f 6)"
if [[ "${TCPW}" != "" ]]; then
cat <<- ADDENTRY
expect "error id=0 msg=ok"
send "addentry pw=${TSPW} desc=${DESC} duration=${DURATION} tcid=${TCID} tcpw=${TCPW}\r"
ADDENTRY
else
cat <<- ADDENTRY
expect "error id=0 msg=ok"
send "addentry pw=${TSPW} desc=${DESC} duration=${DURATION} tcid=${TCID}\r"
ADDENTRY
fi
done
) | expect > RESULT.txt
关于某些特殊字符,例如“和$失败并显示错误消息,在close-quote之后有一些额外的字符,或者例如找不到变量'sa5'。
$ ./test.sh
extra characters after close-quote
while executing
"send "addentry pw=ala"s"
我需要发送的一些文本已被转义,因为空格需要以'\ s'而不是''的形式发送。例如,你会发送
Hello World!
作为
Hello\sWorld!
重要的是,没有双重逃脱。
答案 0 :(得分:0)
解决方案是将字符串设置为大括号:
cat <<- ADDENTRY
expect "error id=0 msg=ok"
send {addentry pw=${TSPW} desc=${DESC} duration=${DURATION} tcid=${TCID} tcpw=${TCPW}}
expect "error id=0 msg=ok"
send "\r"
ADDENTRY
答案 1 :(得分:0)
混合这样的语言会让你感到悲伤。只需使用expect。而且,你在做什么?
#!/usr/bin/env expect
set sid [lindex $argv 0]
set fid [open SERVERTEMPPASSWORDLIST.txt r]
spawn ???
while {[gets $fid line] != -1} {
if { ! [string match "*SID=$sid*" $line} continue
# string replacements
set line [string map {{ } {\s} {"} {\"}} $line]
lassign [split $line |] tcid tspw tcpw duration desc
if {$tcpw ne ""} {
expect "error id=0 msg=ok"
send "addentry pw=$tspw desc=$desc duration=$duration tcid=$tcid tcpw=$tcpw\r"
} else {
expect "error id=0 msg=ok"
send "addentry pw=$tspw desc=$desc duration=$duration tcid=$tcid tcpw=$tcpw\r"
}
}
并将SID作为expect脚本的第一个参数传递