Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

expect/ssh/send problems

Help
tim lloyd
2009-02-19
2013-05-02
  • tim lloyd
    tim lloyd
    2009-02-19

    I have been given a script at work to look at. This script was begrudgingly sent by partners overseas and the level of assistance is minimal. The idea is to flash some data from a PC across to multiple devices connected via a switch (rather than pay our partners to carry out the work, hence the level of assistance).

    I have had a few problems but this is more related to the setup we have here which may not replicate the overseas setup (I have a centos 5 box – no problem there but some of the directories required are not explained very well). Anyway, I have got over most of the problems and automated the data transfer. At the end of the script though I need to compare the md5sum and this is timing out.

    There are 2 scripts, the first gets all the IP addresses available on the system. This invokes another script via  “expect scp_kopy.exp”.

    The scp_kopy script copies (via “spawn scp”) a file across to the other device. After this we want to check the md5sum of the original file against the one just copied so it connects with “spawn ssh”. All good. At this point I try to initiate some "send" commands and this just does not seem to work. The “echo 0” command seems to have no effect. The md5sum command times out

    I have attached a section of code to this message. Can anyone see any obvious problems with this code? I should point out that the only code I have changed is the timeout periods. I have adapted our system to meet the rest of the code.

    CODE BEGINS vvvvv

    set timeout 60
    spawn ssh root@$ip_addr

    #Older TS100 use "password", 'p' lowercase:
    expect "Password" {
          send $root_password
        } timeout {
          puts $file_out "FLASH FAILED IP:$ip_addr  MAC:$mac_addr  SSH timeout."
          puts "ssh process timed out for IP:$ip_addr  MAC:$mac_addr"
          close $file_out
          exit
        }

    # Erase the FLASH configuration files so on next boot-up it will use the zImage configuration files.
    send "echo 0 > /proc/flash/script\r"
    set timeout 180

    set timeout 20
    # Compare MD5 checksum?
    send "md5sum -b /proc/flash/zImage\r"
    set timeout 180

    expect "$md5sum" {
          puts "process md5 ok for IP:$ip_addr  MAC:$mac_addr"
        } timeout {
          send "exit\r"
          puts "process md5 timed out for IP:$ip_addr  MAC:$mac_addr"
          puts $file_out "FLASH FAILED IP:$ip_addr  MAC:$mac_addr  MD5 checksum error/timeout."
          close $file_out
          exit
        }

     
    • ender
      ender
      2009-02-19

      Are you watching what happens when you run the script and can you see it send the md5sum command?

      You may want to try turning on debugging so you can see exactly what expect is "expecting" and what it is getting. It will show you something like

      does "this is what the spawned process is sending" match "match this" no

      Put

      exp_internal -f /logfile 0

      before the parts you want to test.

      Also, in this block
      <bold>send "echo 0 > /proc/flash/script\r"
      set timeout 180

      set timeout 20
      # Compare MD5 checksum?
      send "md5sum -b /proc/flash/zImage\r"
      set timeout 180

      expect "$md5sum" { </bold>
      you set the timeout value a few times but don't have any expect statements. The timeout is for the expect command not the send command.

      Try the debugging and post back.

       
      • tim lloyd
        tim lloyd
        2009-02-20

        thanks, this is sort of what I ended up doing. I found that the md5sums did not match hence the script simply sat there. I am not happy with the way the script was originally written but at least I know why it is doing what it is doing.

        Noted about the timeouts. Hopefully I can use this kind of script more regularly so I remember the knowledge gained on this exercise.

         
    • ender
      ender
      2009-02-20

      Thanks for the update. expect is great and if you plan on using it more you should get the book exploring expect.