Results 1 to 10 of 10

Thread: shell script SSH not working

  1. #1
    Join Date
    Jul 2010
    Posts
    29

    shell script SSH not working

    Hi, I am new to this forums, so not having much idea about the posting. But I think that I am posting in correct section (hope). Now I am having problem with the shell script SSH. It is not working accordingly. So thought that some members over there would contribute some help. I have a zabbix server version of Linux kernel 2.6.26-2-amd64 is running. Now my question how can I make a script that does:
    • Connect with ssh to a device
    • Get systeminfo (but only interfaces, MAC address, serial number and software version)
    • Write the output to a file
    Now I have this code but it doesn't work:
    Code:
    # / Bin / bash
     # / Usr / bin / expect -
     spawn ssh user @ IP> / Infomania / test / juniper / file.txt
     expect password:
     send "password \ r"
     send "get system | mac includes | Int | Ser | Software
     expect eof
    Now, when I start the script I get the following error message:
    Code:
    Errormessage:
    
     ./ZAJOH01_IP.sh: Line 3: spawn: command not found
     Could not read file "password:": no such file or directory
     ./ZAJOH01_IP.sh: Line 5: send: command not found
     ./ZAJOH01_IP.sh: Line 6: send: command not found
     Could not read file "eof": no such file or directory
    If the tip of the command only works means that I write:
    Code:
    ssh user @ IP> / Infomania / test / juniper / file.txt
     Password
     get system | mac includes | Int | Ser | Software
     exit
    then write it in the file / Information / test / juniper / file.txt:
    Code:
    Remote Management Console
     Device-> get system | mac includes | Int | Ser | Soft
     Serial Number: xxxxxxxxxxxxxx, Control Number: 00000000
     Software Version: 5.xx.xrxx.x, Type: Firewall + VPN
     Base Mac: 0010.db44.xxxx
     Use interface IP, Config Port: 8005
     User Name: ig-root
     Interface ethernet1:
       * Ip mac 10.182.128.14/23 0010.db44.xxxx
       * Manage ip 10.182.128.14, mac 0010.db44.xxxx
     Ethernet2 Interface:
       * Ip 0.0.0.0 / 0 mac 0010.db44.xxxx
     Ethernet3 Interface:
       * Ip 0.0.0.0 / 0 mac 0010.db44.xxxx
     Ethernet4 Interface:
       * Ip mac 196.30.139.43/28 0010.db44.xxxx
       * Manage ip 196.30.139.43, mac 0010.db44.xxxx
     Device-> exit
    I am new to shell scripts and perl so plz help i need it for my work

  2. #2
    Join Date
    Mar 2008
    Posts
    227

    Re: shell script SSH not working

    I don't think that there should be some major mistakes. I would like to suggest you can put the full path of ssh and try spawn / usr / bin / ssh user @ IP instead of spawn ssh user @ IP. After that your code should look like :
    Code:
    # / Bin / bash
     # / Usr / bin / expect -
     spawn / usr / bin / ssh user @ IP> / Infomania / test / juniper / file.txt
     expect password:
     send "password \ r"
     send "get system | mac includes | Int | Ser | Software
     expect eof

  3. #3
    Join Date
    Jul 2010
    Posts
    29

    Re: shell script SSH not working

    Hello, first thanks for trying to help. Now I tried and this is the error message:
    Code:
    ./MXCUA01_IP.sh: Line 3: spawn: command not found
     Could not read file "password:": no such file or directory
     ./MXCUA01_IP.sh: Line 5: send: command not found
     ./MXCUA01_IP.sh: Line 6: send: command not found
     Could not read file "eof": no such file or directory
    The spawn work with the command ssh?
    Here the input:

    Code:
    USER @ IP's password: 
     PTY allocation request failed on channel 0
     IP Connection to closed.
    
     Could not read file "password:": no such file or directory
     ./MXCUA01_IP.sh: Line 5: send: command not found
     ./MXCUA01_IP.sh: Line 6: send: command not found
     Could not read file "eof": no such file or directory
     ASRV-12: / Infomania / test / devices #
    spawn scp but ssh works with? ..

  4. #4
    Join Date
    Mar 2008
    Posts
    192

    Re: shell script SSH not working

    Even I think that your are missing some basic thing so an error message is getting generated. A script also has the advantage of being run on any Unix machine without modification. It need not be compiled! The only tool needed is (usually) on all Unix machines: the shell! The steps necessary to execute the scripts:
    1. You write the script in a text editor (remember to specify the shell on the first line)
    2. (Optional) You put the script in a directory that is in $ PATH
    3. You change the file type, making it executable: chmod a + x script
    4. You can run it by typing: script (or if the path is not found in $ PATH)

    A script is interpreted by the shell line by line until the end of the script or the meeting of the exit command. In case of error, the execution stops with an error message. The shell running the script is the shell that appears in the first line of the script (if it starts with #!), Otherwise your current shell to execute it.

    Losing the first line in the script, because you're trying to run a script to expect inside a shell. I think this might also help you.

  5. #5
    Join Date
    Jul 2010
    Posts
    29

    Re: shell script SSH not working

    Lose the first line in your script, Because You're Trying to run an expect script inside an shell.
    Did you mean #! / Bin / bash # o! / Usr / bin / except -? Anyway I tried both bash and once without a time without exception and without bash Gave an error that he did not know and without exception ssh doesn't change anything, it wanted a password (which I gave manually) and after closure session, said:
    Code:
    Could not read file "password:": no such file or directory
     ./MXCUA01_IP.sh: Line 5: send: command not found
     ./MXCUA01_IP.sh: Line 6: send: command not found
     Could not read file "eof": no such file or directory
    This is now my code:
    Code:
    # / Bin / bash
     / Usr / bin / ssh user @ IP> / infoman/test/juniper/MXCUA01.txt
     expect password:
     send "password \ r"
     send "get system | mac includes | Int | Ser | Software
     expect eof

  6. #6
    Join Date
    Dec 2009
    Posts
    67

    Re: shell script SSH not working

    I have a shell script that is on three machines containing some SCO sqlplus statments. I need to run these scripts remotely. Currently, I try to use rsh to do: rsh hostname myscript args The problem is that the arguments of the statements in sqlplus remote shell scripts are ignored. If I run the script from the command line of their machines, everything works fine. So it looks like a rsh question. Here are some more detailed information: I am running rsh as follows:
    Code:
    Raptor rsh bin / YYYY my_script 
    
    where the Ys are the args my_script. my_script contains the following elements: 
    
    #! / Usr / bin / ksh 
    
    which_sql \ u003d where `sqlplus" 
    
    -S $ which_sql sti / sti <<EOF 
    put in the off position 
    paused off 
    update my_table 
    all HOST_ON_DUTY \ u003d '$ 1' 
    EXPRESS_SHIPPING \ u003d '$ 2' 
    INTERNATIONAL_ORDERS \ u003d '$ 3' 
    COD \ u003d '$ 4'; 
    output 
    EOF
    and error message pops up:
    bin / my_script: line 5:-s: not found
    I guess the flags of the SQL statement is interpreted locally rather than remotely. Any help would be greatly appreciated!!

  7. #7
    Join Date
    Mar 2008
    Posts
    258

    Re: shell script SSH not working

    I suggest you create a remote shell script. I do not use RSH more because it is not a secure connection type. I suggest you set the environment first, for example:
    Code:
    #! / Bin / sh 
    
    PATH \ u003d / usr / local / sbin: / usr / sbin: / sbin: / usr / local / bin: / usr / bin: / bin
    your shell code here. This stuff just seems to come back. People want to run scripts remotely. I tried every way I can think about what is the best way. You not only functionality, but you have security. Each Unix environment needs a means of delivering shell scripts, perl scripts, SQL scripts for remote servers. I issued the following example, already, but I will do it again. Every time I go to a new company, the first thing I do is build a licensing process. One way remote script out of the question as to use some scheduler Maestro or Control-m.

  8. #8
    Join Date
    Dec 2008
    Posts
    161

    Re: shell script SSH not working

    It really is a multistep process. You must correctly configure SSH and create a key pair and password. Then you must load your keys in the SSH agent, so you will not be prompted for a password. The SSH agent will forward your password to other systems.
    Code:
    # Ssh-add 
    Adding identity: / home/tester/.ssh2/id_dsa_1024_defender.pub 
    Need passphrase for / home/tester/.ssh2/id_dsa_1024_defender.pub 
    (1024-bit DSA test @ defender, Sun Apr 30 2000 9:39:13 p.m.). 
    Enter passphrase: 
    
    # Ssh server date 
    Wed May 10 2:06:57 p.m. EDT 2006 
    # 
    
    # Now that you have the SSH configuration. You can take any script and the script scp remote server. 
    
    
    
    Sample script below 
    ############################################### 
    #! / Bin / sh 
    
    LOG \ u003d "/ tmp / MYTEST.log 
    ################## 
    function if_error 
    ################## 
    ( 
    if [[$? -Ne 0]] then # check return code passed to the function 
    print "$ 1 TIME: $ TIME" | tee-a $ LOG # if rc> 0 then print error msg and quit 
    exit $? 
    fi 
    ) 
    
    chown root: root / etc / passwd 
    if_error "Unable Chowning / etc / passwd root: root" 
    
    chmod 700 / etc / passwd 
    if_error "Unable chmoding / etc / passwd to 700 
    
    rm-rf / etc / somedir 
    if_error "Can not delete the file / etc / somedir" 
    ################################################ 
    
    # Okay .. SSH is working and you have a little test with the control script error in it. Also, I used the tee command to redirect the script output to the screen and a log file in tmp. 
    
    scp $ USER @ $ HOST: $ PATH / SCRIPT $ $ USER @ $ HOST: / tmp / $ SCRIPT 
    ssh-l $ USER $ host chmod 700 / tmp / $ SCRIPT 
    ssh-l $ USER $ HOST / tmp / $ SCRIPT 
    ssh-l $ USER HOST rm / tmp / $ SCRIPT
    1. Copied script on the server
    2. Change permissions
    3. Run script that will write to the log file and screen because I wrote it like that.
    4. rm script
    I think that this will be helpful.

  9. #9
    Join Date
    Jan 2010
    Posts
    64

    Re: shell script SSH not working

    I am having some similar kind of query, so thought to post over here instead of making new thread. I would like to create a shell script to connect to a remote server via SSH (with port forwarding) and then launch an application on my local terminal. The problem is that once the connection is established the remote host, I find myself on the shell of it. Basically, after entering the password, I would stay on my local terminal to run the second command.
    Code:
    # / Bin / sh 
    ssh-L user@XX.XXX.XXX.XX 8080:127.0.0.1:8080 
    open / Applications / VLC.app "mmsh: / / 127.0.0.1/8080"
    I think something is missing after the second line of code. FYI, it's a script that can establish a remote connection to a Linux box that has a camera and run the stream through VLC.

  10. #10
    Join Date
    Jan 2006
    Posts
    211

    Re: shell script SSH not working

    Your command opens a ssh session and not return to the script by hand to launch the following command ONLY when the ssh session is over, and then the connection will be terminated, and the tunnel closed, making the following command does not work ... So it should run your ssh in the background, waiting for the connection is established, then throw your vlc. But it's horrible, and you have no control to close the ssh properly after use. So actually, no, he must do otherwise ... Maybe like this:
    1. you do run a little demon on your local Mac, which listens on port 9999. When it receives the message "go", the daemon executes the command to open VLC.
    2. user@XX.XXX.XXX.XX ssh-L-R 8080:127.0.0.1:8080 9999:127.0.0.1:9999
    3. in the open shell with ssh on the remote machine, run "echo go | nc localhost 9999" to send the message "go" through the tunnel "back". Your demon will receive the message and launch VLC.
    4. VLC you can leave and close the ssh shell
    Simplistic example for the little devil (who does not even control the message sent on port 9999):
    Code:
    nc-l 9999 & & open-a VLC "mmsh: / / 127.0.0.1/8080"
    In a shell I type the command below, and B in a shell I type "echo blah | nc localhost 9999":
    Code:
    Plotter minimize@ ~% nc-l 9999 & & echo message_recu 
    message_recu

Similar Threads

  1. Creating shell script in ubuntu
    By Elizabeth Allen in forum Operating Systems
    Replies: 5
    Last Post: 24-03-2010, 01:35 PM
  2. Selecting a user in a shell script
    By Quattro in forum Operating Systems
    Replies: 5
    Last Post: 25-12-2009, 02:11 PM
  3. Shell script with sed
    By Ameeryan in forum Software Development
    Replies: 5
    Last Post: 23-12-2009, 02:29 PM
  4. Shell Script Formatting
    By abhi701 in forum Software Development
    Replies: 1
    Last Post: 04-07-2009, 04:30 PM
  5. Unix Shell Script
    By BlackSunReyes in forum Software Development
    Replies: 3
    Last Post: 12-08-2008, 01:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,502,167.55875 seconds with 17 queries