getting log from telnet session and writing relevant info to file but line gets overwritten by timestamp variable in expect script - expect

Here is a sample remote session that I am automating
[sample-session]
$telnet 172.16.11.2
Trying 172.16.11.2...
Connected to 172.16.11.2.
Escape character is '^]'.
BCM96858 Broadband Router
Login: user
Password:
> voice show
Global Parameters:
------------------
BoundIfName : bronu8.2501
IP address family : IPv4
BoundIpAddr : 172.16.11.2
Voice Log Levels : general=3,cmgr=3,disp=3,sipcctk=3,bos=3,ept=3,cms=3,prov=3,lhapi=3,istw=3,dsphal=3,slicslac=3
Management Protocol : OMCI
Voice DNS Server Enable : Off
Voice DNS Server Primary : 0.0.0.0
Voice DNS Server Secondary : 0.0.0.0
Service Provider 0:
--------------------
Associated Voice Profile: 1
Locale : IND
DTMFMethod : InBand
HookFlashMethod : None
DigitMap : x+T
Log Server Addr : 0.0.0.0
Log Server Port : 0
T38 : on
V18 : on
RTPDSCPMark : 46
European flash : Off
SIP:
SIP mode : RFC3261
Domain :
Port : 5060
Transport : UDP
SIP URI for TLS calls: Off
RegExpires : 300
RegRetryInterval : 60
DSCPMark : 46
Registrar Addr : 172.16.11.201
Registrar Port : 5060
Proxy Addr : 172.16.11.201
Proxy Port : 5060
OutBoundProxy Addr : 172.16.11.201
OutBoundProxy Port : 5060
Music Server Addr : 0.0.0.0
Music Server Port : 0
Conferencing URI :
Conferencing Option : Local
Failover Enable : Off
Sip OPTIONS Enable : Off
Back-to-primary cfg : Disabled
Secondary Domain :
Secondary Registrar Addr : 0.0.0.0
Secondary Registrar Port : 5060
Secondary Proxy Addr : 0.0.0.0
Secondary Proxy Port : 5060
Secondary Outbound Proxy Addr : 0.0.0.0
Secondary Outbound Proxy Port : 5060
To Tag Matching : On
Timer B ( in ms ) : 32000
Timer F ( in ms ) : 32000
SRTP Usage Option : Disabled
Account 0:
-----------
ActivationStatus : Enabled
VoipServiceStatus : Up
CallStatus : Idle
Associated LineInst : 1
PhysEndpt : 0
Extension : 122
DisplayName :
AuthName : 122
AuthPwd : 0000
TxGain : 0 dB
RxGain : 0 dB
CALLFEATURES:
MWI : off
Caller ID number : on
Caller ID name : on
CallWaiting : on
CFWDNum :
CallFwdAll : off
CallFwdBusy : off
CallFwdNoans : off
AnonymousOutgoingCall: off
AnonymousCallRcvBlock: off
DoNotDisturb : off
CallCompOnBusy : off
SpeedDial : off
WarmLine : off
WarmLineNum :
CallBarring : off
CallBarringMode : None
CallBarringPin : 9999
CallBarringDigitMap :
NetPrivacy : on
CODECSETTINGS:
VAD : on
pTime : 20
CodecList : (0) G.711MuLaw
(1) G.711ALaw
(2) G.723.1
(3) G.726_16
(4) G.726_24
(5) G.726_32
Account 1:
-----------
ActivationStatus : Enabled
VoipServiceStatus : Up
CallStatus : Idle
Associated LineInst : 2
PhysEndpt : 1
Extension : 121
DisplayName :
AuthName : 121
AuthPwd : 0000
TxGain : 0 dB
RxGain : 0 dB
CALLFEATURES:
MWI : off
Caller ID number : on
Caller ID name : on
CallWaiting : on
CFWDNum :
CallFwdAll : off
CallFwdBusy : off
CallFwdNoans : off
AnonymousOutgoingCall: off
AnonymousCallRcvBlock: off
DoNotDisturb : off
CallCompOnBusy : off
SpeedDial : off
WarmLine : off
WarmLineNum :
CallBarring : off
CallBarringMode : None
CallBarringPin : 9999
CallBarringDigitMap :
NetPrivacy : on
CODECSETTINGS:
VAD : on
pTime : 20
CodecList : (0) G.711MuLaw
(1) G.711ALaw
(2) G.723.1
(3) G.726_16
(4) G.726_24
(5) G.726_32
> exit
Bye bye. Have a nice day!!!
Connection closed by foreign host.
[/sample-session]
My aim is to obtain lines containing ActivationStatus and VoipServiceStatus.
So I wrote this script
#!/usr/bin/expect -f
set timeout 2
set ip_addr [lindex $argv 0]
set OUTFILE "voip_status.log"
spawn telnet 172.16.11.$ip_addr
expect {
timeout { send_user "\nFailed to get login prompt for $ip_addr\n"; exit 1 }
eof { send_user "\ntelnet failure for server $ip_addr\n"; exit 2 }
"*Login:"
}
#expect "*Login:"
send -- "user\r"
expect "*Password:"
send -- "user\r"
expect ">"
send -- "voice show\r"
expect ">"
set voice_full_output $expect_out(buffer)
set voice_stat_lines [ split $voice_full_output "\n" ]
foreach line $voice_stat_lines {
if {[string match "*VoipServiceStatus*" $line]} {
set timestamp [timestamp -format %Y-%m-%d_%H:%M:%S]
set chan [open $OUTFILE a+]
#puts "ok - MATCH $line"
puts $chan "$line $timestamp"
close $chan
}
if {[string match "*ActivationStatus*" $line]} {
set chan [open $OUTFILE a+]
#puts "ok - MATCH $line"
puts $chan "$line"
close $chan
}
}
send -- "exit\r"
expect eof
Here is my output
$ cat voip_status.log
ActivationStatus : Enabled
2021-03-17_11:32:35tus : Up
ActivationStatus : Enabled
2021-03-17_11:32:35tus : Up
As you can see that time stamp has overwritten the line I had filtered out.
I wanted timestamp to appear at the end of line .
Why is this happening ?
Here I made an experimental setup which anybody can run to see this behaviour.
Contents of 'output-voice-log-for-experiment.sh' follow
[file to give input]
#!/usr/bin/perl
print "
Account 0:
-----------
ActivationStatus : Enabled
VoipServiceStatus : Up
CallStatus : Idle
Associated LineInst : 1
PhysEndpt : 0
Extension : 122
DisplayName :
AuthName : 122
AuthPwd : 0000
TxGain : 0 dB
RxGain : 0 dB
CALLFEATURES:
MWI : off
Caller ID number : on
Caller ID name : on
CallWaiting : on
CFWDNum :
CallFwdAll : off
CallFwdBusy : off
CallFwdNoans : off
AnonymousOutgoingCall: off
AnonymousCallRcvBlock: off
DoNotDisturb : off
CallCompOnBusy : off
SpeedDial : off
WarmLine : off
WarmLineNum :
CallBarring : off
CallBarringMode : None
CallBarringPin : 9999
CallBarringDigitMap :
NetPrivacy : on
CODECSETTINGS:
VAD : on
pTime : 20
CodecList : (0) G.711MuLaw
(1) G.711ALaw
(2) G.723.1
(3) G.726_16
(4) G.726_24
(5) G.726_32
Account 1:
-----------
ActivationStatus : Enabled
VoipServiceStatus : Up
CallStatus : Idle
Associated LineInst : 2
PhysEndpt : 1
Extension : 121
DisplayName :
AuthName : 121
AuthPwd : 0000
TxGain : 0 dB
RxGain : 0 dB
CALLFEATURES:
MWI : off
Caller ID number : on
Caller ID name : on
CallWaiting : on
CFWDNum :
CallFwdAll : off
CallFwdBusy : off
CallFwdNoans : off
AnonymousOutgoingCall: off
AnonymousCallRcvBlock: off
DoNotDisturb : off
CallCompOnBusy : off
SpeedDial : off
WarmLine : off
WarmLineNum :
CallBarring : off
CallBarringMode : None
CallBarringPin : 9999
CallBarringDigitMap :
NetPrivacy : on
CODECSETTINGS:
VAD : on
pTime : 20
CodecList : (0) G.711MuLaw
(1) G.711ALaw
(2) G.723.1
(3) G.726_16
(4) G.726_24
(5) G.726_32
> exit
"
[/file to give input]
Contents of 'process-experimental-echo.sh' follow
[file to process input]
#!/usr/bin/expect -f
spawn ./output-voice-log-for-experiment.sh
set timeout 20
set OUTFILE "experimental_voip_status.log"
expect ">"
set voice_full_output $expect_out(buffer)
set voice_stat_lines [ split $voice_full_output "\n" ]
foreach line $voice_stat_lines {
if {[string match "*VoipServiceStatus*" $line]} {
set timestamp [timestamp -format %Y-%m-%d_%H:%M:%S]
set chan [open $OUTFILE a+]
puts "ok - MATCH $line"
puts $chan "$line $timestamp"
close $chan
}
if {[string match "*ActivationStatus*" $line]} {
set chan [open $OUTFILE a+]
puts "ok - MATCH $line"
puts $chan "$line"
close $chan
}
}
[/file to process input]
Now simply run ./process-experimental-echo.sh and see this behaviour yourself.
Here is the output file you will get
$ cat experimental_voip_status.log
ActivationStatus : Enabled
2021-03-17_12:27:11tus : Up
ActivationStatus : Enabled
2021-03-17_12:27:11tus : Up
Thanks.

If you look at the output file using cat -vet it shows the non-printing control characters, tabs, and end-of-line, and you get:
ActivationStatus : Enabled^M$
VoipServiceStatus : Up^M 2021-03-17_09:02:53$
You can see ^M in the middle of a line, which is carriage-return. On a terminal this makes the cursor go back to the start of the line, and start overprinting.
This is the typical result of spawn, which runs a command through a pty, which alters each output newlines into carriage-return and newline. This is normal and desired for a pty. You can either turn it off (see stty -onlcr), or knowing that it is likely to be there, you can remove it in your script: after the foreach ... line, trim the variable $line to remove \r:
set line [string trimright $line "\r"]

Related

Why i can't run c++ code in micropython env esp32

I am following this guide to run my C++ code in micropython environment, the normal/simple C++ code works on my ESP32 board but when i try running big loops it gives an error.
The error:
$ picocom -b 115200 /dev/ttyUSB0
picocom v3.1
port is : /dev/ttyUSB0
flowcontrol : none
baudrate is : 115200
parity is : none
databits are : 8
stopbits are : 1
escape is : C-a
local echo is : no
noinit is : no
noreset is : no
hangup is : no
nolock is : no
send_cmd is : sz -vv
receive_cmd is : rz -vv -E
imap is :
omap is :
emap is : crcrlf,delbs,
logfile is : none
initstring : none
exit_after is : not set
exit is : no
Type [C-a] [C-h] to see available commands
Terminal ready
>>> import cppexample
>>> cppexample.cppfunc()
abort() was called at PC 0x402acc26 on core 0
Backtrace:0x400912c6:0x3ffce1c0 0x40091989:0x3ffce1e0 0x400956ae:0x3ffce200 0x402acc26:0x3ffce270 0x40396985:0x3ffce290 0x40396ac5:0x3ffce2b0 0x400d3d73:0x3ffce2d0 0x400d4f53:0x3ffce390 0x402a7d26:0x3ffce3d0 0x402af51d:0x3ffce680 0x402b55f9:0x3ffce6a0 0x402b5726:0x3ffce6c0 0x402b77e1:0x3ffce6e0 0x402af6f0:0x3ffce780 0x402b55f9:0x3ffce7d0 0x402b5622:0x3ffce7f0 0x402e1847:0x3ffce810 0x402e1b99:0x3ffce8a0 0x402c69b0:0x3ffce8e0
ELF file SHA256: fe8de9466e5c428e
Rebooting...
ets Jul 29 2019 12:21:46
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4
load:0x3fff0034,len:5572
load:0x40078000,len:12696
load:0x40080400,len:4292
entry 0x40080414
MicroPython v1.16-243-g8c4ba575f-dirty on 2021-09-20; ESP32 module with ESP32
Type "help()" for more information.
>>>
I got no idea what's happening,any help is appreciated

TimeoutException when trying to connect to Azure service bus queue through informatica

I am trying to load 2k records on azure service bus queue through informatica, but getting timeout exception. The connection is working file for 700 records, successfully loading on queue.
I have created JMS and JNDI connection and is working fine if number of records are less.
Error:
2020-05-04 23:27:28 : ERROR : (3084 | WRITER_1__1) : (IS | PC_INT_EE_QA) : node01_lxinfaeeqa1 : JAVA PLUGIN_1762 : [ERROR] JMS writer encountered a JMS exception: Timed out while waiting to get credit to sendException Stack: javax.jms.JMSException: Timed out while waiting to get credit to send
at org.apache.qpid.amqp_1_0.jms.impl.MessageProducerImpl.send(MessageProducerImpl.java:331)
at com.informatica.powerconnect.jms.server.writer.JMSMessageWriter$QueueWriter.writeMessage(JMSMessageWriter.java:93)
at com.informatica.powerconnect.jms.server.writer.JMSWriterPartitionDriver.execute(JMSWriterPartitionDriver.java:401)
Linked Exception Stack: java.util.concurrent.TimeoutException
at org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint.waitUntil(ConnectionEndpoint.java:1232)
at org.apache.qpid.amqp_1_0.transport.SessionEndpoint.waitUntil(SessionEndpoint.java:686)
at org.apache.qpid.amqp_1_0.transport.LinkEndpoint.waitUntil(LinkEndpoint.java:360)
at org.apache.qpid.amqp_1_0.client.Sender.send(Sender.java:320)
at org.apache.qpid.amqp_1_0.jms.impl.MessageProducerImpl.send(MessageProducerImpl.java:321)
at com.informatica.powerconnect.jms.server.writer.JMSMessageWriter$QueueWriter.writeMessage(JMSMessageWriter.java:93)
at com.informatica.powerconnect.jms.server.writer.JMSWriterPartitionDriver.execute(JMSWriterPartitionDriver.java:401)
.
2020-05-04 23:27:28 : ERROR : (3084 | WRITER_1__1) : (IS | PC_INT_EE_QA) : node01_lxinfaeeqa1 : JAVA PLUGIN_1762 : [ERROR] at com.informatica.powerconnect.jms.server.writer.JMSWriterPartitionDriver.execute(JMSWriterPartitionDriver.java:431)
2020-05-04 23:27:28 : ERROR : (3084 | WRITER_1__1) : (IS | PC_INT_EE_QA) : node01_lxinfaeeqa1 : SDKS_38502 : Plug-in #300800's target [Target_jms: Partition 1] failed in method [execute].
2020-05-04 23:27:28 : INFO : (3084 | WRITER_1__1) : (IS | PC_INT_EE_QA) : node01_lxinfaeeqa1 : WRT_8333 : Rolling back all the targets due to fatal session error.
2020-05-04 23:28:28 : INFO : (3084 | WRITER_1__1) : (IS | PC_INT_EE_QA) : node01_lxinfaeeqa1 : WRT_8325 : Final rollback executed for the target [Target_jms] at end of load
2020-05-04 23:28:28 : ERROR : (3084 | WRITER_1__1) : (IS | PC_INT_EE_QA) : node01_lxinfaeeqa1 : WRT_8081 : Writer run terminated. [Error in loading data to target table [Target_jms: Partition 1]]
2020-05-04 23:28:28 : INFO : (3084 | WRITER_1__1) : (IS | PC_INT_EE_QA) : node01_lxinfaeeqa1 : WRT_8168 : End loading table [Target_jms: Partition 1] at: Mon May 04 13:58:28 2020
2020-05-04 23:28:28 : INFO : (3084 | WRITER_1__1) : (IS | PC_INT_EE_QA) : node01_lxinfaeeqa1 : WRT_8035 : Load complete time: Mon May 04 13:58:28 2020
Appreciate the help.
The queue size for azure was 1GB but it seems that Azure service bus queue can only accept 100 messages in one transaction. I was able to solve the issue by changing the properties at informatica session level.
Commit type to "Target"
commit interval to "100"
And in target properties kept the JMS priority to 9.

Why my variables are not changing ? Bash Script

#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
Bokoblin_HP=30
Bokoblin_STR=5
Link_HP=60
Link_STR=10
while [ $Bokoblin_HP -ne 0 ]
echo "Bokblin HP : ${Bokoblin_HP}/30"
do
read -p "Press A to Attack or Press H to Heal : " action
if [ $action = "A" ]
then
((Bokoblin_HP=$Bokoblin_HP-$Link_STR))
echo $Bokoblin_HP
fi
done
Result :
Bokoblin HP : 30/30
Press A to Attack or Press H to Heal : A
30
I'm doing an operation on Bokoblin_HP and I would like to stop the program when it reach 0, but my variable won't change and still 30.
Just swap two lines make the program work.
Also, be sure to type an upper A
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
Bokoblin_HP=30
Bokoblin_STR=5
Link_HP=60
Link_STR=10
while [ $Bokoblin_HP -ne 0 ]
do
echo "Bokblin HP : ${Bokoblin_HP}/30"
read -p "Press A to Attack or Press H to Heal : " action
if [ $action = "A" ]
then
((Bokoblin_HP=$Bokoblin_HP-$Link_STR))
echo $Bokoblin_HP
fi
done
Output :
<user>#<machine> ~
└─ $ ./test.sh
Bokblin HP : 30/30
Press A to Attack or Press H to Heal : A
20
Bokblin HP : 20/30
Press A to Attack or Press H to Heal : A
10
Bokblin HP : 10/30
Press A to Attack or Press H to Heal : A
0
<user>#<machine> ~
Idk how is supposed to work your solution, im using this
Bokoblin_HP=$(($Bokoblin_HP-$Link_STR))

Powershell: get DHCP status with Get-NetIPConfiguration?

I'm a powershell newbie.
If I run Get-NetIPConfiguration -Detailed I can see the IPv4 DHCP status. This is a good start - took me a lot of googling to get this far:
PS C:\Windows\system32> Get-NetIPConfiguration -Detailed
ComputerName : VM-172-26-39-24
InterfaceAlias : Ethernet
InterfaceIndex : 6
InterfaceDescription : Red Hat VirtIO Ethernet Adapter
NetCompartment.CompartmentId : 1
NetCompartment.CompartmentDescription : Default Compartment
NetAdapter.LinkLayerAddress : FA-16-3E-FF-0D-60
NetAdapter.Status : Up
NetProfile.Name : Network
NetProfile.NetworkCategory : Public
NetProfile.IPv6Connectivity : NoTraffic
NetProfile.IPv4Connectivity : Internet
IPv6LinkLocalAddress : fe80::x:x:x:x
IPv4Address : 172.26.39.249
IPv6DefaultGateway :
IPv4DefaultGateway : 172.26.36.1
NetIPv6Interface.NlMTU : 9000
NetIPv4Interface.NlMTU : 9000
NetIPv6Interface.DHCP : Enabled
NetIPv4Interface.DHCP : Disabled
DNSServer : x.x.x.x
x.x.x.x
But if I run something like
Get-NetIPConfiguration -Detailed | select InterfaceAlias,NetIPv4Interface.DHCP
I get an empty field?
PS C:\Windows\system32> Get-NetIPConfiguration -Detailed | `
select InterfaceAlias, NetIPv4Interface.DHCP
InterfaceAlias NetIPv4Interface.DHCP
-------------- ---------------------
Ethernet
Am I doing something wrong?
If I remove the trailing .DHCP ("what's in there?") I see nothing recognisable
PS C:\Windows\system32> Get-NetIPConfiguration -Detailed | select InterfaceAlias, NetIPv4Interface
InterfaceAlias NetIPv4Interface
-------------- ----------------
Ethernet MSFT_NetIPInterface (Name = "#55?55;", CreationClassName = "", SystemCreationClassName = "", SystemName = "")
You need to calculate the sub property when used in a Select statement:
Get-NetIPConfiguration -Detailed | `
select InterfaceAlias, #{N="DHCP";E={$_.NetIPv4Interface.DHCP}}
Or try this:
Get-NetIPConfiguration | Select -ExpandProperty NetIPv4Interface | Select InterfaceAlias,DHCP
...or you could just retrieve the config, put it in a variable then get the desired sub-properties:
$Config = Get-NetIPConfiguration
$Config | forEach {
[pscustomobject]#{InterFaceAlias=$_.InterFaceAlias;DHCP=$_.NetIPv4Interface.DHCP}
}

Match strings after multiline pattern of texts

I need to extract the value of "Manage VLAN" from the below output with regular expressions and store the value inside a variable to be used later in my script.
set switch 100.10.100.100
set Username "test"
set Password "test"
spawn ssh -o StrictHostKeyChecking=no $Username#$switch
expect "*assword: "
send "$Password\r"
expect *>
send "enable\r"
expect "*#"
send "config\r"
expect "(config)#"
send "display something"
The output will be:
status : Connected
IP Address : 2.2.2.2
Mask : 255.255.255.255
Gateway : 2.2.2.1
Manage VLAN : 456
Manage priority : 0
Option60 : No
Switch : Enable
How might I achieve this?
See example:
expect1.1> spawn bash -c "echo vlan : 1234"
spawn bash -c echo vlan : 1234
22902
expect1.2> expect -re {vlan *: *([0-9]+)}
vlan : 1234
expect1.4> set vlanid $expect_out(1,string)
1234
expect1.5> puts $vlanid
1234
expect1.6>

Resources