How can I ssh into multiple password protected machines to reboot them?
up vote
-1
down vote
favorite
I'm trying to write a bash script to reboot multiple machines via ssh that are all password protected. I have all the IPs listed in a .txt file and want the script to read the IPs from there. I've been messing with the expect command since my mac does not have sshpass. Is there a way to do this without sshpass??
I have this so far:
#!/bin/bash
for server in 'testreboot.txt'; do
expect -c 'spawn ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server "sudo shutdown -r now"; expect "Password:"; send
"passwordr"; interact'
done
In testreboot.txt I simply have the IP address listed of a test machine. Is that ok or do I need to assign it to a variable? I keep getting a variable error when I try to run it.
ssh reboot expect
New contributor
add a comment |
up vote
-1
down vote
favorite
I'm trying to write a bash script to reboot multiple machines via ssh that are all password protected. I have all the IPs listed in a .txt file and want the script to read the IPs from there. I've been messing with the expect command since my mac does not have sshpass. Is there a way to do this without sshpass??
I have this so far:
#!/bin/bash
for server in 'testreboot.txt'; do
expect -c 'spawn ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server "sudo shutdown -r now"; expect "Password:"; send
"passwordr"; interact'
done
In testreboot.txt I simply have the IP address listed of a test machine. Is that ok or do I need to assign it to a variable? I keep getting a variable error when I try to run it.
ssh reboot expect
New contributor
It might be time to start looking into Python. I really like paramiko for this sort of work. Also consider Ansible
– Ben
Nov 29 at 1:07
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 29 at 5:47
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm trying to write a bash script to reboot multiple machines via ssh that are all password protected. I have all the IPs listed in a .txt file and want the script to read the IPs from there. I've been messing with the expect command since my mac does not have sshpass. Is there a way to do this without sshpass??
I have this so far:
#!/bin/bash
for server in 'testreboot.txt'; do
expect -c 'spawn ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server "sudo shutdown -r now"; expect "Password:"; send
"passwordr"; interact'
done
In testreboot.txt I simply have the IP address listed of a test machine. Is that ok or do I need to assign it to a variable? I keep getting a variable error when I try to run it.
ssh reboot expect
New contributor
I'm trying to write a bash script to reboot multiple machines via ssh that are all password protected. I have all the IPs listed in a .txt file and want the script to read the IPs from there. I've been messing with the expect command since my mac does not have sshpass. Is there a way to do this without sshpass??
I have this so far:
#!/bin/bash
for server in 'testreboot.txt'; do
expect -c 'spawn ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server "sudo shutdown -r now"; expect "Password:"; send
"passwordr"; interact'
done
In testreboot.txt I simply have the IP address listed of a test machine. Is that ok or do I need to assign it to a variable? I keep getting a variable error when I try to run it.
ssh reboot expect
ssh reboot expect
New contributor
New contributor
edited Nov 29 at 1:49
Rui F Ribeiro
38.3k1476127
38.3k1476127
New contributor
asked Nov 29 at 0:43
Danny
6
6
New contributor
New contributor
It might be time to start looking into Python. I really like paramiko for this sort of work. Also consider Ansible
– Ben
Nov 29 at 1:07
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 29 at 5:47
add a comment |
It might be time to start looking into Python. I really like paramiko for this sort of work. Also consider Ansible
– Ben
Nov 29 at 1:07
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 29 at 5:47
It might be time to start looking into Python. I really like paramiko for this sort of work. Also consider Ansible
– Ben
Nov 29 at 1:07
It might be time to start looking into Python. I really like paramiko for this sort of work. Also consider Ansible
– Ben
Nov 29 at 1:07
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 29 at 5:47
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 29 at 5:47
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You have single quoted the string; this prevents the shell from
interpolating $server
. TCL (which is what expect
code is written in)
also uses the $server
form to interpolate variables, but you have not
set server ...
anywhere in your TCL code. Hence the error, as the
shell did not interpolate it nor did the TCL set it. One method would be
to double quote the TCL expression which will allow the shell to
interpolate in the shell $server
variable:
$ server=example; expect -c 'puts $server'
can't read "server": no such variable
while executing
"puts $server"
$ server=example; expect -c "puts $server"
example
$
However this will mean that any "
in the TCL code will need to be
escaped to protect them from messing with the shell interpolation which
will probably get ugly real fast and will be hard to debug, especially
as the commands get longer and more complicated.
$ server=example; expect -c "puts "a server named '$server'""
a server named 'example'
$
Another option would be to read the argument into a TCL variable, but,
alas, the -c
flag code of expect
has no access to the arguments list
in char *argv
as the -c
code is run before the arguments list is
made available for use by TCL. Therefore to use $server
as a TCL
variable the script would need to be rewritten in TCL:
#!/usr/bin/env expect
set fh [open testreboot.txt r]
while {[gets $fh server] >= 0} {
spawn -noecho ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server
expect -ex "Password:"
send "Hunter2r"
expect -ex {$ }
send "sudo shutdown -r nowr"
expect -ex "Password:"
send "Hunter2r"
interact
}
which will may need improvements for error handling, timeouts, matching
the shell prompt better, etc.
add a comment |
up vote
1
down vote
It's not easy to embed Expect in shell scripts. I'd give you an example using the sexpect tool.
The code:
[STEP 101] # cat foo.xsh
user=foo
passwd=foobar
while read host; do
echo ==== $host ====
export SEXPECT_SOCKFILE=~/tmp/host-$host-$$.sock
sexpect spawn
ssh -o StrictHostKeyChecking=no
-o ConnectTimeout=5
-o UserKnownHostsFile=/dev/null
-t
$user@$host bash --norc
sexpect expect -i password:
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter "sudo date"
sexpect expect -i -re "password( for [^[:blank:]]+):"
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter exit
sexpect wait
done < hosts-file
Testing:
[STEP 102] # cat hosts-file
host1
host2
[STEP 103] # bash foo.xsh
==== host1 ====
foo@host1's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:39 CST 2018
bash-4.4$ exit
exit
Connection to host1 closed.
==== host2 ====
foo@host2's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:40 CST 2018
bash-4.4$ exit
exit
Connection to host2 closed.
[STEP 104] #
New contributor
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You have single quoted the string; this prevents the shell from
interpolating $server
. TCL (which is what expect
code is written in)
also uses the $server
form to interpolate variables, but you have not
set server ...
anywhere in your TCL code. Hence the error, as the
shell did not interpolate it nor did the TCL set it. One method would be
to double quote the TCL expression which will allow the shell to
interpolate in the shell $server
variable:
$ server=example; expect -c 'puts $server'
can't read "server": no such variable
while executing
"puts $server"
$ server=example; expect -c "puts $server"
example
$
However this will mean that any "
in the TCL code will need to be
escaped to protect them from messing with the shell interpolation which
will probably get ugly real fast and will be hard to debug, especially
as the commands get longer and more complicated.
$ server=example; expect -c "puts "a server named '$server'""
a server named 'example'
$
Another option would be to read the argument into a TCL variable, but,
alas, the -c
flag code of expect
has no access to the arguments list
in char *argv
as the -c
code is run before the arguments list is
made available for use by TCL. Therefore to use $server
as a TCL
variable the script would need to be rewritten in TCL:
#!/usr/bin/env expect
set fh [open testreboot.txt r]
while {[gets $fh server] >= 0} {
spawn -noecho ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server
expect -ex "Password:"
send "Hunter2r"
expect -ex {$ }
send "sudo shutdown -r nowr"
expect -ex "Password:"
send "Hunter2r"
interact
}
which will may need improvements for error handling, timeouts, matching
the shell prompt better, etc.
add a comment |
up vote
2
down vote
You have single quoted the string; this prevents the shell from
interpolating $server
. TCL (which is what expect
code is written in)
also uses the $server
form to interpolate variables, but you have not
set server ...
anywhere in your TCL code. Hence the error, as the
shell did not interpolate it nor did the TCL set it. One method would be
to double quote the TCL expression which will allow the shell to
interpolate in the shell $server
variable:
$ server=example; expect -c 'puts $server'
can't read "server": no such variable
while executing
"puts $server"
$ server=example; expect -c "puts $server"
example
$
However this will mean that any "
in the TCL code will need to be
escaped to protect them from messing with the shell interpolation which
will probably get ugly real fast and will be hard to debug, especially
as the commands get longer and more complicated.
$ server=example; expect -c "puts "a server named '$server'""
a server named 'example'
$
Another option would be to read the argument into a TCL variable, but,
alas, the -c
flag code of expect
has no access to the arguments list
in char *argv
as the -c
code is run before the arguments list is
made available for use by TCL. Therefore to use $server
as a TCL
variable the script would need to be rewritten in TCL:
#!/usr/bin/env expect
set fh [open testreboot.txt r]
while {[gets $fh server] >= 0} {
spawn -noecho ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server
expect -ex "Password:"
send "Hunter2r"
expect -ex {$ }
send "sudo shutdown -r nowr"
expect -ex "Password:"
send "Hunter2r"
interact
}
which will may need improvements for error handling, timeouts, matching
the shell prompt better, etc.
add a comment |
up vote
2
down vote
up vote
2
down vote
You have single quoted the string; this prevents the shell from
interpolating $server
. TCL (which is what expect
code is written in)
also uses the $server
form to interpolate variables, but you have not
set server ...
anywhere in your TCL code. Hence the error, as the
shell did not interpolate it nor did the TCL set it. One method would be
to double quote the TCL expression which will allow the shell to
interpolate in the shell $server
variable:
$ server=example; expect -c 'puts $server'
can't read "server": no such variable
while executing
"puts $server"
$ server=example; expect -c "puts $server"
example
$
However this will mean that any "
in the TCL code will need to be
escaped to protect them from messing with the shell interpolation which
will probably get ugly real fast and will be hard to debug, especially
as the commands get longer and more complicated.
$ server=example; expect -c "puts "a server named '$server'""
a server named 'example'
$
Another option would be to read the argument into a TCL variable, but,
alas, the -c
flag code of expect
has no access to the arguments list
in char *argv
as the -c
code is run before the arguments list is
made available for use by TCL. Therefore to use $server
as a TCL
variable the script would need to be rewritten in TCL:
#!/usr/bin/env expect
set fh [open testreboot.txt r]
while {[gets $fh server] >= 0} {
spawn -noecho ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server
expect -ex "Password:"
send "Hunter2r"
expect -ex {$ }
send "sudo shutdown -r nowr"
expect -ex "Password:"
send "Hunter2r"
interact
}
which will may need improvements for error handling, timeouts, matching
the shell prompt better, etc.
You have single quoted the string; this prevents the shell from
interpolating $server
. TCL (which is what expect
code is written in)
also uses the $server
form to interpolate variables, but you have not
set server ...
anywhere in your TCL code. Hence the error, as the
shell did not interpolate it nor did the TCL set it. One method would be
to double quote the TCL expression which will allow the shell to
interpolate in the shell $server
variable:
$ server=example; expect -c 'puts $server'
can't read "server": no such variable
while executing
"puts $server"
$ server=example; expect -c "puts $server"
example
$
However this will mean that any "
in the TCL code will need to be
escaped to protect them from messing with the shell interpolation which
will probably get ugly real fast and will be hard to debug, especially
as the commands get longer and more complicated.
$ server=example; expect -c "puts "a server named '$server'""
a server named 'example'
$
Another option would be to read the argument into a TCL variable, but,
alas, the -c
flag code of expect
has no access to the arguments list
in char *argv
as the -c
code is run before the arguments list is
made available for use by TCL. Therefore to use $server
as a TCL
variable the script would need to be rewritten in TCL:
#!/usr/bin/env expect
set fh [open testreboot.txt r]
while {[gets $fh server] >= 0} {
spawn -noecho ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5
administrator@$server
expect -ex "Password:"
send "Hunter2r"
expect -ex {$ }
send "sudo shutdown -r nowr"
expect -ex "Password:"
send "Hunter2r"
interact
}
which will may need improvements for error handling, timeouts, matching
the shell prompt better, etc.
answered Nov 29 at 1:26
thrig
23.8k12955
23.8k12955
add a comment |
add a comment |
up vote
1
down vote
It's not easy to embed Expect in shell scripts. I'd give you an example using the sexpect tool.
The code:
[STEP 101] # cat foo.xsh
user=foo
passwd=foobar
while read host; do
echo ==== $host ====
export SEXPECT_SOCKFILE=~/tmp/host-$host-$$.sock
sexpect spawn
ssh -o StrictHostKeyChecking=no
-o ConnectTimeout=5
-o UserKnownHostsFile=/dev/null
-t
$user@$host bash --norc
sexpect expect -i password:
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter "sudo date"
sexpect expect -i -re "password( for [^[:blank:]]+):"
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter exit
sexpect wait
done < hosts-file
Testing:
[STEP 102] # cat hosts-file
host1
host2
[STEP 103] # bash foo.xsh
==== host1 ====
foo@host1's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:39 CST 2018
bash-4.4$ exit
exit
Connection to host1 closed.
==== host2 ====
foo@host2's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:40 CST 2018
bash-4.4$ exit
exit
Connection to host2 closed.
[STEP 104] #
New contributor
add a comment |
up vote
1
down vote
It's not easy to embed Expect in shell scripts. I'd give you an example using the sexpect tool.
The code:
[STEP 101] # cat foo.xsh
user=foo
passwd=foobar
while read host; do
echo ==== $host ====
export SEXPECT_SOCKFILE=~/tmp/host-$host-$$.sock
sexpect spawn
ssh -o StrictHostKeyChecking=no
-o ConnectTimeout=5
-o UserKnownHostsFile=/dev/null
-t
$user@$host bash --norc
sexpect expect -i password:
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter "sudo date"
sexpect expect -i -re "password( for [^[:blank:]]+):"
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter exit
sexpect wait
done < hosts-file
Testing:
[STEP 102] # cat hosts-file
host1
host2
[STEP 103] # bash foo.xsh
==== host1 ====
foo@host1's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:39 CST 2018
bash-4.4$ exit
exit
Connection to host1 closed.
==== host2 ====
foo@host2's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:40 CST 2018
bash-4.4$ exit
exit
Connection to host2 closed.
[STEP 104] #
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
It's not easy to embed Expect in shell scripts. I'd give you an example using the sexpect tool.
The code:
[STEP 101] # cat foo.xsh
user=foo
passwd=foobar
while read host; do
echo ==== $host ====
export SEXPECT_SOCKFILE=~/tmp/host-$host-$$.sock
sexpect spawn
ssh -o StrictHostKeyChecking=no
-o ConnectTimeout=5
-o UserKnownHostsFile=/dev/null
-t
$user@$host bash --norc
sexpect expect -i password:
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter "sudo date"
sexpect expect -i -re "password( for [^[:blank:]]+):"
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter exit
sexpect wait
done < hosts-file
Testing:
[STEP 102] # cat hosts-file
host1
host2
[STEP 103] # bash foo.xsh
==== host1 ====
foo@host1's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:39 CST 2018
bash-4.4$ exit
exit
Connection to host1 closed.
==== host2 ====
foo@host2's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:40 CST 2018
bash-4.4$ exit
exit
Connection to host2 closed.
[STEP 104] #
New contributor
It's not easy to embed Expect in shell scripts. I'd give you an example using the sexpect tool.
The code:
[STEP 101] # cat foo.xsh
user=foo
passwd=foobar
while read host; do
echo ==== $host ====
export SEXPECT_SOCKFILE=~/tmp/host-$host-$$.sock
sexpect spawn
ssh -o StrictHostKeyChecking=no
-o ConnectTimeout=5
-o UserKnownHostsFile=/dev/null
-t
$user@$host bash --norc
sexpect expect -i password:
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter "sudo date"
sexpect expect -i -re "password( for [^[:blank:]]+):"
sexpect send -enter "$passwd"
sexpect expect -re 'bash-[.0-9]+[#$] $'
sexpect send -enter exit
sexpect wait
done < hosts-file
Testing:
[STEP 102] # cat hosts-file
host1
host2
[STEP 103] # bash foo.xsh
==== host1 ====
foo@host1's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:39 CST 2018
bash-4.4$ exit
exit
Connection to host1 closed.
==== host2 ====
foo@host2's password:
bash-4.4$ sudo date
[sudo] password for foo:
Thu Nov 29 14:28:40 CST 2018
bash-4.4$ exit
exit
Connection to host2 closed.
[STEP 104] #
New contributor
New contributor
answered Nov 29 at 6:31
pynexj
1117
1117
New contributor
New contributor
add a comment |
add a comment |
Danny is a new contributor. Be nice, and check out our Code of Conduct.
Danny is a new contributor. Be nice, and check out our Code of Conduct.
Danny is a new contributor. Be nice, and check out our Code of Conduct.
Danny is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484801%2fhow-can-i-ssh-into-multiple-password-protected-machines-to-reboot-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It might be time to start looking into Python. I really like paramiko for this sort of work. Also consider Ansible
– Ben
Nov 29 at 1:07
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 29 at 5:47