Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I have a script which is logging in to network devices via ssh using expect programming. My problem is that if I do Code: ssh host -l uname It ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Location
    Chicago
    Posts
    7

    If and else statement with Expect

    Hi,

    I have a script which is logging in to network devices via ssh using expect programming.

    My problem is that if I do

    Code:
    ssh host -l uname
    It only works if y ssh_config is setup to use Protocol 1,2 and my network device I am trying to connect has ssh 1 but not 2. If the network device has ssh 2 but not 1 my script will stop there with an error.

    Bottom line is I need to apply if and else structure after I try to ssh to see if ssh -1 was successful if not try ssh -2 if not go down hill


    Here is my script:

    Code:
    #!/usr/local/bin/expect -f
    spawn ssh myHost -l myUsername
    expect "password:"
    send "myPassword\r"
    expect "#"
    #send "show int status\r"
    expect "#"
    send "exit\r"
    And this is how my script should look like with the proper syntax for expect

    Code:
    #!/usr/local/bin/expect -f
    spawn ssh -1 myHost -l myUsername
    if(not logged in){
       ssh -2 myHost -l myUname
    }else{
    echo "it failed\r"
    }
    
    expect "password:"
    send "myPassword\r"
    expect "#"
    #send "show int status\r"
    expect "#"
    send "exit\r"
    Thank you in advance.

  2. #2
    Just Joined!
    Join Date
    Nov 2009
    Location
    Chicago
    Posts
    7
    I think I know how to do the if and else structure now I am having different problem.

    Code:
    #!/usr/local/bin/expect -f
    
    spawn ssh -2 xxx.xx.xxx.xx -l root
    
    expect
     {
            "password:"{
                    send "myPw\r"
            }"*ssh_rsa_verify:*"{
                    spawn ssh -1 xxx.xx.xxx.xx -l root
            }"password":{
                    end "myPw\r"
            }eof{
                    exit
            }
    }
    
    
    expect "#"
    #send "show int status\r"
    send "config t\r"
    .
    .
    some more commands follow
    For some reason after the spawn ssh ip is executed I can't read the result of this command act accordingly in my if and else structure....any ideas?

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...