linux - Remote login in, creating alias, need advice

06
2014-04
  • Conor

    I usually remote login to the server at my school. I have a user name and password. The command is 'ssh -l username -p port machineName'. (username is mine, port is a number and machineName is the machines in the school) I assigned this command to an alias called log but then when i log in it always asks me for a password. Is there any way I can make my alias enter the password after the command too?

  • Answers
  • techie007

    SSH doesn't let you pass a password, as it would be considered insecure (you should use Public Keys instead, but the school would probably have to set it up on their end too).

    PuTTY allows a password to be passed from the command-line:

    3.8.3.8 -pw: specify a password
    Note that the -pw option only works when you are using the SSH protocol.

    and there's also SSHPass:

    Sshpass is a tool for non-interactivly performing password authentication with SSH's so called "interactive keyboard password authentication".

  • Bill McCloskey

    If it is enabled for your ssh installation, you should follow a procedure to create what are known as public encrypted keys, and then use those keys to auto-log you in. It is rather quite an easy procedure, and you should be able to bypass the whole password entry issue.

    Search the net with the search phrase, "generate rsa keys pairs ssh" to find a plethora of pages which describe the process. Here is just one such link.

    I typically do not use a pass phrase. I'll let other commentators provide any justification to do the opposite.

    whmcclos@mbp-wireless:~
    [8] ls -ld ~/.ssh
    drwx------+ 2 whmcclos staff 68 Feb  4 09:05 /Users/whmcclos/.ssh
    whmcclos@mbp-wireless:~
    [9] ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/whmcclos/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/whmcclos/.ssh/id_rsa.
    Your public key has been saved in /Users/whmcclos/.ssh/id_rsa.pub.
    The key fingerprint is:
    77:93:ef:57:40:7d:ef:9b:6e:99:4a:a3:8a:8b:81:a9 [email protected]
    The key's randomart image is:
    +--[ RSA 2048]----+
    |               . |
    |              . o|
    |             .  o|
    |             .. .|
    |        S . +  o |
    |    o    . . o  o|
    |   o .       o. *|
    |  .   o .   o..*.|
    | E   . o.... .=o |
    +-----------------+
    whmcclos@mbp-wireless:~
    [10] ls -l .ssh
    total 8
    -rw-------+ 1 whmcclos staff 1679 Feb  4 09:06 id_rsa
    -rw-r--r--+ 1 whmcclos staff  417 Feb  4 09:06 id_rsa.pub
    
    [11] cat $HOME/.ssh/id_rsa.pub | ssh myRemoteHost 'cat >> .ssh/authorized_keys && echo "Key copied"'
    

    type in your password for the last time. Now, you should be able to ssh into the remote host sans the password.

    Please note that the $HOME/.ssh folder is assumed to have been created on the two hosts, the one you are coming from and the one you are logging into. If they haven't, create those folders first, before doing the above. Also note that the permissions on the $HOME/.ssh folder should be set to chmod 700 $HOME/.ssh on both hosts.

    On step [9], just accept the default entry for each question, but it would be worth-while to learn what the questions mean.


  • Related Question

    command line - Create an alias in Windows XP
  • Pops

    Back in school, I used to have a .login file along the lines of

    alias ll = ls -l
    alias dir = ls -Fhl
    alias web = cd ~/public/public_www/development
    ...

    I'd like to do that sort of thing with my XP box here at work, but most of the resources I've found online seem fairly complicated and heavy-duty. Is there a way to do this that doesn't involve mucking about in the registry or running a large batch file?

    EDIT: My original reason for asking this was that I only need the command line for one command in one specific folder, and I wanted to be able to get to that folder quickly when I launched the command line. But the accepted answer for this question is so good that I decided to ask about my original issue as a separate question here.


  • Related Answers
  • grawity

    Not many people seem to know about it, but you can use the doskey built-in macro tool, the only issue is that it doesn't save. There are many ways to work around this though.

    usage:

    doskey ls=dir
    

    ls will now do a directory listing just like dir would.

    If you want to use arguments with the commands, use this syntax:

    doskey d=dir $*
    

    As for the workaround to make them save:

    • save all aliases to a file in this format:
    doskey ls=dir
    doskey ..=cd ..

    and place it in one of the directories in your path. Name it something short like a.cmd, so when you open cmd you can type a to load your aliases.

    If typing an a and pressing Enter seems too much work, throw this into your AutoHotkey script:

    WinWaitActive, C:\WINDOWS\system32\cmd.exe
    Send {a}{Enter}
    

    Loading aliases automatically:

    You can change all shortcuts to cmd to point to %SystemRoot%\system32\cmd.exe /K C:\path\to\aliases.cmd, replacing C:\path\to\aliases.cmd with the location of your aliases file. If you typically run it from the run box, you can:

    • Rename the cmd executable to cmd2.exe for example, and replace it with a script or another executable which launches the above command (I wouldn't really recommend this method as a lot of apps depend on cmd)
    • Make a batch script and call it cmda (cmd with aliases) for example. Have it launch the above command and put this batch script somewhere in your path.
  • BillP3rd

    It's a simple as:

    1. Create a file with aliases, e.g. c:\bin\aliases:

      ls=dir /ONE $*
      cd=cd /d $*
      python=python -ic "" 
      ps=tasklist $*
      kill=taskkill /IM $*
      
    2. Create a file with all the stuff you want to run when cmd.exe is started, including loading the aliases with doskey e.g. c:\bin\cmd_autoruns.cmd:

      @echo off
      cls
      color 0A
      doskey /macrofile=c:\bin\aliases
      
    3. Create and run once a batch file (e.g. set_cmd_autorun.cmd) which will set the Command Processor Autorun key to our cmd_autoruns.cmd:

      reg add "hkcu\software\microsoft\command processor" /v Autorun /t reg_sz /d c:\bin\cmd_autoruns.cmd
      

    As an alternative to set_cmd_autorun.cmd it is also possible to instead create a .reg file like the one below and then merge it with a double click:

    REGEDIT4
    
    [HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
    "CompletionChar"=dword:00000009
    "DefaultColor"=dword:00000000
    "EnableExtensions"=dword:00000001
    "PathCompletionChar"=dword:00000009
    "Autorun"="c:\\bin\\cmd_autoruns.cmd"
    
  • andematt

    My answer is similar to vriolk's

    I created a .bat file that contained my macros (e.g. c:\winscripts\autoexec.bat):

    @doskey whereis=c:\winscripts\whereis.cmd $*
    @doskey ls=dir /b $*
    @doskey l=dir /od/p/q/tw $*
    

    and then from a cmd prompt ran "cmd /?" to find the registry key to edit for the cmd autorun:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
      and/or
    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
    

    using regedit, add the path for your macro batch file to the AutoRun value (add the AutoRun key if it's not there):

    c:\winscripts\autoexec.bat
    

    now whenever you run "cmd" from the Start->Run prompt, this autoexec.bat will also run and create the doskey macros for you.

    By the way, whereis.cmd contains this:

    @for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT "%%~$PATH:i"=="" echo %%~$PATH:i

    which searches your PATH variable for the term you provide:

    c:>whereis javaw
    c:\jdk\bin\javaw.exe
  • djhowell

    You can create .cmd files and place them someplace in your %PATH% (such as C:\Windows). To use your web alias as an example:

    @C:
    @cd \inetpub\wwwroot
    

    Would do something like:

    M:\> web
    C:\inetpub\wwwroot>
    

    I'm not aware of any way to make a flat .aliases style file.

  • John T

    The way I did it was with a quick python script:

    import sys
    import string
    import os
    import glob
    
    def listAll():
        	for infile in glob.glob("c:\\aliases\\*.bat"):
        		fileName = infile
        		fileName = fileName[len("c:\\aliases\\"):len(fileName)-4]
        		fileContents = open("c:\\aliases\\" + fileName + ".bat", "r")
        		fileContents.readline()
        		fileContentString=fileContents.readline()
        		fileName += " is aliased to "
        		fileName += fileContentString[0:len(fileContentString)-3]
        		print fileName
    
    def listSome(which):
        	for infile in glob.glob("c:\\aliases\\*.bat"):
        		fileName = infile
        		fileName = fileName[len("c:\\aliases\\"):len(fileName)-4]
        		fileContents = open("c:\\aliases\\" + fileName + ".bat", "r")
        		fileContents.readline()
        		fileContentString=fileContents.readline()
        		if fileName.find(which)==0:
        			fileName += " is aliased to "
        			fileName += fileContentString[0:len(fileContentString)-3]
        			print fileName
    
    if len(sys.argv)>1:
        if sys.argv[1]!="-p":
        	file = open("c:\\aliases\\"+sys.argv[1]+".bat", "w")
        	file.write("@ECHO OFF\n")
        	counter=0
        	totalInput=""
        	counter=0
        	for arg in sys.argv:
        		if counter > 1:
        			totalInput+= arg + " "
        		counter+=1
    
        	if totalInput.find(".exe")!=-1:
        		file.write("\"")
    
        	counter=0
    
        	for arg in sys.argv:
        		if counter > 1:
        			file.write(arg)
        			if sys.argv[1]==sys.argv[2]:
        				if counter==2:
        					file.write(".exe")
        			temparg=str(arg)
        			if temparg.find(".exe")!=-1:
        				file.write("\"")
        			file.write(" ")
        		counter+=1
        	file.write("%*")
    
        	print "Aliased " + sys.argv[1] + " to " + totalInput
        else:
        	if len(sys.argv)>2:
        		listSome(sys.argv[2])
        	else:
        		listAll()
    else:
        listAll()
    

    Apologies for the poor scripting, but the usage is quite nice, imo. Place it somewhere in your path, add .py to your PATHEXT, and add c:\aliases to your PATH too (or change it, whatever suits), then use:

    alias <command> <action>
    

    to alias (Yep, no =, though it wouldn't be hard to add a .split in there), and:

    alias -p <command or part of>
    

    To display what something is.

    Hackish, but stupidly useful. There's an equivalent unalias script, but I'm sure you can work that one out.

    edit: This obviously requires python, written on v26 but will probably work in anything recent. As before, sorry for the quality :)

    edit2: Actually, something like this but to add to the doskey stuff would be better. You can add startup commands to cmd with the autorun registry key, too, so that could be much cleaner.

  • harrymc

    You can have almost every linux utility and shell you used inschool, by installing Cygwin, a Linux-like environment for Windows.

    However, when working with a new operating system, it's much better to look forward rather than return to the past. Windows is a graphical environment, where graphical applications do almost everything that you used to do via the command line in Linux. Launching system commands is done therefore by mostly using the mouse, rather than the keyboard. "Be prepared" is the word.