Is there a way to edit a file if there are no vi, vim, joe, mcedit? In another words is there a way to edit a file using just the shell commands?
In that instance, I'd try transferring files out and editing them on another computer, then transfer them back. If you have ssh, you should have scp (I hope), so you should be able to push files in and out. If not, you can also look for ftp to transfer files in and out.
If not, then I think your best option is to try and make use of cat, grep, sed, echo, and I/O redirection (especially append with
>>). And lots of temporary files.
Though if you have access to perl (or something similar), you can run it with no arguments and it will let you input a script source from standard input. Once you press ctrl+d, it can then run the script. You could use that method to create a file. It would be more powerful than hacking something on the command line as I mentioned before.
Use a terminal that lets you copy and paste with the mouse, and assemble your files that way?
e.g. cat > myfile (use terminal to copy/paste) ^D
If it is a binary file, use this to turn it into text which you can copy with the mouse:
openssl base64 -in <infile>
then on another computer, openssl again to decode it using the -d switch, edit however you like (e.g. hexedit) then re-encode, and on the box with no editor, openssl again to decode it and paste from your terminal. do
Assuming you're SSH'ing into your router, you can also use various utilities to transfer the file back and fourth to your computer/router. You can download a copy on your PC, modify it, and then SSH it back to the router via SFTP (see Putty or WinSCP if you're a Windows user).
If you're using a custom firmware on the router/gateway, however, you may be in luck. There are various Optware packages containing simple (and small) text editors. Depending on your needs, you could get the
nano package, or just go for
busybox which contains
I imagine you could do what you need with
perl - look for the line you want with
grep, edit that line with
perl can act like a big replacement for
sed) and then confirm you didn't make more changes than you intended by doing
diff filename filename.new. If so, make the changes permanent -
mv filename.new filename
I was working on Router with BusyBox installed and it didn't provided any text editor. One of the ways I was able to achieve it was,
- Output the file content using cat
- Edit the files on your system/pc
- Upload the updated file on internet such as Transfer.sh
- Download the updated file on the router/device using curl command, in my case I had to use the --ignore switch as well to bypass certificate/https warning.
- Remove [RM] the old file.
- Rename/Move [MV] the new file with the old files' name.
Did you wipe out /bin or something? Otherwise maybe you could hack something together with the text utilities in the GNU Coreutils that should be standard on a linux system.
Install vi - as weird as it sounds, you can often copy an existing "vi" from another machine and it will run OK on your box. I've found a "/data" partition on a lot of devices where you can save it to, and run it from.
I've done this. It works.
If you want to improve your chances of getting it working first-try, find any existing executable on your box and run this over it:
readelf -h </bin/filename>
then check that the "vi" you get form elsewhere uses the same chipset, bitsize, etc.
Works for other programs too (tcpdump is another one I've succeeded with)