SLES11 Expect script

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
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 ^^ ?

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
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…