Skip to main content
Question

PATH Variable with the $User variable


Forum|alt.badge.img+10

I have a script that has the the following variables

## Get User Name.
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

PATH1='/Users/$loggedInUser/path/to/folder/'

Then it uses expect to pass the path to a SCP command. When passing the path it leaves the text as "$loggedInUser" and does not put the username in

/usr/bin/expect<<EOD

# Set Timeout of Expect

set timeout -1

spawn /usr/bin/scp -r $PATH1 $USERNAME@$HOST:/Volumes/Storage/sftp/$loggedInUser/

expect "Password:"
send "$PASSWD
"
EOD

Any ideas how to get the path to contain the username?

5 replies

Forum|alt.badge.img+16
  • Honored Contributor
  • 330 replies
  • March 23, 2017

Remove both ' from the variable. If you have spaces and such and don't want to deal with backslashes you could always use double quotes (") instead.


Forum|alt.badge.img+10
  • Author
  • Contributor
  • 126 replies
  • March 24, 2017

That worked. Now the script just exits and never passes the password with the expect.


Forum|alt.badge.img+16
  • Honored Contributor
  • 330 replies
  • March 24, 2017

Have you tried just putting the entire script in expect?

!/usr/bin/expect -f

Or is this part of a greater script? I've never really worked with expect calls within another script so I'm not sure if anything else is necessary.

In any event, the guy in this link is attempting to do something similar and someone uses a similar script to yours but appends:

expect " "
send " "

After the password send. Not sure what's happening there exactly but worth a shot.

http://serverfault.com/questions/209248/can-i-use-expect-with-scp-to-bypass-the-password-prompt


Forum|alt.badge.img+10
  • Author
  • Contributor
  • 126 replies
  • March 24, 2017

Tried using the /usr/bin/expect

#!/usr/bin/expect -f

## Set variables here
## JSS parameters are -1

set user [lindex $argv 2]
set host [lindex $argv 3]
set password *putpasswordhere*
set path1 [lindex $argv 5]
set path2 [lindex $argv 6]
set path3 [lindex $argv 7]
set path4 [lindex $argv 8]
set path5 [lindex $argv 9]
set path6 [lindex $argv 5]

spawn scp -r $path1 root@$host:/Volumes/Storage/sftp/$user/
expect {
    "*?word:" {
    send "$password
"
    exp_continue
    }
}

spawn scp -r $path2 root@$host:/Volumes/Storage/sftp/$user/
expect {
    "*?word:" {
    send "$password
"
    exp_continue
    }
}

The path variables are not able to use the $user in the parameter and you have to type in the username .

like /Users/username/path/to/folder/

I would like it to be /Users/$user/path/to/folder/


Forum|alt.badge.img+16
  • Honored Contributor
  • 330 replies
  • March 24, 2017

This may sound dumb... but have you tested it in non-script form? Are you maybe getting an ECDSA key fingerprint warning? That would bring up this query and maybe interfere with your expect:
"Are you sure you want to continue connecting (yes/no)?"


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings