Phi
Phi

Reputation: 514

Spawn command not found

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 [email protected]
expect "XXX.XXX.XXX.XXX.gatewayEnter passphrase for key 'MyFile.ppk.ppk':"
send "myPassword"

Any idea why it happens?

Upvotes: 5

Views: 50248

Answers (5)

Dheeraj
Dheeraj

Reputation: 31

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 [email protected]:/root/$DIRNAME
expect \"password:\"
send \"xxxx\r\"
expect \"#\"
spawn ssh -o StrictHostKeychecking=no [email protected]
expect \"password:\"<
send \"xxxx\r\"
expect \"#\"
send \"rm -f /root/$DIRNAME/abc.txt\r\"
expect \"#\"
send \"scp -r /root/$DIRNAME/* [email protected]:/root/ABC/\r\"
expect \"password:\"
send \"xxxxx\r\"
expect \"#\"
send \"exit \r\"
")<b

echo "$expect_sh"

Upvotes: 3

glenn jackman
glenn jackman

Reputation: 247022

  1. that is an expect script, so ".exp" would be an appropriate file extension: mv bash.sh sftp.exp
  2. do not launch it like bash bash.sh or sh bash.sh. Do this:
    1. make the program executable: chmod a+x sftp.exp
    2. 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
  3. after you send "myPassword" you have to "hit enter": send "myPassword\r"
  4. while developing an expect program, add exp_internal 1 to the top.

Good luck, and come back with further questions.

Upvotes: 12

Lattis
Lattis

Reputation: 73

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.

Upvotes: 1

Brightshine
Brightshine

Reputation: 965

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.

Upvotes: 3

Digital Trauma
Digital Trauma

Reputation: 16016

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 () script is a little off-putting ;-)

Often when this sort of unexplainable/unpredictable behavior happens, it is because the script was edited under (notepad.exe), which uses \r\n to delimit lines. This plays havoc with / 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 [email protected]
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:~$ 

Upvotes: 3

Related Questions