Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

shell script newbie.

  • 28-11-2006 12:26am
    #1
    Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭


    i'm trying to write a simple shell script that will basically open up a terminal window and ssh into a server supply in the script.

    i have a fair idea how to do it but i'm stuck at this stage. here's what i got
    #!/bin/bash
    
    PWD=/usr/bin
    gnome-terminal
    
    server=server.address.com
    
    ssh - l `$USERNAME` `$server`
    
    i'm probably miles off or their may be an easier way to do this but i'm sick of typing in the address of the server in college in the terminal window everytime...

    i'm using ubuntu 6.06 64bit if that helps.


Comments

  • Registered Users, Registered Users 2 Posts: 865 ✭✭✭generalmiaow


    well, let's see. I'm not too great with shell scripts either but I might be able to help. with the script you've written so far, first I think your opening the gnome-terminal will cause a terminal to open alright, but the script won't be executed in that terminal, it will be executed by the terminal that runs the script (which AFAIK you won't see) You presumably want the script to run in the terminal you're opening. If you're making this a desktop shortcut say, you can right click, go to properties and specify that the script be run in a terminal window, so you won't need that gnome-terminal command.

    Second, the quotes in your script are backticks, which have a different meaning (say if I said $variable=`command -v` then $variable will be the output of command -v, it's like outputting a function into a variable in C or PHP etc). So you should not use backticks, you can use unescaped quotes or I think in most cases not use quotes at all.

    Also, I usually ssh in the format generalmiaow@server, so you'll need an @ in there between the variables. In fact in this case you may need quotes after all (but not backticks)


  • Registered Users, Registered Users 2 Posts: 2,755 ✭✭✭niallb


    What you want is
    gnome-terminal -e "ssh -l $USERNAME $server"

    If you're using gnome-terminal, it might be easier for you to
    add a "profile" for the other machine.


    Select "File ->New Profile..." and type in the name of your server in "Profile Name". Base it on your default.
    Change the tab to "Title and Command", and type the command (with your username and servername rather than variables)
    into the box marked "Run a custom command instead of my shell."

    It's already running a shell, so you'll just type in "ssh -l $USERNAME $server"

    NiallB


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    niallb wrote:
    What you want is
    gnome-terminal -e "ssh -l $USERNAME $server"

    brilliant exactly what i've wanted.

    now i've expanded on the script by having it store the address of 5 servers with a menu to select which one i want to ssh into.

    thanks guys for your help. :D


  • Registered Users, Registered Users 2 Posts: 2,755 ✭✭✭niallb


    If you like that option, you may find this interesting.

    There is an often neglected file called config in the .ssh directory.
    It can do things like this:
    [b]# For a non standard port, change from 22 without using -p[/b]
    Host nx
    Hostname nx
    Port 2093
    
    [b]# Hop through an accessible firewall box to a system behind it.
    # This runs the ssh command on the intermediate machine.
    # call with "ssh site1" and automatically connect through to remoteaccount@ssh.site1.tld
    # which would normally be inaccessible.[/b]
    Host site1
    Hostname site1host.site1.tld
    ProxyCommand ssh -a -x firewallbox "ssh -i ~/.ssh/site1apps -q -T -p 2022 remoteaccount@ssh.site1.tld"
    
    [b]# Proxy command can be anything - this example just netcats a port from further in.[/b]
    Host fileserver
    Hostname fileserver.site1.tld
    ProxyCommand ssh -q -a -x firewallbox "nc ssh.site1.tld 2022"
    
    [b]# The controlmaster reuses a control channel for multiple links to the same site.
    # Particularly handy on dialup as it saves loads of bandwidth.
    # Replace the * to only use it for named hosts.[/b]
    Host *
    ControlMaster auto
    ControlPath ~/.ssh/master-%r@%h:%p
    
    [b]# Avoid annoying messages when connecting to a system with dynamic IPs.[/b]
    Host remotedynamicip.dnsalias.net
    CheckHostIP no
    
    [b]# This saves headaches if you do a lot of chaining machines together to make connections...[/b]
    Host localhost
    NoHostAuthenticationForLocalhost yes
    
    
    


  • Registered Users, Registered Users 2 Posts: 998 ✭✭✭zekiel


    Cremo, wouldnt be a chance you could post that script up.. sounds like a script that would make my life that little bit easier. :D


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    zekiel wrote:
    Cremo, wouldnt be a chance you could post that script up.. sounds like a script that would make my life that little bit easier. :D

    yup i'll post it up later once i get home from college.


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    here's the shell script, the servers in the script are just the ones i use in college so if your editing the file you may want to change the server addresses and their varible names, i welcome people to voice their opinions as this is my first kinda functional useful shell script.
    #written by: neil cremins
    #date: 28/11/06 
    
    #change these to whatever you fancy
    
    aisling=aisling.student.comp.dit.ie
    taranaki=taranaki.student.comp.dit.ie
    taupo=taupo.student.comp.dit.ie
    calculon=netsoc.dit.ie
    bender=bender.netsoc.dit.ie
    
    
    
    echo "======================================"
    echo "Script to ssh to servers."
    echo "======================================"
    
    echo -e "\nHello $USERNAME Please select a server\n"
    
    choice=99 #dummy value so loop's parameter is tested on first iteration.
    until [ $choice -lt 6 ]
    do
    echo -e "==============="
    echo -e "Choose an option"
    echo -e "==============="
    echo -e "1. aisling."
    echo -e "2. taranaki"
    echo -e "3. taupo"
    echo -e "4. calculon (netsoc account holders only)"
    echo -e "5. bender (netsoc account holders only)"
    echo -e "0. exit program\n"
    read choice
    
    
    case $choice in
    
        1)    servername=aisling
            echo -e "you have selected $servername.\n"
            echo -e "please enter your username"
            read login_name
            echo -e "logging into aisling server...\n"
            ssh -l $login_name $aisling
            ;;
    
        2)    servername=taranaki
            echo "you have selected $servername."
            echo -e "please enter your username"
            read login_name
            echo -e "logging into taranaki server...\n"
            ssh -l $login_name $taranaki
            ;;
    
        3)    servername=taupo
            echo "you have selected $servername."
            echo -e "please enter your username"
            read login_name
            echo -e "logging into taupo server...\n"
            ssh -l $login_name $taupo
            ;;
    
        4)    servername=calculon
            echo "you have selected $servername."
            echo -e "please enter your username"
            read login_name
            echo -e "logging into calculon server...\n"
            ssh -l $login_name $calculon
            ;;
    
        5)    servername=bender
            echo "you have selected $servername."
            echo -e "please enter your username"
            read login_name
            echo -e "logging into calculon server...\n"
            ssh -l $login_name $calculon
            ;;
    
        0)    echo -e "Good bye and have a nice day!\n"
            exit 1
            ;;
    
        *)    echo -e "Invalid input try again!\n"
            ;;
    
    esac
    done
    
    


Advertisement