Having issues with an expect script. I've been scripting bash, python, etc... for a couple years now, but just started to try and use Expect. Trying to create a script that takes in some arguments, and then for now, just runs a pwd command(for testing, final will be command I pass).
Here is the bash script:
Code:
#!/bin/bash
if [[ $# -eq 0 ]];then
echo "USAGE: $0 <dirname> <servername1> <servername2> ..."
fi
dirVar="$1"
shift
mypassword=`cat /home/chris/sshPush/.pass`
for i in $*; do
expect -d ./expect-pwd.exp $dirVar chris $i
done
Code:
#!/usr/bin/expect -f
set filename [lindex $argv 0]
set user [lindex $argv 1]
set server [lindex $argv 2]
set timeout -1
set exp_internal 1
spawn ssh $server
sleep 1
expect ^*/home/chris*
send "su - asa\r"
expect ^*sword:
send "password\r"
expect ^*$server*
#send -- "mkdir -p $filename\r"
send -- "pwd\r"
close
Here is the output I get from that:
Code:
expect: does "\r\n[YOU HAVE NEW MAIL]\r\n\u001b[?1034h[08:27:37]\u001b[1;34m\u0002asa@remoteHost\u001b[00m:\u001b[1;34m~\u001b[00m$" (spawn_id exp6) match glob pattern "^*remoteHost*"? yes
expect: set expect_out(0,string) "\r\n[YOU HAVE NEW MAIL]\r\n\u001b[?1034h[08:27:37]\u001b[1;34m\u0002asa@remoteHost\u001b[00m:\u001b[1;34m~\u001b[00m$"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\n[YOU HAVE NEW MAIL]\r\n\u001b[?1034h[08:27:37]\u001b[1;34m\u0002asa@remoteHost\u001b[00m:\u001b[1;34m~\u001b[00m$"
send: sending "pwd\r" to { exp6 }
expect: spawn id exp6 not open
while executing
"expect eof"
(file "./expect-pwd.exp" line 21)
chris@localHost:~/sshPush$ vi expect-pwd.exp
chris@localHost:~/sshPush$ ./mkDirSrv.sh /home/asa/ remoteHost
expect version 5.45
argv[0] = expect argv[1] = -d argv[2] = ./expect-pwd.exp argv[3] = /home/asa/ argv[4] = chris argv[5] = remoteHost
set argc 3
set argv0 "./expect-pwd.exp"
set argv "/home/asa/ chris remoteHost"
executing commands from command file ./expect-pwd.exp
spawn ssh remoteHost
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {12876}
expect: does "" (spawn_id exp6) match glob pattern "^*/home/chris*"? no
Warning: Permanently added 'remoteHost,192.168.1.10' (RSA) to the list of known hosts.
*******************************************************************************
(remoteHost(chris):/home/chris)$
expect: does "Warning: Permanently added 'remoteHost,192.168.1.10' (RSA) to the list of known hosts.\r\r\n*******************************************************************************\r\n(remoteHost(chris):/home/chris)$ " (spawn_id exp6) match glob pattern "^*/home/chris*"? yes
expect: set expect_out(0,string) "Warning: Permanently added 'remoteHost,192.168.1.10' (RSA) to the list of known hosts.\r\r\n*******************************************************************************\r\n(remoteHost(chris):/home/chris)$ "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Warning: Permanently added 'remoteHost,192.168.1.10' (RSA) to the list of known hosts.\r\r\n*******************************************************************************\r\n(remoteHost(chris):/home/chris)$ "
send: sending "su - asa\r" to { exp6 }
expect: does "" (spawn_id exp6) match glob pattern "^*sword:"? no
su - asa
expect: does "su - asa\r\n" (spawn_id exp6) match glob pattern "^*sword:"? no
asa's Password:
expect: does "su - asa\r\nasa's Password:" (spawn_id exp6) match glob pattern "^*sword:"? yes
expect: set expect_out(0,string) "su - asa\r\nasa's Password:"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "su - asa\r\nasa's Password:"
send: sending "password\r" to { exp6 }
expect: does "" (spawn_id exp6) match glob pattern "^*remoteHost*"? no
expect: does "\r\n" (spawn_id exp6) match glob pattern "^*remoteHost*"? no
[YOU HAVE NEW MAIL]
expect: does "\r\n[YOU HAVE NEW MAIL]\r\n" (spawn_id exp6) match glob pattern "^*remoteHost*"? no
[08:27:51]asa@remoteHost:~$
expect: does "\r\n[YOU HAVE NEW MAIL]\r\n\u001b[?1034h[08:27:51]\u001b[1;34m\u0002asa@remoteHost\u001b[00m:\u001b[1;34m~\u001b[00m$" (spawn_id exp6) match glob pattern "^*remoteHost*"? yes
expect: set expect_out(0,string) "\r\n[YOU HAVE NEW MAIL]\r\n\u001b[?1034h[08:27:51]\u001b[1;34m\u0002asa@remoteHost\u001b[00m:\u001b[1;34m~\u001b[00m$"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\n[YOU HAVE NEW MAIL]\r\n\u001b[?1034h[08:27:51]\u001b[1;34m\u0002asa@remoteHost\u001b[00m:\u001b[1;34m~\u001b[00m$"
send: sending "pwd\r" to { exp6 }
It says it is sending the "pwd\r", but it never really does. I've also tried this with a mkdir and a touch command, not working. Almost like the connection is closing before it's able to send the command.
So, I have an expect script (let's call it expect.exp) that takes 3 arguments. It logs into a remote server, runs a set of commands, then hands control over to the user by the "interact" command. If I call this script from the command line, it works properly.
Now I'd like to apply this script... (2 Replies)
Hi, I'm writing a shell script that calls a few commands that prompt the user for two simple yes/no questions. if the answers are consistent (the first is a yes, the second is a no), what would my expect script look like? Google is only giving me answers for scripts where I telnet or ssh. right now... (3 Replies)
I am using blow script :--
#!/bin/bash
FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not
if
then
# echo "process found"
exit 0;
else
echo "process not found"
exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
set fid
set content
close $fid
## Split into records on newlines
set records
send "records splited\n"
send $records\n
set a "test\n"
send $a
foreach rec $records {
## Split into fields on colons
set fields
#set fields
puts $fields
for {set x 1} {$x < 4} {incr x} {... (1 Reply)
Hi all,
I have a little problem with a expect in a bash Script.
The hull of my script:
#!/bin/sh
( expect -c '
set a \"eee\"; # the variable a
' )
echo $a; # using the variable out of the expect script
I would like to use the variable out of the expect script(in bash),... (3 Replies)
I've created a tag in the makefile:
mytag: $(shell ${PWD}/script.sh)
When i do: make clean - the script is executed
When i perform make or make mytag the script is again executed with the output:
make: Nothing to be done for mytag
What i want ?
I want script.sh to be executed only... (0 Replies)
Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin.
Basically the path structure is
... (6 Replies)
I know something simple is missing here, "log_user 1" is set . . . after this utility opens ${InFile} (handle? for IntInFile) it needs to look for something to appear in the file ${IntInFile} and then send it to the spawned process. Then I am locking the file ${IntInFile} and clearing it out -... (0 Replies)
Dear all
Hi
I want use expect in bash so that we can not use these with each other
/bin/bash. With. /usr/bin/expect
How can use these with on script or how can call a script from other script
#!/bin/bash
clear
echo "================================== "
echo "Enter your Esxi IP"... (3 Replies)
im very happy to back for this forum
I have servers with alias of double dns extentions:
sample:
servera.test.com
servera.test1.com
serverb.test.com
serverb.test1.com
I need to login to that severs and executing the set of commands
if test.com failed then try to login via... (0 Replies)