简体   繁体   中英

OSX use a variable in Expect script

Using OSX I'd like to access $(pbpaste) within Expect.

So for example this alias connects to a copied ip address (assuming I copied it before executing it)- leaving me to enter the password:

alias s='ssh $(pbpaste) -l root'

And this script connects me to the specific host listed in the script entering the password for me:

#!/usr/bin/expect
spawn  ssh hostname -l username
expect "password:"
send "secret\n"
interact

I would like to incorporate the $(pbpaste) into the expect script so that the copied host is accessed and the password is entered for me too. How?

On a side note, I'm aware of ssh keys, and thats its generally poor form to put passwords in plain text. In my situation this is unimportant and won't help me - the utility of getting the above EXPECT script working with $(pbpaste) variable as the hostname is more important to me.

Additionally, I would like to be able to pass the host as an argument instead of $(pbpaste) too. This is secondary to the original question though- one script for each? Or a default of $(pbpaste) if no argument for host is provided? Which ever is easier to write/ understand.

I can't test it, but this should work:

spawn ssh [exec pbpaste] -l username

Or with a command line argument:

#!/usr/bin/expect
if {$argc == 0} {
  set hostname [exec pbpaste]
} else {
  set hostname [lindex $argv 0]
}
spawn ssh $hostname -l username

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM