在期望脚本中从文件grep ipaddress

时间:2014-03-12 14:50:52

标签: expect

这是代码,我遇到了需要从文件$ filename grep IP地址的行,         #!/ usr / local / bin / expect -     ###由Etihkru制作####

set env(TERM) vt100
set env(SHELL) /bin/sh
set env(HOME) /usr/local/bin

set PASSWORD er
set SIU [lindex $argv 0]
exp_internal 1
spawn ssh mashost
set USER admin
set PASS hidden

expect {
    "assword"  {send "$PASSWORD\r"}
}

        expect "ranosusr@rn2osscs603"
        send -- "cd /var/opt/bla/edd/ARNE_SIU \r"
        expect "ranosusr@rn2osscs603"
        send -- "grep -il $SIU *\r"
        set prompt {ranosusr@rn2osscs603> }
        expect -re "($SIU\\S+\\.xml).*$prompt$"
        set filename $expect_out(1,string)  
        send -- "/opt/ericsson/arne/bin/import.sh -f  -val:rall\r"
        expect "ranosusr@rn2osscs603"
        send -- "/opt/ericsson/arne/bin/import.sh -f  -import\r"
        expect "ranosusr@rn2osscs603"
        send -- "cat $filename | grep {1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\} \r"
        expect -re "{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}" *$prompt$"
        set IP $expect_out(1,string)
        sleep 10s

spawn ssh admin@$IP
expect {
    -re "RSA key fingerprint" {send "yes\r"}
    timeout {puts "Host is known"}
}

expect {
    "assword"  {send "$PASS\r"}
}

expect "Osmon>"
send -- "resumePMMeasurements STN=0,MeasurementDefinition=0 sftp://pmup-rn2sossv605:er@10.211.149.60/GRAN/rn2sossv605/$SIU/NeTransientUp \r"
expect "Osmon>"
send -- "getalarmlist \r"
expect -re "(\Operation Succeed\)"
send -- "rev \r"
expect "Osmon>"
send -- "subscribe 10.211.149.40 1 \r"
expect "Osmon>"
send -- "getsubscriptionstatus 1 \r"
expect "Osmon>"
interact

输出是:

perl siu1 FRTAMX
spawn ssh mashost
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {1162}

expect: does "" (spawn_id exp4) match glob pattern "assword"? no

expect: does "\r\n|-----------------------------------------------------------------|\r\n| This system is for the use of authorized users only.            |\r\n| Individuals using this computer system without authority, or in |\r\n| excess of their authority, are subject to having all of their   |\r\n| activities on this system monitored and recorded by system      |\r\n| personnel.                                                      |\r\n|                                                                 |\r\n| In the course of monitoring individuals improperly using this   |\r\n| system, or in the course of system maintenance, the activities  |\r\n| of authorized users may also be monitored.                    |\r\n|                                                                 |\r\n| Anyone using this system expressly consents to such monitoring  |\r\n| and is advised that if such monitoring reveals possible         |\r\n| evidence of criminal activity, system personnel may provide the |\r\n| evidence of such monitoring to law enforcement officials.       |\r\n|-----------------------------------------------------------------|\r\n\r\n" (spawn_id exp4) match glob pattern "assword"? no
Password:
expect: does "\r\n|-----------------------------------------------------------------|\r\n| This system is for the use of authorized users only.            |\r\n| Individuals using this computer system without authority, or in |\r\n| excess of their authority, are subject to having all of their   |\r\n| activities on this system monitored and recorded by system      |\r\n| personnel.                                                      |\r\n|                                                                 |\r\n| In the course of monitoring individuals improperly using this   |\r\n| system, or in the course of system maintenance, the activities  |\r\n| of authorized users may also be monitored.                    |\r\n|                                                                 |\r\n| Anyone using this system expressly consents to such monitoring  |\r\n| and is advised that if such monitoring reveals possible         |\r\n| evidence of criminal activity, system personnel may provide the |\r\n| evidence of such monitoring to law enforcement officials.       |\r\n|-----------------------------------------------------------------|\r\n\r\nPassword: " (spawn_id exp4) match glob pattern "assword"? yes
expect: set expect_out(0,string) "assword"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\r\n|-----------------------------------------------------------------|\r\n| This system is for the use of authorized users only.            |\r\n| Individuals using this computer system without authority, or in |\r\n| excess of their authority, are subject to having all of their   |\r\n| activities on this system monitored and recorded by system      |\r\n| personnel.                                                      |\r\n| |\r\n| In the course of monitoring individuals improperly using this   |\r\n| system, or in the course of system maintenance, the activities  |\r\n| of authorized users may also be monitored.                      |\r\n|                                                                 |\r\n| Anyone using this system expressly consents to such monitoring  |\r\n| and is advised that if such monitoring reveals possible         |\r\n| evidence of criminal activity, system personnel may provide the |\r\n| evidence of such monitoring to law enforcement officials.       |\r\n|-----------------------------------------------------------------|\r\n\r\nPassword"
send: sending "bla\r" to { exp4 }

expect: does ": " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no


expect: does ": \r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
Last login: Wed Mar 12 15:42:34 2014 from rn2sosas601

expect: does ": \r\nLast login: Wed Mar 12 15:42:34 2014 from rn2sosas601\r\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
You have new mail.

expect: does ": \r\nLast login: Wed Mar 12 15:42:34 2014 from rn2sosas601\r\r\nYou have new mail.\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
ranosusr@rn2osscs603>
expect: does ": \r\nLast login: Wed Mar 12 15:42:34 2014 from rn2sosas601\r\r\nYou have new mail.\r\nranosusr@rn2osscs603> " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? yes
expect: set expect_out(0,string) "ranosusr@rn2osscs603"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) ": \r\nLast login: Wed Mar 12 15:42:34 2014 from rn2sosas601\r\r\nYou have new mail.\r\nranosusr@rn2osscs603"
send: sending "cd /var/opt/bla/edd/ARNE_SIU \r" to { exp4 }

expect: does "> " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
cd /var/opt/e
expect: does "> cd /var/opt/e" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
ricsson/edd
expect: does "> cd /var/opt/bla/edd" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
/ARNE_SIU

expect: does "> cd /var/opt/bla/edd/ARNE_SIU \r\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
ranosusr@rn2osscs603>
expect: does "> cd /var/opt/bla/edd/ARNE_SIU \r\r\nranosusr@rn2osscs603> " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? yes
expect: set expect_out(0,string) "ranosusr@rn2osscs603"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "> cd /var/opt/bla/edd/ARNE_SIU \r\r\nranosusr@rn2osscs603"
send: sending "grep -il FRTAMX *\r" to { exp4 }

expect: does "> " (spawn_id exp4) match regular expression "(FRTAMX\S+\.xml).*ranosusr@rn2osscs603> $"? no
grep -il FRTAMX *

expect: does "> grep -il FRTAMX *\r\r\n" (spawn_id exp4) match regular expression "(FRTAMX\S+\.xml).*ranosusr@rn2osscs603> $"? no
FXL704_FRTAMX_SIU_ARNE.xml
FXL704_FRTAMX_SIU_ARNE_upgraded_temp.xml

expect: does "> grep -il FRTAMX *\r\r\nFXL704_FRTAMX_SIU_ARNE.xml\r\nFXL704_FRTAMX_SIU_ARNE_upgraded_temp.xml\r\n" (spawn_id exp4) match regular expression "(FRTAMX\S+\.xml).*ranosusr@rn2osscs603> $"? no
ranosusr@rn2osscs603>
expect: does "> grep -il FRTAMX *\r\r\nFXL704_FRTAMX_SIU_ARNE.xml\r\nFXL704_FRTAMX_SIU_ARNE_upgraded_temp.xml\r\nranosusr@rn2osscs603> " (spawn_id exp4) match regular expression "(FRTAMX\S+\.xml).*ranosusr@rn2osscs603> $"? yes
expect: set expect_out(0,string) "FRTAMX_SIU_ARNE.xml\r\nFXL704_FRTAMX_SIU_ARNE_upgraded_temp.xml\r\nranosusr@rn2osscs603> "
expect: set expect_out(1,string) "FRTAMX_SIU_ARNE.xml"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "> grep -il FRTAMX *\r\r\nFXL704_FRTAMX_SIU_ARNE.xml\r\nFXL704_FRTAMX_SIU_ARNE_upgraded_temp.xml\r\nranosusr@rn2osscs603> "
send: sending "/opt/bla/arne/bin/import.sh -f  -val:rall\r" to { exp4 }

expect: does "" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
/opt/bla/arne/bin/import.sh
expect: does "/opt/bla/arne/bin/import.sh" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
 -f  -val:rall

expect: does "/opt/bla/arne/bin/import.sh -f  -val:rall\r\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
Logging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:36

expect: does "/opt/bla/arne/bin/import.sh -f  -val:rall\r\r\nLogging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:36\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
Failed to write to file. Writing to stdout instead.
The file '-val:rall' can not be found.

expect: does "/opt/bla/arne/bin/import.sh -f  -val:rall\r\r\nLogging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:36\r\nFailed to write to file. Writing to stdout instead.\r\nThe file '-val:rall' can not be found.\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
ranosusr@rn2osscs603>
expect: does "/opt/bla/arne/bin/import.sh -f  -val:rall\r\r\nLogging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:36\r\nFailed to write to file. Writing to stdout instead.\r\nThe file '-val:rall' can not be found.\r\nranosusr@rn2osscs603> " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? yes
expect: set expect_out(0,string) "ranosusr@rn2osscs603"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "/opt/bla/arne/bin/import.sh -f  -val:rall\r\r\nLogging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:36\r\nFailed to write to file. Writing to stdout instead.\r\nThe file '-val:rall' can not be found.\r\nranosusr@rn2osscs603"
send: sending "/opt/bla/arne/bin/import.sh -f  -import\r" to { exp4 }

expect: does "> " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
/opt/ericsso
expect: does "> /opt/ericsso" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
n/arn
expect: does "> /opt/bla/arn" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
e/bin/import.sh -f
expect: does "> /opt/bla/arne/bin/import.sh -f" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
  -import
expect: does "> /opt/bla/arne/bin/import.sh -f  -import" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no


expect: does "> /opt/bla/arne/bin/import.sh -f  -import\r\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
Logging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:40

expect: does "> /opt/bla/arne/bin/import.sh -f  -import\r\r\nLogging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:40\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
Failed to write to file. Writing to stdout instead.
The file '-import' can not be found.

expect: does "> /opt/bla/arne/bin/import.sh -f  -import\r\r\nLogging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:40\r\nFailed to write to file. Writing to stdout instead.\r\nThe file '-import' can not be found.\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
ranosusr@rn2osscs603>
expect: does "> /opt/bla/arne/bin/import.sh -f  -import\r\r\nLogging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:40\r\nFailed to write to file. Writing to stdout instead.\r\nThe file '-import' can not be found.\r\nranosusr@rn2osscs603> " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? yes
expect: set expect_out(0,string) "ranosusr@rn2osscs603"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "> /opt/bla/arne/bin/import.sh -f  -import\r\r\nLogging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-12_15:43:40\r\nFailed to write to file. Writing to stdout instead.\r\nThe file '-import' can not be found.\r\nranosusr@rn2osscs603"
send: sending "cat FRTAMX_SIU_ARNE.xml | grep {1,3}.{1,3}.{1,3}.{1,3} \r" to { exp4 }
couldn't compile regular expression pattern: quantifier operand invalid
    while executing
"expect -re "{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}" *$prompt$""
    (file "siu1" line 32)
ranosusr@rn2sosas601>

sory for the bigouput,但这是我用exp_internal 1得到的。所以问题是grepin Ip地址,我在代码中禁用的一些命令,要更快,直到我管理ipaddress。感谢

0 个答案:

没有答案