can't create folder with expect script - shell

I have this script
#!/usr/bin/expect
set DATE [exec date +%F]
spawn sftp user#192.168.0.20
expect "password:"
send "password\n"
expect "sftp>"
send "cd /getfile/$DATE/ \n"
expect "sftp>"
send "lcd /putfile/ \n"
expect "sftp>"
send "mkdir $DATE"
expect "sftp>"
send "lcd /putfile/$DATE \n"
expect "sftp>"
send "mget *.* \n"
expect "sftp>"
send "quit \n"
everything works here, but it does not create the directory. The error is:
cant create directory
I don't know what I'm doing wrong. Any help would be appreciated.

If you want to download files to a local new directory, why not create this directory with expect's exec instead of with sftp?
#!/usr/bin/expect
set DATE [exec date +%F]
exec mkdir "$DATE"
cd "$DATE"
spawn ...

Related

Expect Script : spawn id exp4 not open

I have created an expect script to download some files via sftp.
I now need to copy the files so I have the script:
#!/usr/bin/expect
set now [clock seconds]
set date [clock format [clock seconds] -format {%Y-%m-%d}]
spawn sftp test#user
expect "sftp>"
send "cd public_html\n";
expect "sftp>"
send "get *\n";
expect "sftp>"
send "quit\n";
expect " $"
send "cp -R /media/user/A/$date /media/user/B\n"
But I am getting the error:
sftp> quit
send: spawn id exp4 not open
while executing
"send "cp -R /media/user/A/$date /media/user/B\n""
(file "./test1" line 44)
What is causing this?

SFTP file transfer between two server and running this script from 3rd server

I have developed a script using TCL expect. The use of the script is - if user runs it from server A, it will check sftp file transfer between server B and server C. Below is my code:
#!/usr/bin/expect -f
lassign $argv 1 2
spawn ping -c 2 -i 3 -W 1 $1
expect {
" 0%" {puts "Source is rechable!"}
" 100.0%" {puts "Source is not rechable.Please restart IPSEC and check!";exit 1}
}
#SSH to remote server $1
spawn ssh -o StrictHostKeyChecking=no root#$1
expect {
"Password:" {send "password\r";exp_continue}
"*#" {send "ssh successful\r";exp_continue}
}
send "\n"
#Creating a file in remote server which will be transferred via sftp
send "touch /tmp/mfile\n"
expect "#"
send "chmod 755 /tmp/mfile\n"
expect "#"
#sftp to server$2
spawn sftp -o StrictHostKeyChecking=no root#$2
expect {
"Password:" {send "Training\r";exp_continue}
"sftp>" {send "sftp successful\r";exp_continue}
}
send "\n"
#sending remote file
send "put /tmp/mfile\n"
send "\n"
sleep 2
send "File send to Remote Server successfully\n"
expect "sftp>"
send "cd /root/\n"
expect "sftp>"
send "rename mfile mfile_1\n"
expect "sftp>"
#sending back the file
send "get mfile_1 /tmp\n"
expect "sftp>"
sleep 2
send "quit\n"
send "exit\n"
The issue is the file is getting transferred to the Server C from the Server B with this code, but the file is not sent back to Server B. I am yet to add other logic in the code but first wanted to check if the basic code works. Any clue regarding file transfer back would be helpful.

Expect script to run a shell script in remote server

I am trying to create an expect script to execute a shell script with arguments in remote server, however the script is not getting executed and it is returning the $ prompt in remote server. Here is the script
#!/usr/bin/expect
spawn ssh username#servername
expect "password:"
send "abc\r"
expect "$ "
send "cd /home/abc\r"
send "./script.sh --status arg1 arg2\r"
-- here the script is located in home/abc directory and the shell script ./script needs to be executed with the parameter --status arg1 arg2. Also i need to save the contents of ./script.sh --status arg1 arg2 in a file and email it. Can the mailx command works inside a expect script or i need to call this expect script from a another shell script. Please assist. Thanks
Try something quick and risky:
#!/usr/bin/expect -f
set password your_password
set timeout 100
spawn scp -q /home/abc/script.sh username#server:/tmp
expect {
"(yes/no)?" {send "yes\r"; exp_continue}
"password:" {send "$password\r"; exp_continue}
"lost connection" {puts "ERROR: lost connection"}
"No route to host" {puts "ERROR: no route to host"}
timeout {puts "ERROR: timeout"}
}
spawn ssh username#server
expect {
"(yes/no)?" {send "yes\r"; exp_continue}
"password:" {send "$password\r"; exp_continue}
"username#server" {send "sh /tmp/script.sh; exit\r";exp_continue }
"password for username:" {send "$password\r"; exp_continue}
"password:" {send "$password\r"; exp_continue}
"lost connection" {puts "ERROR: lost connection"}
"No route to host" {puts "ERROR: no route to host"}
timeout {puts "ERROR: timeout"}
}
Variable password put there to run script automatically.
Also you see "$password\r" as reference to set variable.
spawn scp -q /home/abc/script.sh username#server:/tmp copies your script to /tmp in remote server. -q flag disables the progress meter.
spawn ssh username#server connects to your server.
sh /tmp/script.sh runs script copied in the previous step.
Also, I send e-mails using mutt

perl program not running thorough automated script until it is executed manually first

i am writing code to automate some steps . First it is required to switch user and then run a perl script. Here is my code
if [ -a /try/Test ]
then
su trial -c ". /try/.profile Test"
expect -c 'spawn try1;
send "3\r";
send "1\r";
send "show\r";
interact';
fi
try1 is my perl program which i am trying to call.This script throws this error
couldn't execute "try1": no such file or directory
while executing
"spawn try1"
but once i do this step manually and then run this script then this script runs without nay error.
I think you've already asked about it (and I did answer, didn't I)?
Here's the basic skeleton (make sure to add error/timeout/unexpected output handling):
# collect password
stty -echo
send_user -- "Password: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)
spawn sudo sh;
expect -re ": *$";
send -- "$pass\r"
expect -re "\$ *$";
send "echo SETTING PARAMS\r";
expect -re "\$ *$";
send "echo RUNNING MY COMMAND\r";
expect -re "\$ *$";
interact

expect in bash script × 16172

I try to set a expect script in bash.
#!/bin/bash
/usr/bin/expect <<- EOD
set router 192.168.0.251
set user admin
set pass test
set timeout 1000
set filesave [exec date +%m-%d-%Y]
spawn telnet $router
send "\n"
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"
expect ">"
send "en\n"
expect "Password:"
send "$pass\n"
send "term len 0\n"
log_file $router--$filesave.cfg
send "show running-config\n"
expect "end\r"
send "\n"
log_file
send "exit\n"
EOD
cat /Users/test/Desktop/python/$router--$filesave.cfg | grep end
exit 0
I just got this output
./script2
spawn telnet
telnet> telnet>
Try changing your shebang from:
#!/bin/bash
to
#!/bin/expect
and remove:
/usr/bin/expect <<- EOD
And see if that works.
Update: if you need to have expect run as part of your bash script, either encapsulate the expect code in a separate script with a expect shebang and source it from your bash script, or encode it as in the following example:
expect_sh=$(expect -c "
spawn ssh $login#$IP
expect \"password:\"
send \"$password\r\"
expect \"#\"
send \"cd $dest_dir\r\"
expect \"#\"
send \"chmod +x $server_side_script $other_script\r\"
expect \"#\"
send \"./$device_side_script\r\"
expect \"#\"
send \"cat $deploy_count\r\"
expect \"#\"
send \"exit\r\"
")
echo "$expect_sh"
The problem is that bash is interpreting all the $variables before expect sees the script. Thus you're simply spawning telnet with no hostname given. Change:
/usr/bin/expect <<- EOD
to:
/usr/bin/expect <<- 'EOD'
This has the effect of single-quoting the entire here-document.
http://www.gnu.org/software/bash/manual/bashref.html#Here-Documents
Note also that the next bash command (cat /Users/...) relies on variables defined in Expect -- they are not defined in bash. Try this
#!/bin/bash
export router=192.168.0.251
export filesave=$(date +%m-%d-%Y)
/usr/bin/expect <<- 'EOD'
set router $env(router)
set filesave $env(filesave)
# the rest stays the same

Resources