Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Openning a file with current date & time appended

Posted by raees (raees), 7 August 2007
I am using a simple script as follows to remote login through telnet and capture command output and then save the output in a file using expect_out(buffer)

I want to open file with current date & time appended to it

#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
package require Expect

spawn telnet x.x.x.x
expect "login:"
exp_send "username\r"
expect "Password:"
exp_send "passwd\r"
expect ">"
exp_send "showdata\r"
expect "Endofdata"
set var [open filename.txt a]
puts $var $expect_out(buffer)
exit

The desired name of the file is as follows
<filename070807223000.txt>
with date&time appended to the filename in the format (yymmdd.hhmmss)

Please guide me how to use the date/clock command to meet the requirement

Regards


Posted by admin (Graham Ellis), 7 August 2007
% clock format [clock seconds] -format filename%d%m%y%H%M%S.txt
filename070807182925.txt
%

Posted by yzhang (yzhang), 3 September 2007
Here is my file that i want to test:

MANAGEMENT MATRIX CONFIGURATION MENU
------------------------------------

(A)  Duplicate Active Configuration
(B)  System Configuration
(C)  Host Configuration
(D)  Cluster Configuration
(E)  Management Matrix Configuration Report
(Q)  Quit -- Forget Changes
(X)  Exit -- Save Changes
COMMAND (A/B/C/D/E/Q/X): b
.

And here is my script:

#!/usr/local/bin/expect
set host [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set cmdfile [lindex $argv 3]
set resultfile [lindex $argv 4]

set timeout 60
if { $argc != 5 } {
puts "Usage: ./console [host] [user] [passwd] [cmdfile] [resultfile]"
exit 1
}

proc start { } {
global cmdfile
global resultfile
set readcmdfile [open $cmdfile r]
set readresultfile [open $resultfile r]
set n 0
while { ![eof $readcmdfile] } {
set readcmdfile [open $cmdfile r]
set readresultfile [open $resultfile r]
set n 0
while { ![eof $readcmdfile] } {
gets $readcmdfile line1
incr n
send "$line1\r"
while { ![eof $readresultfile] } {
   gets $readresultfile line2

switch $line2 {
    "@@@@@@@@@@" {           break
}
default {
      expect -indices{
      "$line2" {}
      timeout {
      send_user "\n$cmdfile Line $n Option $line1 Test Failed \n"
      exit 1
}
}
}
}
}
}
puts "\r"
puts "\n$cmdfile Test Passed \n"
close $readcmdfile
close $readresultfile
}
spawn telnet $host
expect "login:"
send "$user\r"
expect "Password:"
send "$passwd\r"
expect {
 "Starting Console" {
 send "\r"
 expect {
 "MAIN MENU" {start}
  timeout {
  send_user "\nLOGIN $user FAILED !\n"
  exit 1
   }
   }
   }
  "Login incorrect" {
  send_user "\nLOGIN $user FAILED !\n"
  exit 1
   }
   }


And the script will go wrong by following prompt:

bad flag "-indices{": must be -glob, -regexp, -exact, -notransfer, -nocase, -i, -indices, -iread
   while executing
"expect -indices{"t" {
   ("default" arm line 2)r FAILED !\n"
   invoked from within
"switch $line2 {
    "@@@@@@@@@@" {
    break
}
default {
      expect -indices{
      "$line2" {}
      timeout {
      send_user "\n$cmdfile Li..."
   (procedure "start" line 14)
   invoked from within
"start"
   invoked from within
"expect -nobrace {MAIN MENU} start timeout {
  send_user "\nLOGIN $user FAILED !\n"
  exit 1
   }"
   invoked from within
"expect {
 "MAIN MENU" {start}
  timeout {
  send_user "\nLOGIN $user FAILED !\n"
  exit 1
   }
   }"
   invoked from within
"expect -nobrace {Starting Console} {
 send "\r"
 expect {
 "MAIN MENU" {start}
  timeout {
  send_user "\nLOGIN $user FAILED !\n"
  exit 1
   ..."
   invoked from within
"expect {
 "Starting Console" {
 send "\r"
 expect {
 "MAIN MENU" {start}
  timeout {
  send_user "\nLOGIN $user FAILED !\n"
  exit 1
   }
..."
   (file "./console_new" line 53)


I think there maybe sth wrong with the parameter of the expect and how can i avoid this kind of problem?

Thank you in advance!

Posted by admin (Graham Ellis), 3 September 2007
You have written

expect -indices{

when you should have written

expect -indices {

The difference is "only" a space character, but that's vital in Tcl as a separator between parameters.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho