Expect script reads from file and puts everything on the same line - expect

Expect script should read commands from file and send them line by line through telnet, but it puts everything on the same line
I have file with commands and my task is to connect to telnet, read command from file and send it. I have to do it for all commands in file. I prepared script, but it puts all commands on the same line and send them as one.
Here is content of file with commands (those are just test commands)
add chain=forward src-address=1.1.1.1 action=log;
add chain=forward src-address=1.1.1.1 action=log;
add chain=forward src-address=1.1.1.1 action=log;
add chain=forward src-address=2.2.2.2 action=log;
add chain=forward src-address=2.2.2.2 action=log;
add chain=forward src-address=2.2.2.2 action=log;
add chain=forward src-address=2.2.2.2 action=log;
add chain=forward src-address=2.2.2.2 action=log;
add chain=forward src-address=3.3.3.3 action=log;
add chain=forward src-address=3.3.3.3 action=log;
add chain=forward src-address=3.3.3.3 action=log;
and here is script I am using
set timeout 1
set fid [open /home/toor/file.txt]
set content [read $fid]
close $fid
###
start telnet and login
###
set records [split $content "\r"]
foreach record $records {
lassign $records \
commands
expect "> "
send "$commands\r"
}
sleep 1
expect "> "
send "quit\r"
Any advice, which help me to send every command separately would really help me and I will be very grateful to you

Your commands file probably has \n line endings not \r
set fid [open /home/toor/file.txt]
set records [split [read -nonewline $fid] "\n"]
# ..........................................^
close $fid
Your lassign command has no purpose. You can just send "$record\r"

Related

expect script capturing garbage characters - need to remove

I have a switch I'm trying to write to grab output from a hp switch. The source machine is an ubuntu 18.04. The code version on the switch is 16.04. I tried changing the terminal settings on the switch from the default of vty100 to ansi and none but no difference. How do I get rid of the extra special characters show below?
set timeout 20
spawn ssh -l manager 192.168.10.10
expect "manager#192.168.10.10's password:"
send "admin\n"
expect "Press any key to continue"
send "j\n"
log_file -a hp.log
send "show vlan 10\n"
expect "labswitch#"
send "conf\n"
send "hostname newswitch\n"
expect "newswitch#"
send "exit\n"
expect "newswitch#"
send "logout\n"
I have this extra output of characters through each part of the script in the log file and the console. Need to remove these characters from output. ??
^[[?6l^[[1;30r^[[?7h^[[2J^[[1;1H^[[1920;1920H^[[6n^[[1;1HYour previous successful login (as manager) was on 2019-03-07 $ from 192.168.10.4
^[[1;30r^[[30;1H^[[30;1H^[[2K^[[30;1H^[[?25h^[[30;1H^[[30;1labswitch# ^[[30;1H^[[30;11H^[[30;1H^[[?25h^[[30;11H^[[1;0H^$ Status and Counters - VLAN Information - VLAN 10
VLAN ID : 10
Name : Server_Lab
Status : Port-based
Voice : No
Jumbo : Yes
Private VLAN : none
Associated Primary VID : none
Associated Secondary VIDs : none
Port Information Mode Unknown VLAN Status
---------------- -------- ------------ ----------
11 Tagged Learn Up
12 Tagged Learn Up
^[[1;30r^[[30;1H^[[30;1H^[[2K^[[30;1H^[[?25h^[[30;1H^[[30;1H
Those are ANSI Escape sequences
You should be able to strip them out of the log file with:
send "logout\r" ;# idiomatically, use \r to "hit enter"
log_file ;# turn off logging
# read the log file
set fid [open hp.log r]
set contents [read -nonewline $fid]
close $fid
# remove the escape sequences
regsub -all {\033\[(?:\?|\d+;)?\d+[[:alpha:]]} $contents {} stripped
# write the new contents
set fid [open hp.log w]
puts $fid $stripped
close $fid

expect telnet - command not executing

I am struggling with executing command via telnet/expect.
set send_slow {500 .5}
send -s -- "show slot *\r"
expect {
".*>" {
send -s -- "y\r"
exp_continue
}
".*#\s"
}
send -s "who\r"
expect "# "
send -s "alm\r"
expect "# "
send -s -- "logout\r"
show slot command prints card in slots. Due to paging user is asked to continue. After execution that a prompt NODE2-1# is shown and I want to execute command who.
What I get is:
2/36 PF Empty Up Down UEQ
2/37 FAN FAN Up Up
2/38 Empty Empty Down Down UAS UEQ
2/39 Empty Empty Down Down UAS UEQ
25/1 SFD40 SFD40 Up Up
Node2-1#<br> Node2-1# who
Session Username Date Terminal
-------------------------------------------------------------------------------
116 (cli tel) * admin May 29 06:57 XX.X.XX.XX
Node2-1# almlogout
Alarm Status: Critical-3 Major-0 Minor-0
As you can see, first prompt is left empty and in second one there is a command. It takes few seconds to execute it.
Moreover, below one can see, that in prompt there is pasted command almlogout. These two are separate commands -> alm and logout. However, they are pasted together.
Question is - how to execute command in prompt without that delay, and second - how to separate two commands
By default, expect statement's pattern is in glob style so ".*>" should be -re ".*>" and ".*#\s" should be -re ".*#\\s" or -re {.*#\s} ("\s" is actually "s").
And since .* can match nothing, so -re {.*>} is the same as -re {>} and -re {.*#\s} the same as -re {#\s}.

How to grab 'show tech' with bash and expect?

I'm using 2 scripts. A bash and an expect script. the bash is just a for loop with a set of IPs. If I can, I would use the expect script to create a variable near the beginning, right after login. It would look for the hostname, assign it to a variable, then use that variable as the 'match' after show tech output.
Also, the IPs are example. I'm not actually expecting (no pun intended) this to operate on public DNS IPs. Ok ok, you got me, I put this disclaimer here just so I could 'not' make that pun.
#!/bin/bash
arrayHOST+=( '8.8.8.8' '8.8.4.4' '4.2.2.2' '4.2.2.1' )
username="user1"; password="pass1"
for host in ${arrayHOST[#]}; do
./expect.sh $host $username $password >> $host.txt
done
and the expect script:
#!/usr/bin/expect -f
#log_user 0
set timeout -1
set varIP1 [lindex $argv 0]
set varUSER [lindex $argv 1]
set varPASS [lindex $argv 2]
spawn telnet $varIP1
expect "Username:"
send "$varUSER\r"
expect "Password:"
send "$varPASS\n"
expect "#"
send "term leng 0\r"
expect "#"
send "term wid 0\r"
expect "#"
send "show tech\r"
expect "#"
puts $expect_out(buffer);
Problem is that there are '#' characters that match before the command finishes so I don't get to capture the whole thing. Points to consider:
The end of the output from 'show tech' isn't the same on every device in the IP list unless you consider the hostname of the device itself.
Yes, I know I can "show tech | redirect tftp:// etc". This wont help me achieve the desired results.
I've tried 'not expecting anything' but this causes expect to not capture anything. (sending show tech without following with a new line containing expect "#" or anything else)
spawn telnet 8.8.8.8
Trying 8.8.8.8...
Connected to 8.8.8.8.
Escape character is '^]'.
User Access Verification
Username: benjamin
Password:
rowtar#term leng 0
rowtar#term wid 0
rowtar#term wid 0
rowtar#
I really like the idea of trying to regex/match an expect variable to the output that matches the hostname of the device but I don't how to do that. In my example, I would try to match "rowtar#" but without per-defining that ahead of time or before the expect script is called.
The solution was looking me right in the face.
Simply:
expect -re #$
matching any line ending with #
I still encourage someone to help/answer with hostname matching to a variable

Change standard separator comma for attachments

I know that comma will be used to separate files and other stuff with Blat mailer.
But I must send files with a comma in their filenames and have no possibility to change this circumstance.
Is there a way to change the standard separator?
Or any other way without changing the filename?
Here my example what I try to do:
set SERVER=127.0.0.1:2525
set USER=sendout#ralfbb-test.local
set PW=secret
set FROMNAME=sendout#ralfbb-test.local
set TO=nurmail1#ralfbb-test.local
set SUBJECT="Subject test - attached file!"
set BODY="Oh yes my dear lad!"
set BODYF="c:\Mailbody.txt"
set ATTACH="c:\517-BS24430-210416-NULL-0,750.pdf"
blat -server %SERVER% -f %FROMNAME% -u %USER% -pw %PW% -to %TO% -subject %SUBJECT% -bodyF %BODYF% -attach %ATTACH% -debug -log blat.log
Add a "" when calling the variable:
"%ATTACH%"

Why 'Expect' is not sending the full command?

I want to configure default route in a server via below code. My issue here is that the Expect is not sending full route command. Looks like the script is truncating the command.
It should send route add default gw 5.66.0.1 but it is sending route add defaul
I am not sure why this happening.
Code
puts "Sending route add default gw $cv_chassis_gw_ip_1 command"
send -- "route add default gw $cv_chassis_gw_ip_1\r"
sleep 1
expect {
-timeout $cv_cmd_timeout
-re "route.*#" { }
timeout {
puts "Timeout happend in getting # prompt"
}
}
sleep 1
Debug Output
Sending route add default gw 5.66.0.1 command
send: sending "route add default gw 5.66.0.1\r" to { exp9 }
expect: does "\n" (spawn_id exp9) match regular expression "route.*#"? no
sh-4.0#
sh-4.0# route add defaul
expect: does "\nsh-4.0# \r\nsh-4.0# route add defaul" (spawn_id exp9) match regular expression "route.*#"? no
Thanks Dinesh.
Actually its server issue,server is not able to read a command fully sometime,i tried doing copy pasting a same command and found that sometime it is not taking full command.
so we can close this thread.

Resources