Expect: SSH into a remote host, run a command, and save its output to a variable - tcl

I'm trying to ssh into a server, run a command, and save its output to a variable, with no success.
spawn $env(SHELL)
expect "\$ "
send "ls\r"
expect "\$ "
send "ssh myserver1\r"
expect "\$ "
send "cd /tmp/remotedir1\r"
expect "\$ "
send "ls\r"
expect "\$ "
set myvar1 [exec ls]
puts "The value of \$myvar1 is: "
puts $myvar1
send "exit\r"
expect "\$ "
send "exit\r"
expect eof
When I run it, I get:
spawn /bin/bash
$ ls
localfile1 localfile2 localfile3
$ ssh myserver1
Last login: Tue Sep 10 15:45:07 2017 from 192.168.0.100
myserver1$ cd /tmp/remotedir1
myserver1$ ls
remotefile1
myserver1$ The value of $myvar1 is:
localfile1
localfile2
localfile3
exit
logout
Connection to myserver1 closed.
bash-3.2$ exit
exit
Apparently, instead of setting $myvar1 to "remotefile1", it sets to those 3 files in the $cwd on the local host.
Thank you for your help in advance!

Using exec will execute the command locally.
Upon sending the ls command, you have to make use of the expect_out array to get the response.
set prompt "(.*)(#|%|>|\\\$) $"
send "ls\r"
expect -re $prompt
puts $expect_out(1,string)

Related

Expect script ends without error at the New Password prompt

I have Solaris servers of which I'm not sure if I changed the password to a particular account. Either I'm going to authenticate successfully because I already changed the password or I'm going to authenticate with the old password and it will prompt me to change my password after I authenticate with the old password because it expired.
Warning: Your password has expired, please change it now.
New Password:
The script will stop at the New Password: prompt and exits w/o an error.
#!/bin/bash
NewPassword="cccccc"
OtherPassword="ffffffff"
for i in `cat x.txt`
do
/opt/csw/bin/expect -f <(cat << EOF
spawn ssh -o StrictHostKeyChecking=no adminuser#$i
expect "Password: "
send "$NewPassword\r"
expect {
"$ " {
## new password worked
send "uname -a\r"
}
"Password: " {
## The new password did not work
send "$OtherPassword\r"
expect "$ "
}
"New Password: " {
## after authenticating, my old password expired need to change it now
send ${NewPassword}\r
expect "Re-enter new Password: "
send ${NewPassword}\r
expect "$ "
}
}
EOF
)
done
The order of clauses in the expect matters; the internal matcher tries the different matching rules in the order you specify. This is important here because the rule for Password: also matches what New Password: will match, and so will have priority over it.
Swap the two clauses or rewrite them so that they can't both match the same input text (probably by including the newline or changing to using anchored regular expressions). Swapping them is far easier.
In addition to Donal's cogent advice, a couple of notes:
In the shell part, don't read lines with for
using a process substitution and cat and a here document is overkill: you just need the here-doc
use exp_continue to handle the exceptional cases that occur before you see the prompt
exit your login session and expect to see it close so you don't have to wait around to timeout.
#!/bin/bash
NewPassword="cccccc"
OtherPassword="ffffffff"
while read server; do
/opt/csw/bin/expect << EOF
spawn ssh -o StrictHostKeyChecking=no adminuser#$server
expect "Password: "
send "$NewPassword\r"
set count 0
expect {
"New Password: " {
## after authenticating, my old password expired need to change it now
send ${NewPassword}\r
expect "Re-enter new Password: "
send ${NewPassword}\r
exp_continue
}
"Password: " {
if {[incr count] == 2} {
error "neither new password nor old password worked for $server"
}
## The new password did not work
send "$OtherPassword\r"
exp_continue
}
"$ "
}
send "uname -a\r"
expect "$ "
send "exit\r"
expect eof
EOF
done < x.txt

Bash - Break up returned value from MySQL query

I am trying to break up a returned value from a mysql call in a shell script. Essentially what I have done so far is query the database for IP addresses that I have stored in a specific table. From there I store that returned value into a bash variable. The code is below:
#!/bin/bash
# This file will be used to obtain the system details for given ip address
retrieve_details()
{
# get all the ip addresses in the hosts table to pass to snmp call
host_ips=$(mysql -u user -ppassword -D honours_project -e "SELECT host_ip FROM hosts" -s)
echo "$host_ips"
# break up the returned host ip values
# loop through array of ip addresses
# while [ ]
# do
# pass ip values to snmp command call
# store details into mysql table host_descriptions
# done
}
retrieve_details
So this returns the following:
192.168.1.1
192.168.1.100
192.168.1.101
These are essentially the values I have in my hosts table. So what I am trying to do is break up each value such that I can get an array that looks like the following:
arr[0]=192.168.1.1
arr[1]=192.168.1.100
arr[2]=192.168.1.101
...
I have reviewed this link here: bash script - select from database into variable but I don't believe this applies to my situation. Any help would be appreciated
host_ips=($(mysql -u user -ppassword -D honours_project -e "SELECT host_ip FROM hosts" -s));
outer () will convert that in array. But you need to change your IFS (Internal Field Separator) to a newline first.
IFS=$'\n';
host_ips=($(mysql -u user -ppassword -D honours_project -e "SELECT host_ip FROM hosts" -s));
unset IFS;
for i in ${host_ips[#]} ; do echo $i ; done;
to print with key
for i in "${!host_ips[#]}"
do
echo "key :" $i "value:" ${host_ips[$i]}
done
wspace#lw:~$ echo $host_ips
192.168.1.1 192.168.1.100 192.168.1.101
wspace#lw:~$ arr=($(echo $host_ips))
wspace#lw:~$ echo ${arr[0]}
192.168.1.1
wspace#lw:~$ echo ${arr[1]}
192.168.1.100
wspace#lw:~$ echo ${arr[2]}
192.168.1.101
wspace#lw:~$ echo ${arr[#]}
192.168.1.1 192.168.1.100 192.168.1.101
wspace#lw:~$
maybe this is what you want

How to start and stop ping federation using a unix script

I need a script to start and stop the pingfederate server using a unix script. I am looking for a best practise to start and stop pingfederate using the script
http://documentation.pingidentity.com/display/PF72/Running+PingFederate+as+a+Service has:
#! /bin/sh
start() {
echo "starting PingFederate.."
su - <pf_user> \
-c '<pf_install>/pingfederate/sbin/pingfederate-run.sh \
> /dev/null 2> /dev/null'
}
stop() {
echo "stopping PingFederate.."
su - <pf_user> \
-c '<pf_install>/pingfederate/sbin/\
pingfederate-shutdown.sh'
}
restart(){
stop
# padding time to stop before restart
sleep 60
# To protect against any services that are not stopped,
# uncomment the following command.
# (Warning: this kills all Java instances running as
# <pf_user>.)
# su - <pf_user> -c 'killall java'
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: <pf_user> {start|stop|restart}"
exit 1
esac
exit 0

Tcl Expect Keep SSH Spawn open

I want to open a spawn SSH connection and then query a MySQL Server for new users (in a loop), and if a new user is found a command should be sent to this SSH Connection via Expect.
I don't know if this is possible, up until now i allways kill ssh.exe when i try the "send" command after the MySQL Query.
I want the SSH to be open because it takes 10 seconds to login with Expect (Host ist slow) and i don't want that pause everytime a create a user.
How can i do this?
What i am doing:
...
set db [::mysql::connect -host 127.0.0.1 -user root -password **** -db test]
spawn ssh admin#192.168.1.2
expect {
timeout { send_user "\nFalscher SSH User admin!\n"; exit 1 }
"User:"
}
send "admin\r"
expect {
timeout { send_user "\nFalscher SSH User admin!\n"; exit 1 }
"Password:"
}
send "******\r"
set x = 1
while {$x>0} {
set query [::mysql::query $db {SELECT username, passwort FROM users WHERE erstellt='0'}]
while {[set row [::mysql::fetch $query]]!=""} {
set username [lindex $row 0]
set passwort [lindex $row 1]
send "create user...;\r"
}
::mysql::endquery $query
after 2000
}
I solved this by saving the expect output into a file.
After starting in the background, ssh wanted to verify my ssl fingerprint.
After accepting that, it worked fine.

Expect Output inconsistency

I want to automate the following interaction using Tcl/expect
[root#mgmt NAS]# ssh -q -p 8022 -l user 10.1.1.1
Password:
HP Network Automation Version 9.10.02
Type "HELP connect" to see how to connect to a device.
Type "HELP" to view a list of available commands.
NA>connect 10.1.1.2
WARNING: You do not have an approved reservation for this device at this time.
Attempting to connect to device bigip1.network.company.net (10.1.1.2).
Last login: Wed Sep 26 08:11:42 2012 from 10.2.1.1
Last login: Wed Sep 26 08:11:42 2012 from 10.2.1.1
[root#bigip1:Standby] config #
[root#bigip1:Standby] config #
[root#bigip1:Standby] config #
[root#bigip1:Standby] config # uname -a
Linux bigip1.network.company.net 2.6.18-164.11.1.el5.1.0.f5app #1 SMP Thu Apr 8 18:26:58 PDT 2010 i686 i686 i386 GNU/Linux
[root#bigip1:Standby] config # exit
logout
Disconnected from device bigip1.network.company.net (10.1.1.2).
NA>quit
Logging out of the NA Proxy Interface.
<Blank Line: couldn't show it with simple formatting>
The user input is essentially:
password
connect 10.1.1.2
uname -a
exit
quit
The script I wrote out, connect.exp, is as follows:
#!/usr/local/bin/expect
# Set the input parameters
set nashost [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set passw [lindex $argv 3]
set device [lindex $argv 4]
set cmd [lindex $argv 5]
set binpath /usr/bin
log_user 0
# Set timeout to 45 seconds
set timeout 45
#check if all were provided
if { $nashost == "" || $port == "" || $user == "" || $passw == "" || $device == "" || $cmd == "" } {
puts "Usage: <nashost> <port> <user> <passw> <device> <command>\n"
exit 1
}
# String Variables
set nasprompt "NA>$"
set prompt "config # $"
# Flag Variables
set running 1
set count 0
# SSH to specified NAS host
if { [catch {spawn $binpath/ssh -q -p $port -o "StrictHostKeyChecking no" -l $user $nashost} error] } {
puts "Spawn: SSH failed: $error"
exit
}
expect {
"assword: " {
send "$passw\r"
incr count
if {$count > 3} {
puts "SSH failed on authentication after 3 tries"
set running 0
} else {
exp_continue
}
}
-re "$nasprompt" {
set running 1
}
"Connection refused" {
puts "$expect_out(buffer)"
set running 0
}
"Offending key" {
puts "Host key verification failed."
set running 0
}
eof {
puts -nonewline "Connection terminated unexpectedly:\n$expect_out(buffer)"
set running 0
}
timeout {
puts "ssh: connect to NAS host $host: Connection timed out"
set running 0
}
}
if {$running == 1} {
send "connect $device\r"
expect {
-re "$nasprompt" {
if {$running > 0} {
puts "connect to Device $device failed:\n$expect_out(buffer)"
}
send "quit\r"
}
-re "$prompt" {
if {$running > 0} {
send "$cmd\r"
set running 0
exp_continue
} else {
puts "$expect_out(buffer)"
send "exit\r"
}
}
full_buffer {
puts "$expect_out(buffer)"
exp_continue
}
eof {
puts "ssh: Connection terminated unexpectedly during command execution: $host."
}
timeout {
puts "ssh: Connection timed out during command execution: $host."
}
}
}
The issue I face is that the output I get for this interaction with this script is inconsistent.
I call the script as follows: expect connect.exp 10.1.1.1 8022 user 'pwd' 10.1.1.2 'uname -a'
Output one:
[root#bigip1:Standby] config #
[root#bigip1:Standby] config # uname -a
Linux bigip1.network.company.net 2.6.18-164.11.1.el5.1.0.f5app #1 SMP Thu Apr 8 18:26:58 PDT 2010 i686 i686 i386 GNU/Linux
[root#bigip1:Standby] config #
Output two:
<blank line>
<blank line>
u[root#bigip1:Standby] config #
[root#bigip1:Standby] config #
The u at the beginning of line 3 is part of the output, not a typo.
Other variations of output two exist as well.
The output I expected is:
Linux bigip1.network.company.net 2.6.18-164.11.1.el5.1.0.f5app #1 SMP Thu Apr 8 18:26:58 PDT 2010 i686 i686 i386 GNU/Linux
[root#bigip1:Standby] config #
What am I doing incorrect in my script?
After you send the password, you don't actually wait for the NA prompt before sending the connect command. Change your first expect command to:
set running false
expect {
"assword: " {
incr count
if {$count > 3} {
puts "SSH failed on authentication after 3 tries"
} else {
send "$passw\r"
exp_continue
}
}
"Connection refused" {
puts "$expect_out(buffer)"
}
"Offending key" {
puts "Host key verification failed."
}
eof {
puts -nonewline "Connection terminated unexpectedly:\n$expect_out(buffer)"
}
timeout {
puts "ssh: connect to NAS host $host: Connection timed out"
}
-re "$nasprompt" {
set running true
}
}
if {$running} {
send "connect ...