Spawn command not found - bash

I have an error trying to run a .sh file
line 2: spawn: command not found
": no such file or directory
bash.sh: line 3: expect: command not found
bash.sh: line 4: send: command not found
#!/usr/bin/expect -f
spawn sftp -o IdentityFile=MyFile.ppk 500200243#XXX.XXX.XXX.XXX
expect "XXX.XXX.XXX.XXX.gatewayEnter passphrase for key 'MyFile.ppk.ppk':"
send "myPassword"
Any idea why it happens?

that is an expect script, so ".exp" would be an appropriate file extension: mv bash.sh sftp.exp
do not launch it like bash bash.sh or sh bash.sh. Do this:
make the program executable: chmod a+x sftp.exp
launch it with ./sftp.exp or /path/to/sftp.exp or move it to a directory in your $PATH and launch it just with sftp.exp
after you send "myPassword" you have to "hit enter": send "myPassword\r"
while developing an expect program, add exp_internal 1 to the top.
Good luck, and come back with further questions.

It works OK for me (error from sftp: ssh: Could not resolve hostname XXX.XXX.XXX.XXX: Name or service not known), though the .sh extension for an expect (tcl) script is a little off-putting ;-)
Often when this sort of unexplainable/unpredictable behavior happens, it is because the script was edited under windows (notepad.exe), which uses \r\n to delimit lines. This plays havoc with unix/linux scripts, as only \n is expected as a line delimiter.
You can use the dos2unix and unix2dos utilities to convert between the two formats. As an experiment, I converted your script to "dos" format, and sure enough got a similar error:
ubuntu#ubuntu:~$ unix2dos bash.sh
unix2dos: converting file bash.sh to DOS format ...
ubuntu#ubuntu:~$ ./bash.sh
": no such file or directory
ubuntu#ubuntu:~$ dos2unix bash.sh
dos2unix: converting file bash.sh to Unix format ...
ubuntu#ubuntu:~$ ./bash.sh
spawn sftp -o IdentityFile=MyFile.ppk 500200243#XXX.XXX.XXX.XXX
ssh: Could not resolve hostname XXX.XXX.XXX.XXX: Name or service not known
Couldn't read packet: Connection reset by peer
send: spawn id exp6 not open
while executing
"send "myPassword""
(file "./bash.sh" line 4)
ubuntu#ubuntu:~$

It seems /usr/bin/expect haven't been installed in your machine. So you will get 'command not found'
Use which expect to check, and install it to correct path.

I was also getting the same error. it got resolved by using expect in following way:
DIRNAME=$(date +%F:%T)
expect_sh=$(expect -c "
spawn scp -r ABC xxx#yyy.yy.yy.yyy:/root/$DIRNAME
expect \"password:\"
send \"xxxx\r\"
expect \"#\"
spawn ssh -o StrictHostKeychecking=no xxx#yyy.yy.yy.yyy
expect \"password:\"<
send \"xxxx\r\"
expect \"#\"
send \"rm -f /root/$DIRNAME/abc.txt\r\"
expect \"#\"
send \"scp -r /root/$DIRNAME/* root#zzz.zz.zz.zzz:/root/ABC/\r\"
expect \"password:\"
send \"xxxxx\r\"
expect \"#\"
send \"exit \r\"
")<b
echo "$expect_sh"

It all depends on how you invoke the command. Like ray said, even if you specify the environment with a bang at the top, you still have to run it using expect -f.

Related

Script to upload to sftp is not working

I have 2 Linux boxes and i am trying to upload files from one machine to other using sftp. I have put all the commands I use in the terminal to she'll script like below.
#!/bin/bash
cd /home/tests/sftptest
sftp user1#192.168.0.1
cd sftp/sftptest
put test.txt
bye
But this is not working and gives me error like the directory does not exist. Also, the terminal remain in >sftp, which means bye is not executed. How can I fix this?
I suggest to use a here-document:
#!/bin/bash
cd /home/tests/sftptest
sftp user1#192.168.0.1 <<< EOF
cd sftp/sftptest
put test.txt
EOF
When you run the sftp command, it connects and waits for you to type commands. It kind of starts its own "subshell".
The other commands in your script would execute only once the sftp command finishes. And they would obviously execute as local shell commands, so particularly the put will fail as a non existing command.
You have to provide the sftp commands directly to sftp command.
One way to do that, is using an input redirection. E.g. using the "here document" as the answer by #cyrus already shows:
sftp username#host <<< EOF
sftp_command_1
sftp_command_2
EOF
Other way is using an external sftp script:
sftp username#host -b sftp.txt
Where, the sftp.txt script contains:
sftp_command_1
sftp_command_2

Linux expect script

I've spent the better part of 8 hours trying to figure this out with google, so I hope this warrants asking here.
I need a script that will auto-enter a password when I try to connect from my lubuntu image in vmware to a physical device connected by usb.
I've tried at least 50 different scripts I've found online, but none of them worked (or even recognised spawn as a command)
This is my script:
#!/usr/expect
spawn CPY2T_old.sh
expect "root#10.9.8.2's password:"
send "ThePassword"
expect eof
The contents of CPY2T_old.sh is
#!/bin/bash
cd hellolinux/src/Exercise$1
scp $2 root#10.9.8.2:
The above bash script works fine, but I have to enter the password, which is what I'm trying to avoid in the first place. The expect script gives the following when I execute in cmd:
spawn: command not found
couldn't read file "root#10.9.8.2's password:": no such file or directory
The program 'send' can be found in the following packages:
* mailutils-mh
* nmh
Try: sudo apt-get install <selected package>
couldn't read file "eof": no such file or directory
I've downloaded mailutils and nmh at least a dozen times by now as well. Elsewhere I read I need to #echo off at the top, but this command isn't recognised and gives an error.
EDIT: I can't do passwordless ssh to this device, so please don't suggest it.
I see 2 errors: first
#!/usr/expect
You want
#!/usr/bin/expect
That should have caused an error: how are you launching your expect script?
Second
send "ThePassword"
You forgot to hit enter
send "ThePassword\r"
#!/usr/bin/expect
set timeout 60
spawn ssh user#ip
expect "user#ip's password: "
send "Password\r"
interact
Note:Please be sure of all scripts are executable with command $ chmod +x #file_name or $ chmod 700 #file_name.
\r to execute
Github link:https://github.com/asarari207/Lunix_sh

Expect script for ssh connection with password and additional operations

I found the following script which gives me the possibility to go to a server without manually type in a required password.
Sadly I don't know how to execute commands after the connection is made :(
#!/usr/bin/expect -f
spawn ssh user#server
expect "assword:"
send "pw123\r"
interact
#the following is not executed anymore
cd /tmp/
The cd /tmp/ command is not executed, does someone know how to do this ?
I don't care about security :)
Key-based authentication is not an option.
Edit:
Ok, I found a solution that fits my needs:
#!/usr/bin/expect -f
spawn user#server
expect "assword:"
send "pw123\r"
expect "> " { send "cd /tmp\r" }
interact
The expect "> " has to be like your prompt.
After the connection is made, you are in the shell of the remote host to which you connected through the script. So to execute any command you will have to execute command manually on the command prompt.
If you want only to execute the command on the remote server automatically without need for ssh then you can use the below command.
#!/usr/bin/expect -f
#Changed here
spawn ssh user#server "cd /tmp && ls"
expect "password:"
send "pw123\r"
interact

How to send interactive prompt response in linux shell?

I'm trying to make a script that automatically zips a file with a password. Zip does this by asking for a password in an interactive prompt. How do I make the script respond to the prompt?
I've tried:
zip -e xxx myfile
Enter password:
zip error: Interrupted (aborting)
zip -e myfile < xxx
-bash: xxx: No such file or directory
The -e option expects that zip is running on a terminal. You can use a pseudo-terminal and script that (using the Expect package available in Tcl or Perl or something similar) if you don't mind the complexity.
It might be easier to use the -P flag (or --password) flag with zip, and just pass the password on the command line.

Bash Script to SSH into a machine without prompting password and without using keys

I realize this question has been asked a few times but I could not find a relevant answer anywhere in my searching.
I am working in a development environment where security is not an issue and anyone could just guess the password if the thought for a few seconds.
What I am trying to do is simple. I have created an alias function in my local .bashrc file and I would like this function to automatically log into a machine with a default password.
My current implementation looks something like this:
function s () {
ssh root#192.168.1.$1
}
When I run it I get something like this:
~]s 122
ssh root#192.168.1.122
root#192.168.1.122's password:
Using Bash, and not using RSA keys I would like to get this to use the default password 'password'.
I've tried the following where IP and User have already been set.
Do=$(expect -c "
spawn ssh $User#${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]}
expect \"yes/no\"
send \"yes\r\"
expect \"assword\" send \"password\"")
echo $Do
$Do
It gives the follwing error:
Connecting and logging into server using expect
usage: send [args] string
while executing
"send"
invoked from within
"expect "assword" send "password""
Administrator#192.168.1.176's password:
bash: spawn: command not found...
Using the following command I am able to connect a machine. If I remove the interact it just runs the uptime command and closes the connection. With the interact command I am unable to see what I am typing or actually interact with the machine. Any ideas?
Do=$(expect -c "spawn ssh $User#${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]}; set timeout 4; expect \"assword\"; send \"password\n\"; expect \"test\"; send \"uptime\n\"; interact;");echo $Do;
You can do this with the expect tool: http://expect.sourceforge.net/
It's widely available, so depending on your system, the equivalent of sudo apt-get install expect or yum install expect will install it.
Here's an example of an expect script with ssh. This logs you in and gives you control of the interactive prompt:
#!/usr/bin/expect
set login "root"
set addr "127.0.0.1"
set pw "password"
spawn ssh $login#$addr
expect "$login#$addr\'s password:"
send "$pw\r"
expect "#"
send "cd /developer\r"
interact
Here's an example of how to use expect as part of a bash script. This logs in with ssh, cd to /var, runs a script, then exits the ssh session.
#!/bin/bash
...
login_via_ssh_and_do_stuff() {
# build the expect script in bash
expect_sh=$(expect -c "
spawn ssh root#127.0.0.1
expect \"password:\"
send \"password\r\"
expect \"#\"
send \"cd /var\r\"
expect \"#\"
send \"chmod +x my_script.sh\r\"
expect \"#\"
send \"./my_script.sh\r\"
expect \"#\"
send \"exit\r\"
")
# run the expect script
echo "$expect_sh"
}
You can leave these snippets in a script on your local system, and then just alias to the scripts.
Also: I know you said security isn't an issue, but I'd like to just note, again, that the "proper" way to ssh without using a password is to use a ssh key-pair =)
Use sshpass which is available in package repositories on major Linux-es.
For example, when password is in password.txt file:
sshpass -fpassword.txt ssh username#hostname
sshpass runs ssh in a dedicated tty, fooling it into thinking it is
getting the password from an interactive user.

Resources