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

1## Get User Name.
2loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
3
4PATH1='/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

1/usr/bin/expect<<EOD
2
3# Set Timeout of Expect
4
5set timeout -1
6
7spawn /usr/bin/scp -r $PATH1 $USERNAME@$HOST:/Volumes/Storage/sftp/$loggedInUser/
8
9expect "Password:"
10send "$PASSWD
11"
12EOD

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

1#!/usr/bin/expect -f
2
3## Set variables here
4## JSS parameters are -1
5
6set user [lindex $argv 2]
7set host [lindex $argv 3]
8set password *putpasswordhere*
9set path1 [lindex $argv 5]
10set path2 [lindex $argv 6]
11set path3 [lindex $argv 7]
12set path4 [lindex $argv 8]
13set path5 [lindex $argv 9]
14set path6 [lindex $argv 5]
15
16spawn scp -r $path1 root@$host:/Volumes/Storage/sftp/$user/
17expect {
18 "*?word:" {
19 send "$password
20"
21 exp_continue
22 }
23}
24
25spawn scp -r $path2 root@$host:/Volumes/Storage/sftp/$user/
26expect {
27 "*?word:" {
28 send "$password
29"
30 exp_continue
31 }
32}

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