Find the answer to your Linux question:
Results 1 to 4 of 4
hi, I need to switch users in a script I'm newbee to expect I wrote two files 1 sudo.exp, since su4098 is used as root in my laptop Code: #!/usr/bin/expect ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2012
    Posts
    85

    using expect to switch users in a script


    hi, I need to switch users in a script
    I'm newbee to expect
    I wrote two files

    1 sudo.exp, since su4098 is used as root in my laptop
    Code:
    #!/usr/bin/expect
    spawn su su4098
    expect *Password:*
    send mypsw\n
    interact
    2 test.sh
    Code:
    ls
    ./sudo.exp
    tcpdump -i wlan0
    su esolve
    ls
    however, in test.sh
    when it switches to su4098,namely the root
    it stops a while
    and tens of seconds later, it run the password as a command
    and the following commands: tcpdump, su esolve, and ls are not run

    what is wrong with my scripts?

    besides, how to share variables between users in a script?
    thanks?

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by esolve View Post
    hi, I need to switch users in a script
    I'm newbee to expect
    I wrote two files

    1 sudo.exp, since su4098 is used as root in my laptop
    Code:
    #!/usr/bin/expect
    spawn su su4098
    expect *Password:*
    send mypsw\n
    interact
    2 test.sh
    Code:
    ls
    ./sudo.exp
    tcpdump -i wlan0
    su esolve
    ls
    however, in test.sh
    when it switches to su4098,namely the root
    it stops a while
    and tens of seconds later, it run the password as a command
    and the following commands: tcpdump, su esolve, and ls are not run
    why not just pass the tcpdump command as a command directly to su in your expect script? e.g.:

    Code:
    #!/usr/bin/expect
    spawn su su4098 -c "tcpdump -i wlan0"
    expect *Password:*
    send mypsw\n
    interact

  3. #3
    Just Joined!
    Join Date
    May 2012
    Posts
    85
    I need to use some variables with tcpdump
    these variables will be lost in the sudo.exp

    Quote Originally Posted by atreyu View Post
    why not just pass the tcpdump command as a command directly to su in your expect script? e.g.:

    Code:
    #!/usr/bin/expect
    spawn su su4098 -c "tcpdump -i wlan0"
    expect *Password:*
    send mypsw\n
    interact

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by esolve View Post
    I need to use some variables with tcpdump
    these variables will be lost in the sudo.exp
    could you be more specific about "variables will be lost"? If you mean the "-i wlan0", they are passed to tcpdump for me, provided that I enclose the whole command in double-quotes (as shown).

Posting Permissions

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