# SLES11 Expect script

**URL:** <https://forums.suse.com/t/sles11-expect-script/27853>\
**Category:** SLES Configure-Administer\
**Created:** [February 1, 2016, 11:29am UTC](https://forums.suse.com/t/sles11-expect-script/27853 "2016-02-01T11:29:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malikcg](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Malikcg](https://forums.suse.com/u/Malikcg)\
**Post date:** [February 1, 2016, 11:29am UTC](https://forums.suse.com/t/sles11-expect-script/27853/1 "2016-02-01T11:29:04Z")

</div>

Hi everybody,

I’m making a script to configure SNMP on my SUSE server.  
This script is wrote mixing BASH and EXPECT.  
It work on RedHat/CentOs et Ubuntu (with some changes)

This is the script :  
expect -c "  
set timeout 1  
spawn ssh root@$IP zypper install -y net-snmp  
expect yes/no { send yes\r; exp\_continue }  
expect password: { send $PWD\r }  
sleep 1  
exit  
"  
expect -c "  
set timeout 1  
spawn scp /usr/local/packages/deploiement/suse/snmpd.conf [root@$IP:/etc/snmp/snmpd.conf](mailto:root@%24IP:/etc/snmp/snmpd.conf)  
expect yes/no { send yes\r; exp\_continue }  
expect password: { send $PWD\r }  
sleep 1  
exit  
"  
expect -c "  
set timeout 1  
spawn ssh root@$IP chkconfig snmpd on  
expect yes/no { send yes\r; exp\_continue }  
expect password: { send $PWD\r }  
sleep 1  
exit  
"  
expect -c "  
set timeout 1  
spawn ssh root@$IP /etc/init.d/snmpd restart  
expect yes/no { send yes\r; exp\_continue }  
expect password: { send $PWD\r }  
sleep 1  
exit  
"

I don’t know why but all the command work if i’m not in the script.  
When i launch the script i write IP and PWD, the script make no error but i don’t see any changes on my server.

Can you help me please ^^ ?

---

<div class="post-metadata">

**Author:** ![ab1](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@ab1](https://forums.suse.com/u/ab1)\
**Post date:** [February 1, 2016, 2:32pm UTC](https://forums.suse.com/t/sles11-expect-script/27853/2 "2016-02-01T14:32:52Z")

</div>

On 02/01/2016 02:34 AM, Malikcg wrote:[color=blue]

> I’m making a script to configure SNMP on my SUSE server.  
> This script is wrote mixing BASH and EXPECT.  
> It work on RedHat/CentOs et Ubuntu (with some changes)[/color]

Which changes? How did you find out that they were necessary?  
[color=blue]

> This is the script :  
> expect -c "  
> set timeout 1  
> spawn ssh root@$IP zypper install -y net-snmp  
> expect yes/no { send yes\r; exp\_continue }  
> expect password: { send $PWD\r }  
> sleep 1  
> exit  
> "[/color]

These expected prompts seem like they could, and should, be overcome in  
other ways. I presume the first one is to accept an unknown host key,  
which you probably should not do in something automated without verifying  
the key is valid first, or else just disable strict host key checking so  
that the prompt never happens if you really d not care about that security  
check. For example add the following to your ssh command:  
-oStrictHostKeyChecking=no A better option would be to add the  
appropriate host key to your user’s ~/.ssh/known\_hosts file before using  
the rest of the script so that either it works when safe, or fails completely.

The second prompt is presumably to write a password which, again, you  
should not do, particularly in combination with your first prompt. If you  
trust the host, then setup SSH keys so that there is no password prompt.  
This will increase security because there is nothing that will store the  
password, or be any need to maintain this particular password as it  
changes in the future (vs. a key).  
[color=blue]

> expect -c "  
> set timeout 1  
> spawn scp /usr/local/packages/deploiement/suse/snmpd.conf  
> [root@$IP:/etc/snmp/snmpd.conf](mailto:root@$IP:/etc/snmp/snmpd.conf)  
> expect yes/no { send yes\r; exp\_continue }  
> expect password: { send $PWD\r }  
> sleep 1  
> exit  
> "  
> expect -c "  
> set timeout 1  
> spawn ssh root@$IP chkconfig snmpd on  
> expect yes/no { send yes\r; exp\_continue }  
> expect password: { send $PWD\r }  
> sleep 1  
> exit  
> "  
> expect -c "  
> set timeout 1  
> spawn ssh root@$IP /etc/init.d/snmpd restart  
> expect yes/no { send yes\r; exp\_continue }  
> expect password: { send $PWD\r }  
> sleep 1  
> exit  
> "
> 
> I don’t know why but all the command work if i’m not in the script.  
> When i launch the script i write IP and PWD, the script make no error[/color]

How do you “write IP and PWD” for the script to use? Showing the command  
that you call could be helpful.  
[color=blue]

> but i don’t see any changes on my server.[/color]

Perhaps try capturing the output, or see on the server side which commands  
are being sent. If you add ‘-v’ (maybe multiple times) to your ssh/scp  
commands you should get more output that could help identify a problem.

–  
Good luck.

If you find this post helpful and are logged into the web interface,  
show your appreciation and click on the star below…
