Support Questions

Find answers, ask questions, and share your expertise

How to automate ambari LDAP sync through expect script

avatar
Rising Star

Hi Team,

I have used ambari-ldap sync script but I get the following error when I ran the below command. One thing I noticed is that if the run the script manually as ./ambari_ldap_sync_all.sh then its getting executed.

Also I have shown my ambari-ldap sync script below. So the script is not getting executed from crontab with 'sh' command .

[root@host1(172.23.34.4)] # sh ambari_ldap_sync_all.sh
ambari_ldap_sync_all.sh: line 3: spawn: command not found
couldn't read file "Enter Ambari Admin login:": no such file or directory
ambari_ldap_sync_all.sh: line 7: send: command not found
couldn't read file "Enter Ambari Admin password:": no such file or directory
ambari_ldap_sync_all.sh: line 11: send: command not found
couldn't read file "eof": no such file or directory

[root@host1(172.23.34.4)] # cat ambari_ldap_sync_all.sh
#!/usr/bin/expect

spawn ambari-server sync-ldap --existing 

expect "Enter Ambari Admin login:"

send "admin\r"

expect "Enter Ambari Admin password:"

send "admin\r"

expect eof

[root@host1(172.23.34.4)] # crontab -e
00 15 * * * /ambari_ldap_sync_all.sh

Can someone help me how to write expect script if it is required??

1 ACCEPTED SOLUTION

avatar
Rising Star

Finally I am able to resolve the issue.

1. I changed the script extension from ambari_ldap_sync_all.sh to ambari_ldap_sync_all.exp

2. I also changed the absolute path of ambari-server as /usr/sbin/ambari-server and added exit statement at the end of script.

#!/usr/bin/expect
spawn  /usr/sbin/ambari-server sync-ldap --existing
expect "Enter Ambari Admin login:"
send   "admin\r"
expect "Enter Ambari Admin password:"
send   "admin\r"
expect eof
exit

3. Finally inside the crontab, I made the entry as

0 23 * * * /usr/bin/expect /opt/ambari_ldap_sync_all.exp

View solution in original post

5 REPLIES 5

avatar
Master Mentor

@Rahul Buragohain

Have you done

# yum install expect -y 

.

avatar
Rising Star

@Jay SenSharma

Yes expect package is already installed. Actually my issue is the script is not getting executed at 3pm even though it is configured in crontab. As I said, when I ran the command manually as ./ambari_ldap_sync_all.sh then it works. So is there any alternative how the script can get executed automatically from crontab?

avatar
Master Mentor

@Rahul Buragohain

The error that you posted clearly indicates that the packages are not properly installed (OR) the current users PATH does is not correct. (OR) after installing the "expect" package you have not included the "#!/usr/bin/expect" at the beginning of your script.

 ambari_ldap_sync_all.sh: line 3: spawn: command not found
 ambari_ldap_sync_all.sh: line 7: send: command not found

- Please double check those packages. Command like "spawn" comes from expect package.

- As you are able to run the command manually, which means these commands might now be available in your PATH variable when you are running them using cron.

So please fix the PATH to include the PATH to point to the location of these commands

.

avatar
Master Mentor

Regarding your query: "is there any alternative how the script can get executed automatically from crontab?"\

 crontab -e

Then add the following entry in the crontab:

 0 15 *  *  *  /PATH/TO/ambari_ldap_sync_all.sh

However above approach has nothing to do with HDP or Ambari. It's simple Linux.

avatar
Rising Star

Finally I am able to resolve the issue.

1. I changed the script extension from ambari_ldap_sync_all.sh to ambari_ldap_sync_all.exp

2. I also changed the absolute path of ambari-server as /usr/sbin/ambari-server and added exit statement at the end of script.

#!/usr/bin/expect
spawn  /usr/sbin/ambari-server sync-ldap --existing
expect "Enter Ambari Admin login:"
send   "admin\r"
expect "Enter Ambari Admin password:"
send   "admin\r"
expect eof
exit

3. Finally inside the crontab, I made the entry as

0 23 * * * /usr/bin/expect /opt/ambari_ldap_sync_all.exp